Add integ test for verifyLogin

This commit is contained in:
Calvin Montgomery 2020-08-21 20:31:54 -07:00
parent f081bc782a
commit 80d3d14c85
4 changed files with 76 additions and 5 deletions

View File

@ -0,0 +1,65 @@
const assert = require('assert');
const { testDB } = require('../testutil/db');
const accounts = require('../../lib/database/accounts');
require('../../lib/database').init(testDB);
describe('AccountsDatabase', () => {
describe('#verifyLogin', () => {
let ip = '169.254.111.111';
let user;
let password;
beforeEach(async () => {
return testDB.knex.table('users')
.where({ ip })
.delete();
});
beforeEach(done => {
user = `u${Math.random().toString(31).substring(2)}`;
password = 'int!gration_Test';
accounts.register(
user,
password,
'',
ip,
(error, res) => {
if (error) {
throw error;
}
console.log(`Created test user ${user}`);
done();
}
)
});
it('verifies a correct login', done => {
accounts.verifyLogin(
user,
password,
(error, res) => {
if (error) {
throw error;
}
assert.strictEqual(res.name, user);
done();
}
);
});
it('rejects an incorrect login', done => {
accounts.verifyLogin(
user,
'not the right password',
(error, res) => {
assert.strictEqual(error, 'Invalid username/password combination');
done();
}
);
});
});
});

View File

@ -1,6 +1,7 @@
const assert = require('assert'); const assert = require('assert');
const GlobalBanDB = require('../../lib/db/globalban').GlobalBanDB; const GlobalBanDB = require('../../lib/db/globalban').GlobalBanDB;
const testDB = require('../testutil/db').testDB; const testDB = require('../testutil/db').testDB;
const { o } = require('../testutil/o');
const globalBanDB = new GlobalBanDB(testDB); const globalBanDB = new GlobalBanDB(testDB);
const testBan = { ip: '8.8.8.8', reason: 'test' }; const testBan = { ip: '8.8.8.8', reason: 'test' };
@ -35,7 +36,7 @@ describe('GlobalBanDB', () => {
assert.deepStrictEqual([{ assert.deepStrictEqual([{
ip: '8.8.8.8', ip: '8.8.8.8',
reason: 'test' reason: 'test'
}], bans); }], bans.map(o));
}); });
}); });
}); });

View File

@ -1,6 +1,7 @@
const assert = require('assert'); const assert = require('assert');
const PasswordResetDB = require('../../lib/db/password-reset').PasswordResetDB; const PasswordResetDB = require('../../lib/db/password-reset').PasswordResetDB;
const testDB = require('../testutil/db').testDB; const testDB = require('../testutil/db').testDB;
const { o } = require('../testutil/o');
const passwordResetDB = new PasswordResetDB(testDB); const passwordResetDB = new PasswordResetDB(testDB);
@ -27,7 +28,7 @@ describe('PasswordResetDB', () => {
.select(); .select();
}).then(rows => { }).then(rows => {
assert.strictEqual(rows.length, 1); assert.strictEqual(rows.length, 1);
assert.deepStrictEqual(rows[0], params); assert.deepStrictEqual(o(rows[0]), params);
}); });
}); });
@ -45,7 +46,7 @@ describe('PasswordResetDB', () => {
.select(); .select();
}).then(rows => { }).then(rows => {
assert.strictEqual(rows.length, 1); assert.strictEqual(rows.length, 1);
assert.deepStrictEqual(rows[0], params); assert.deepStrictEqual(o(rows[0]), params);
}); });
}); });
}); });
@ -65,7 +66,7 @@ describe('PasswordResetDB', () => {
it('gets a password reset by hash', () => { it('gets a password reset by hash', () => {
return passwordResetDB.get(reset.hash).then(result => { return passwordResetDB.get(reset.hash).then(result => {
assert.deepStrictEqual(result, reset); assert.deepStrictEqual(o(result), reset);
}); });
}); });
@ -136,7 +137,7 @@ describe('PasswordResetDB', () => {
.select(); .select();
}).then(rows => { }).then(rows => {
assert.strictEqual(rows.length, 1); assert.strictEqual(rows.length, 1);
assert.deepStrictEqual(rows[0], reset2); assert.deepStrictEqual(o(rows[0]), reset2);
}); });
}); });
}); });

View File

@ -0,0 +1,4 @@
exports.o = function o(obj) {
// Workaround for knex returning RowDataPacket and failing assertions
return Object.assign({}, obj);
}