merge userdb.js into users.js

This commit is contained in:
Girish Ramakrishnan
2021-07-15 09:50:11 -07:00
parent 2840bba4bf
commit a1c61facdc
27 changed files with 1021 additions and 1456 deletions

View File

@@ -9,71 +9,14 @@ const appdb = require('../appdb.js'),
apps = require('../apps.js'),
async = require('async'),
BoxError = require('../boxerror.js'),
constants = require('../constants.js'),
database = require('../database'),
domaindb = require('../domaindb'),
expect = require('expect.js'),
hat = require('../hat.js'),
mailboxdb = require('../mailboxdb.js'),
reverseProxy = require('../reverseproxy.js'),
settingsdb = require('../settingsdb.js'),
userdb = require('../userdb.js'),
_ = require('underscore');
var USER_0 = {
id: 'uuid0',
username: 'uuid0',
password: 'secret',
email: 'safe@me.com',
fallbackEmail: 'safer@me.com',
salt: 'morton',
resetToken: hat(256),
displayName: '',
twoFactorAuthenticationEnabled: false,
twoFactorAuthenticationSecret: '',
role: 'user',
active: true,
source: '',
loginLocations: [],
avatar: constants.AVATAR_GRAVATAR
};
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: 'foobar.com',
zoneName: 'foobar.com',
@@ -110,10 +53,6 @@ describe('database', function () {
});
describe('domains', function () {
before(function (done) {
userdb.add(USER_0.id, USER_0, done);
});
after(function (done) {
database._clear(done);
});
@@ -265,239 +204,6 @@ describe('database', function () {
});
});
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('apps', function () {
var APP_0 = {
id: 'appid-0',
@@ -580,7 +286,6 @@ describe('database', function () {
before(function (done) {
async.series([
database._clear,
userdb.add.bind(null, USER_0.id, USER_0),
domaindb.add.bind(null, DOMAIN_0.domain, DOMAIN_0)
], done);
});