2013-09-06 20:07:25 +04:00
|
|
|
/*globals describe, before, beforeEach, afterEach, it*/
|
|
|
|
var testUtils = require('./testUtils'),
|
2013-06-25 15:43:15 +04:00
|
|
|
should = require('should'),
|
2013-09-06 20:07:25 +04:00
|
|
|
when = require('when'),
|
|
|
|
_ = require('underscore'),
|
2013-07-11 23:02:18 +04:00
|
|
|
errors = require('../../server/errorHandling'),
|
2013-09-06 20:07:25 +04:00
|
|
|
|
|
|
|
// Stuff we are testing
|
|
|
|
Models = require('../../server/models');
|
|
|
|
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
describe('User Model', function run() {
|
2013-06-25 15:43:15 +04:00
|
|
|
var UserModel = Models.User;
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
before(function (done) {
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.clearData().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-08-25 00:51:58 +04:00
|
|
|
afterEach(function (done) {
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.clearData().then(function () {
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Registration', function runRegistration() {
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.timeout(5000);
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.initData().then(function () {
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can add first', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass1',
|
|
|
|
email_address: "test@test1.com"
|
|
|
|
};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
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");
|
2013-08-25 14:49:31 +04:00
|
|
|
createdUser.attributes.email_address.should.eql(userData.email_address, "email address correct");
|
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-08-25 00:51:58 +04:00
|
|
|
describe('Basic Operations', function () {
|
|
|
|
|
|
|
|
beforeEach(function (done) {
|
|
|
|
this.timeout(5000);
|
2013-09-06 20:07:25 +04:00
|
|
|
testUtils.initData()
|
2013-08-25 00:51:58 +04:00
|
|
|
.then(function () {
|
2013-09-06 20:07:25 +04:00
|
|
|
return when(testUtils.insertDefaultUser());
|
2013-08-25 00:51:58 +04:00
|
|
|
})
|
|
|
|
.then(function () {
|
|
|
|
done();
|
|
|
|
}, done);
|
|
|
|
});
|
2013-06-15 02:12:04 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
it('can\'t add second', function (done) {
|
|
|
|
var userData = {
|
|
|
|
password: 'testpass3',
|
|
|
|
email_address: "test3@test1.com"
|
|
|
|
};
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-09-06 20:07:25 +04:00
|
|
|
return testUtils.insertDefaultUser().then(function () {
|
2013-08-25 00:51:58 +04:00
|
|
|
UserModel.add(userData).then(done, function (failure) {
|
|
|
|
failure.message.should.eql('A user is already registered. Only one user for now!');
|
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
|
|
|
});
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
it('can browse', function (done) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
it('can read', function (done) {
|
|
|
|
var firstUser;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
return UserModel.read({email_address: firstUser.attributes.email_address});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function (found) {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(found);
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
found.attributes.full_name.should.equal(firstUser.attributes.full_name);
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(null, done);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
it('can edit', function (done) {
|
|
|
|
var firstUser;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(results);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
firstUser = results.models[0];
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
return UserModel.edit({id: firstUser.id, url: "some.newurl.com"});
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function (edited) {
|
2013-05-24 08:02:41 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(edited);
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
edited.attributes.url.should.equal('some.newurl.com');
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +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-08-25 00:51:58 +04:00
|
|
|
effectivePermissions.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
done();
|
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
it('can delete', function (done) {
|
|
|
|
var firstUserId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
UserModel.browse().then(function (results) {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
should.exist(results);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
results.length.should.be.above(0);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
firstUserId = results.models[0].id;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
return UserModel.destroy(firstUserId);
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function () {
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
return UserModel.browse();
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(function (newResults) {
|
|
|
|
var ids, hasDeletedId;
|
2013-05-25 20:48:15 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
if (newResults.length < 1) {
|
|
|
|
// Bug out if we only had one user and deleted it.
|
|
|
|
return done();
|
|
|
|
}
|
2013-05-27 03:53:44 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
ids = _.pluck(newResults.models, "id");
|
|
|
|
hasDeletedId = _.any(ids, function (id) {
|
|
|
|
return id === firstUserId;
|
|
|
|
});
|
|
|
|
|
|
|
|
hasDeletedId.should.equal(false);
|
|
|
|
done();
|
2013-05-27 05:39:38 +04:00
|
|
|
|
2013-08-25 00:51:58 +04:00
|
|
|
}).then(null, done);
|
|
|
|
});
|
2013-05-24 08:02:41 +04:00
|
|
|
});
|
2013-09-06 20:07:25 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|