2013-05-24 08:02:41 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
"use strict";
|
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
should = require('should'),
|
2013-05-24 08:02:41 +04:00
|
|
|
helpers = require('./helpers'),
|
2013-05-31 02:34:53 +04:00
|
|
|
UserProvider = require('../../shared/models/dataProvider.bookshelf.users');
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
describe('Bookshelf UsersProvider', function () {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
var users;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
helpers.resetData().then(function () {
|
2013-05-31 02:34:53 +04:00
|
|
|
users = new UserProvider();
|
2013-05-25 20:48:15 +04:00
|
|
|
done();
|
2013-05-31 02:34:53 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can browse', function (done) {
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can read', function (done) {
|
|
|
|
var firstUser;
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
firstUser = results.models[0];
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
return users.read({email_address: firstUser.attributes.email_address});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
}).then(function (found) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
should.exist(found);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
found.attributes.username.should.equal(firstUser.attributes.username);
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can edit', function (done) {
|
|
|
|
var firstUser;
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-25 20:48:15 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
return users.edit({id: firstUser.id, url: "some.newurl.com"});
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
should.exist(edited);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-05-27 05:39:38 +04:00
|
|
|
edited.attributes.url.should.equal('some.newurl.com');
|
|
|
|
|
|
|
|
done();
|
|
|
|
|
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can add', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass1',
|
|
|
|
email_address: "test@test1.com"
|
|
|
|
};
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
users.add(userData).then(function (createdUser) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(createdUser);
|
|
|
|
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, "password was hashed");
|
|
|
|
createdUser.attributes.email_address.should.eql(userData.email_address, "email address corred");
|
|
|
|
|
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can delete', function (done) {
|
2013-05-27 03:53:44 +04:00
|
|
|
var firstUserId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
users.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
|
|
|
should.exist(results);
|
|
|
|
|
|
|
|
results.length.should.be.above(0);
|
|
|
|
|
|
|
|
firstUserId = results.models[0].id;
|
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
return users.destroy(firstUserId);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 03:53:44 +04:00
|
|
|
}).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-31 02:34:53 +04:00
|
|
|
return users.browse();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 03:53:44 +04:00
|
|
|
}).then(function (newResults) {
|
|
|
|
var ids, hasDeletedId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 03:53:44 +04:00
|
|
|
if (newResults.length < 1) {
|
|
|
|
// Bug out if we only had one user and deleted it.
|
|
|
|
return done();
|
|
|
|
}
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 03:53:44 +04:00
|
|
|
ids = _.pluck(newResults.models, "id");
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-05-27 03:53:44 +04:00
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstUserId;
|
2013-05-25 20:48:15 +04:00
|
|
|
});
|
2013-05-27 03:53:44 +04:00
|
|
|
|
|
|
|
hasDeletedId.should.equal(false);
|
|
|
|
|
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
|
|
|
}).then(null, done);
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}());
|