2013-05-24 08:02:41 +04:00
|
|
|
/*globals describe, beforeEach, it*/
|
2013-06-25 15:43:15 +04:00
|
|
|
var _ = require('underscore'),
|
|
|
|
should = require('should'),
|
|
|
|
helpers = require('./helpers'),
|
|
|
|
errors = require('../../shared/errorHandling'),
|
|
|
|
Models = require('../../shared/models'),
|
|
|
|
when = require('when');
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
describe('User Model', function () {
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
var UserModel = Models.User;
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
beforeEach(function (done) {
|
2013-07-11 16:50:31 +04:00
|
|
|
helpers.resetData().then(function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
return when(helpers.insertDefaultUser());
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-07-11 03:01:42 +04:00
|
|
|
}, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can add first', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass1',
|
|
|
|
email_address: "test@test1.com"
|
|
|
|
};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
helpers.resetData().then(function () {
|
2013-06-25 15:43:15 +04:00
|
|
|
UserModel.add(userData).then(function (createdUser) {
|
|
|
|
should.exist(createdUser);
|
|
|
|
createdUser.has('uuid').should.equal(true);
|
|
|
|
createdUser.attributes.password.should.not.equal(userData.password, "password was hashed");
|
|
|
|
createdUser.attributes.email_address.should.eql(userData.email_address, "email address corred");
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-15 02:12:04 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can\'t add second', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass3',
|
|
|
|
email_address: "test3@test1.com"
|
|
|
|
};
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-07-11 03:01:42 +04:00
|
|
|
return UserModel.add(userData).then(done, function (failure) {
|
|
|
|
failure.message.should.eql('A user is already registered. Only one user for now!');
|
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can browse', function (done) {
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can read', function (done) {
|
|
|
|
var firstUser;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return UserModel.read({email_address: firstUser.attributes.email_address});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (found) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(found);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
found.attributes.full_name.should.equal(firstUser.attributes.full_name);
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can edit', function (done) {
|
|
|
|
var firstUser;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return UserModel.edit({id: firstUser.id, url: "some.newurl.com"});
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(edited);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
edited.attributes.url.should.equal('some.newurl.com');
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it("can get effective permissions", function (done) {
|
|
|
|
UserModel.effectivePermissions(1).then(function (effectivePermissions) {
|
|
|
|
should.exist(effectivePermissions);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
effectivePermissions.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
done();
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
it('can delete', function (done) {
|
|
|
|
var firstUserId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
firstUserId = results.models[0].id;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return UserModel.destroy(firstUserId);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return UserModel.browse();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}).then(function (newResults) {
|
|
|
|
var ids, hasDeletedId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-06-25 15:43:15 +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-06-25 15:43:15 +04:00
|
|
|
ids = _.pluck(newResults.models, "id");
|
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstUserId;
|
|
|
|
});
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
hasDeletedId.should.equal(false);
|
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-07-11 16:50:31 +04:00
|
|
|
}).then(null, done);
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|