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
+19 -38
View File
@@ -53,56 +53,37 @@ describe('App passwords', function () {
expect(results[0].identifier).to.be('appid');
});
it('can verify app password', function (done) {
users.verify(ADMIN.id, password, 'appid', function (error, result) {
expect(error).to.not.be.ok();
expect(result).to.be.ok();
expect(result.appPassword).to.be(true);
done();
});
it('can verify app password', async function () {
const result = await users.verify(ADMIN.id, password, 'appid');
expect(result).to.be.ok();
expect(result.appPassword).to.be(true);
});
it('can verify non-app password', function (done) {
users.verify(ADMIN.id, ADMIN.password, 'appid', function (error, result) {
expect(error).to.not.be.ok();
expect(result).to.be.ok();
expect(result.appPassword).to.be(undefined);
done();
});
it('can verify non-app password', async function () {
const result = await users.verify(ADMIN.id, ADMIN.password, 'appid');
expect(result).to.be.ok();
expect(result.appPassword).to.be(undefined);
});
it('cannot verify bad password', function (done) {
users.verify(ADMIN.id, 'bad', 'appid', function (error, result) {
expect(error).to.be.ok();
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
done();
});
it('cannot verify bad password', async function () {
const [error, result] = await safe(users.verify(ADMIN.id, 'bad', 'appid'));
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
});
it('cannot verify password for another app', function (done) {
users.verify(ADMIN.id, password, 'appid2', function (error, result) {
expect(error).to.be.ok();
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
done();
});
it('cannot verify password for another app', async function () {
const [error, result] = await safe(users.verify(ADMIN.id, password, 'appid2'));
expect(result).to.not.be.ok();
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
});
it('can del app password', async function () {
await appPasswords.del(id);
});
it('cannot verify deleted app password', function (done) {
users.verify(ADMIN.id, password, 'appid', function (error) {
expect(error).to.be.ok();
done();
});
it('cannot verify deleted app password', async function () {
const [error] = await safe(users.verify(ADMIN.id, password, 'appid'));
expect(error.reason).to.be(BoxError.INVALID_CREDENTIALS);
});
it('cannot del random app password', async function () {
+1 -2
View File
@@ -13,8 +13,7 @@ const appdb = require('../appdb.js'),
common = require('./common.js'),
domains = require('../domains.js'),
expect = require('expect.js'),
hat = require('../hat.js'),
userdb = require('../userdb.js');
hat = require('../hat.js');
let AUDIT_SOURCE = { ip: '1.2.3.4' };
+7 -10
View File
@@ -18,7 +18,6 @@ const appdb = require('../appdb.js'),
settings = require('../settings.js'),
settingsdb = require('../settingsdb.js'),
tasks = require('../tasks.js'),
userdb = require('../userdb.js'),
users = require('../users.js');
const MANIFEST = {
@@ -168,18 +167,16 @@ function setup(done) {
settings.initCache,
blobs.initSecrets,
domains.add.bind(null, DOMAIN.domain, DOMAIN, AUDIT_SOURCE),
function createOwner(done) {
users.createOwner(ADMIN.username, ADMIN.password, ADMIN.email, ADMIN.displayName, AUDIT_SOURCE, function (error, result) {
if (error) return done(error);
ADMIN.id = result.id;
done();
});
async function createOwner() {
const result = await users.createOwner(ADMIN.email, ADMIN.username, ADMIN.password, ADMIN.displayName, AUDIT_SOURCE);
ADMIN.id = result.id;
},
appdb.add.bind(null, APP.id, APP.appStoreId, APP.manifest, APP.location, APP.domain, APP.portBindings, APP),
settingsdb.set.bind(null, settings.CLOUDRON_TOKEN_KEY, exports.APPSTORE_TOKEN), // appstore token
userdb.add.bind(null, USER.id, USER),
users.setPassword.bind(null, USER, USER.password),
async function createUser() {
const result = await users.add(USER.email, USER, AUDIT_SOURCE);
USER.id = result.id;
},
(done) => mailboxdb.addMailbox(exports.MAILBOX_NAME, DOMAIN.domain, { ownerId: USER.id, ownerType: mail.OWNERTYPE_USER, active: true }, done),
(done) => mailboxdb.setAliasesForName(exports.MAILBOX_NAME, DOMAIN.domain, [ { name: exports.ALIAS_NAME, domain: DOMAIN.domain} ], done),
-295
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);
});
+269
View File
@@ -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);