merge userdb.js into users.js
This commit is contained in:
@@ -30,6 +30,42 @@ var DISPLAY_NAME_NEW = 'Somone cares';
|
||||
var userObject = null;
|
||||
var AUDIT_SOURCE = { ip: '1.2.3.4', userId: 'someuserid' };
|
||||
|
||||
var USER_1 = {
|
||||
id: 'uuid1',
|
||||
username: 'uuid1',
|
||||
password: 'secret',
|
||||
email: 'safe2@me.com',
|
||||
fallbackEmail: 'safer2@me.com',
|
||||
salt: 'tata',
|
||||
resetToken: '',
|
||||
displayName: 'Herbert 1',
|
||||
twoFactorAuthenticationEnabled: false,
|
||||
twoFactorAuthenticationSecret: '',
|
||||
role: 'user',
|
||||
active: true,
|
||||
source: '',
|
||||
loginLocations: [],
|
||||
avatar: constants.AVATAR_GRAVATAR
|
||||
};
|
||||
|
||||
var USER_2 = {
|
||||
id: 'uuid2',
|
||||
username: 'uuid2',
|
||||
password: 'secret',
|
||||
email: 'safe3@me.com',
|
||||
fallbackEmail: 'safer3@me.com',
|
||||
salt: 'tata',
|
||||
resetToken: '',
|
||||
displayName: 'Herbert 2',
|
||||
twoFactorAuthenticationEnabled: false,
|
||||
twoFactorAuthenticationSecret: '',
|
||||
role: 'user',
|
||||
active: true,
|
||||
source: '',
|
||||
loginLocations: [],
|
||||
avatar: constants.AVATAR_NONE
|
||||
};
|
||||
|
||||
const DOMAIN_0 = {
|
||||
domain: 'example.com',
|
||||
zoneName: 'example.com',
|
||||
@@ -101,6 +137,239 @@ describe('User', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
describe('user', function () {
|
||||
function validateUser(a, b) {
|
||||
expect(a.creationTime).to.be.a(Date);
|
||||
expect(a.resetTokenCreationTime).to.be.a(Date);
|
||||
expect(_.omit(b, ['avatar'])).to.be.eql(_.omit(a, ['creationTime', 'resetTokenCreationTime']));
|
||||
}
|
||||
|
||||
it('can add user', function (done) {
|
||||
userdb.add(USER_0.id, USER_0, done);
|
||||
});
|
||||
|
||||
it('can add another user', function (done) {
|
||||
userdb.add(USER_1.id, USER_1, done);
|
||||
});
|
||||
|
||||
it('can add another user with empty username', function (done) {
|
||||
userdb.add(USER_2.id, USER_2, done);
|
||||
});
|
||||
|
||||
it('cannot add user with same email again', function (done) {
|
||||
var tmp = JSON.parse(JSON.stringify(USER_0));
|
||||
tmp.id = 'somethingelse';
|
||||
tmp.username = 'somethingelse';
|
||||
tmp.avatar = constants.AVATAR_GRAVATAR;
|
||||
|
||||
userdb.add(tmp.id, tmp, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
expect(error.message).to.equal('email already exists');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot add user with same username again', function (done) {
|
||||
var tmp = JSON.parse(JSON.stringify(USER_0));
|
||||
tmp.id = 'somethingelse';
|
||||
tmp.email = 'somethingelse@not.taken';
|
||||
tmp.avatar = constants.AVATAR_GRAVATAR;
|
||||
|
||||
userdb.add(tmp.id, tmp, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
expect(error.message).to.equal('username already exists');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get by user id', function (done) {
|
||||
userdb.get(USER_0.id, function (error, user) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
validateUser(user, USER_0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get by user name', function (done) {
|
||||
userdb.getByUsername(USER_0.username, function (error, user) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
validateUser(user, USER_0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get by email', function (done) {
|
||||
userdb.getByEmail(USER_0.email, function (error, user) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
validateUser(user, USER_0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('getByResetToken fails for empty resetToken', function (done) {
|
||||
userdb.getByResetToken('', function (error, user) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
expect(user).to.not.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('getByResetToken fails for invalid resetToken', function (done) {
|
||||
userdb.getByResetToken('invalid', function (error, user) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
expect(user).to.not.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get by resetToken', function (done) {
|
||||
userdb.getByResetToken(USER_0.resetToken, function (error, user) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
validateUser(user, USER_0);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get all with group ids', function (done) {
|
||||
userdb.getAllWithGroupIds(function (error, all) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(all.length).to.equal(3);
|
||||
|
||||
var userCopy;
|
||||
|
||||
userCopy = _.extend({}, USER_0);
|
||||
userCopy.groupIds = [ ];
|
||||
validateUser(all[0], userCopy);
|
||||
|
||||
userCopy = _.extend({}, USER_1);
|
||||
userCopy.groupIds = [ ];
|
||||
validateUser(all[1], userCopy);
|
||||
|
||||
userCopy = _.extend({}, USER_2);
|
||||
userCopy.groupIds = [ ];
|
||||
validateUser(all[2], userCopy);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get all with group ids paged', function (done) {
|
||||
userdb.getAllWithGroupIdsPaged(null, 1, 2, function (error, all) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(all.length).to.equal(2);
|
||||
|
||||
var userCopy;
|
||||
|
||||
userCopy = _.extend({}, USER_0);
|
||||
userCopy.groupIds = [];
|
||||
validateUser(all[0], userCopy);
|
||||
|
||||
userCopy = _.extend({}, USER_1);
|
||||
userCopy.groupIds = [];
|
||||
validateUser(all[1], userCopy);
|
||||
|
||||
userdb.getAllWithGroupIdsPaged(null, 2, 2, function (error, all) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(all.length).to.equal(1);
|
||||
|
||||
var userCopy;
|
||||
|
||||
userCopy = _.extend({}, USER_2);
|
||||
userCopy.groupIds = [];
|
||||
validateUser(all[0], userCopy);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can get all with group ids paged and search', function (done) {
|
||||
userdb.getAllWithGroupIdsPaged('id1', 1, 2, function (error, all) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(all.length).to.equal(1);
|
||||
|
||||
var userCopy;
|
||||
|
||||
userCopy = _.extend({}, USER_1);
|
||||
userCopy.groupIds = [];
|
||||
validateUser(all[0], userCopy);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get all admins', function (done) {
|
||||
userdb.getByRole('owner', function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.NOT_FOUND);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('counts the users', function (done) {
|
||||
userdb.count(function (error, count) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(count).to.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can get all users', function (done) {
|
||||
userdb.getByRole('user', function (error, all) {
|
||||
expect(error).to.not.be.ok();
|
||||
expect(all.length).to.equal(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can update the user', function (done) {
|
||||
userdb.update(USER_0.id, { email: 'some@thing.com', displayName: 'Heiter' }, function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
userdb.get(USER_0.id, function (error, user) {
|
||||
expect(user.email).to.equal('some@thing.com');
|
||||
expect(user.displayName).to.equal('Heiter');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('can update the user with already existing email', function (done) {
|
||||
userdb.update(USER_0.id, { email: USER_2.email }, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
expect(error.message).to.equal('email already exists');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can update the user with already existing username', function (done) {
|
||||
userdb.update(USER_0.id, { username: USER_2.username }, function (error) {
|
||||
expect(error).to.be.ok();
|
||||
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
|
||||
expect(error.message).to.equal('username already exists');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('cannot update with null field', function () {
|
||||
expect(function () {
|
||||
userdb.update(USER_0.id, { email: null }, function () {});
|
||||
}).to.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
describe('create', function() {
|
||||
before(cleanupUsers);
|
||||
after(cleanupUsers);
|
||||
|
||||
Reference in New Issue
Block a user