Fixup database tests

This commit is contained in:
Johannes Zellner
2021-07-13 10:38:47 +02:00
parent b67dca8a61
commit 227196138c

View File

@@ -11,6 +11,7 @@ const appdb = require('../appdb.js'),
backupdb = require('../backupdb.js'),
backups = require('../backups.js'),
BoxError = require('../boxerror.js'),
constants = require('../constants.js'),
database = require('../database'),
domaindb = require('../domaindb'),
expect = require('expect.js'),
@@ -29,7 +30,6 @@ var USER_0 = {
email: 'safe@me.com',
fallbackEmail: 'safer@me.com',
salt: 'morton',
creationTime: Date.now(),
resetToken: hat(256),
displayName: '',
twoFactorAuthenticationEnabled: false,
@@ -38,7 +38,7 @@ var USER_0 = {
active: true,
source: '',
loginLocations: [],
resetTokenCreationTime: Date.now()
avatar: constants.AVATAR_GRAVATAR
};
var USER_1 = {
@@ -48,7 +48,6 @@ var USER_1 = {
email: 'safe2@me.com',
fallbackEmail: 'safer2@me.com',
salt: 'tata',
creationTime: Date.now(),
resetToken: '',
displayName: 'Herbert 1',
twoFactorAuthenticationEnabled: false,
@@ -57,7 +56,7 @@ var USER_1 = {
active: true,
source: '',
loginLocations: [],
resetTokenCreationTime: Date.now()
avatar: constants.AVATAR_GRAVATAR
};
var USER_2 = {
@@ -67,7 +66,6 @@ var USER_2 = {
email: 'safe3@me.com',
fallbackEmail: 'safer3@me.com',
salt: 'tata',
creationTime: Date.now(),
resetToken: '',
displayName: 'Herbert 2',
twoFactorAuthenticationEnabled: false,
@@ -76,7 +74,7 @@ var USER_2 = {
active: true,
source: '',
loginLocations: [],
resetTokenCreationTime: Date.now()
avatar: constants.AVATAR_NONE
};
const DOMAIN_0 = {
@@ -271,6 +269,12 @@ 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);
});
@@ -283,10 +287,11 @@ describe('database', function () {
userdb.add(USER_2.id, USER_2, done);
});
it('cannot add user width same email again', function (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();
@@ -296,10 +301,11 @@ describe('database', function () {
});
});
it('cannot add user width same username again', function (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();
@@ -312,7 +318,9 @@ describe('database', function () {
it('can get by user id', function (done) {
userdb.get(USER_0.id, function (error, user) {
expect(error).to.not.be.ok();
expect(user).to.eql(USER_0);
validateUser(user, USER_0);
done();
});
});
@@ -320,7 +328,9 @@ describe('database', function () {
it('can get by user name', function (done) {
userdb.getByUsername(USER_0.username, function (error, user) {
expect(error).to.not.be.ok();
expect(user).to.eql(USER_0);
validateUser(user, USER_0);
done();
});
});
@@ -328,7 +338,9 @@ describe('database', function () {
it('can get by email', function (done) {
userdb.getByEmail(USER_0.email, function (error, user) {
expect(error).to.not.be.ok();
expect(user).to.eql(USER_0);
validateUser(user, USER_0);
done();
});
});
@@ -354,7 +366,9 @@ describe('database', function () {
it('can get by resetToken', function (done) {
userdb.getByResetToken(USER_0.resetToken, function (error, user) {
expect(error).to.not.be.ok();
expect(user).to.eql(USER_0);
validateUser(user, USER_0);
done();
});
});
@@ -368,15 +382,15 @@ describe('database', function () {
userCopy = _.extend({}, USER_0);
userCopy.groupIds = [ ];
expect(all[0]).to.eql(userCopy);
validateUser(all[0], userCopy);
userCopy = _.extend({}, USER_1);
userCopy.groupIds = [ ];
expect(all[1]).to.eql(userCopy);
validateUser(all[1], userCopy);
userCopy = _.extend({}, USER_2);
userCopy.groupIds = [ ];
expect(all[2]).to.eql(userCopy);
validateUser(all[2], userCopy);
done();
});
@@ -391,11 +405,11 @@ describe('database', function () {
userCopy = _.extend({}, USER_0);
userCopy.groupIds = [];
expect(all[0]).to.eql(userCopy);
validateUser(all[0], userCopy);
userCopy = _.extend({}, USER_1);
userCopy.groupIds = [];
expect(all[1]).to.eql(userCopy);
validateUser(all[1], userCopy);
userdb.getAllWithGroupIdsPaged(null, 2, 2, function (error, all) {
expect(error).to.not.be.ok();
@@ -405,7 +419,7 @@ describe('database', function () {
userCopy = _.extend({}, USER_2);
userCopy.groupIds = [];
expect(all[0]).to.eql(userCopy);
validateUser(all[0], userCopy);
done();
});
@@ -421,7 +435,7 @@ describe('database', function () {
userCopy = _.extend({}, USER_1);
userCopy.groupIds = [];
expect(all[0]).to.eql(userCopy);
validateUser(all[0], userCopy);
done();
});