Add integration test for global bans

This commit is contained in:
Calvin Montgomery 2017-06-05 22:45:14 -07:00
parent 8ad9b4e543
commit b80a87ba01
5 changed files with 119 additions and 9 deletions

View File

@ -0,0 +1,91 @@
const assert = require('assert');
const GlobalBanDB = require('../../lib/db/globalban').GlobalBanDB;
const testDB = require('../testutil/db').testDB;
const globalBanDB = new GlobalBanDB(testDB);
const testBan = { ip: '8.8.8.8', reason: 'test' };
function cleanupTestBan() {
return testDB.knex.table('global_bans')
.where({ ip: testBan.ip })
.del();
}
function setupTestBan() {
return testDB.knex.table('global_bans')
.insert(testBan)
.catch(error => {
if (error.code === 'ER_DUP_ENTRY') {
return testDB.knex.table('global_bans')
.where({ ip: testBan.ip })
.update({ reason: testBan.reason });
}
throw error;
});
}
describe('GlobalBanDB', () => {
describe('#listGlobalBans', () => {
beforeEach(setupTestBan);
afterEach(cleanupTestBan);
it('lists existing IP bans', () => {
return globalBanDB.listGlobalBans().then(bans => {
assert.deepStrictEqual([{
ip: '8.8.8.8',
reason: 'test'
}], bans);
});
});
});
describe('#addGlobalIPBan', () => {
beforeEach(cleanupTestBan);
afterEach(cleanupTestBan);
it('adds a new ban', () => {
return globalBanDB.addGlobalIPBan('8.8.8.8', 'test').then(() => {
return testDB.knex.table('global_bans')
.where({ ip: '8.8.8.8' })
.select()
.then(rows => {
assert.strictEqual(rows.length, 1, 'Expected 1 row');
assert.strictEqual(rows[0].ip, '8.8.8.8');
assert.strictEqual(rows[0].reason, 'test');
});
});
});
it('updates the reason on an existing ban', () => {
return globalBanDB.addGlobalIPBan('8.8.8.8', 'test').then(() => {
return globalBanDB.addGlobalIPBan('8.8.8.8', 'different').then(() => {
return testDB.knex.table('global_bans')
.where({ ip: '8.8.8.8' })
.select()
.then(rows => {
assert.strictEqual(rows.length, 1, 'Expected 1 row');
assert.strictEqual(rows[0].ip, '8.8.8.8');
assert.strictEqual(rows[0].reason, 'different');
});
});
});
});
});
describe('#removeGlobalIPBan', () => {
beforeEach(setupTestBan);
afterEach(cleanupTestBan);
it('removes a ban', () => {
return globalBanDB.removeGlobalIPBan('8.8.8.8').then(() => {
return testDB.knex.table('global_bans')
.where({ ip: '8.8.8.8' })
.select()
.then(rows => {
assert.strictEqual(rows.length, 0, 'Expected 0 rows');
});
});
});
});
});

View File

@ -1,13 +1,11 @@
const assert = require('assert'); const assert = require('assert');
const KickbanModule = require('../../lib/channel/kickban'); const KickbanModule = require('../../lib/channel/kickban');
const db = require('../../lib/database'); const database = require('../../lib/database');
const dbChannels = require('../../lib/database/channels'); const dbChannels = require('../../lib/database/channels');
const Promise = require('bluebird'); const Promise = require('bluebird');
const Config = require('../../lib/config');
const ChannelModule = require('../../lib/channel/module'); const ChannelModule = require('../../lib/channel/module');
const Flags = require('../../lib/flags'); const Flags = require('../../lib/flags');
const TestConfig = require('../../integration-test-config.json'); const testDB = require('../testutil/db').testDB;
require('../../lib/counters');
function randomString(length) { function randomString(length) {
const chars = 'abcdefgihkmnpqrstuvwxyz0123456789'; const chars = 'abcdefgihkmnpqrstuvwxyz0123456789';
@ -18,8 +16,7 @@ function randomString(length) {
return str; return str;
} }
Config.set('mysql.password', TestConfig.mysql.password); database.init(testDB);
db.init();
describe('onPreUserJoin Ban Check', () => { describe('onPreUserJoin Ban Check', () => {
const channelName = `test_${randomString(20)}`; const channelName = `test_${randomString(20)}`;

View File

@ -0,0 +1,14 @@
const loadFromToml = require('cytube-common/lib/configuration/configloader').loadFromToml;
const path = require('path');
class IntegrationTestConfig {
constructor(config) {
this.config = config;
}
get knexConfig() {
return this.config.database;
}
}
exports.testConfig = loadFromToml(IntegrationTestConfig, path.resolve(__dirname, '..', '..', 'conf', 'integration-test.toml'));

View File

@ -0,0 +1,4 @@
const testConfig = require('./config').testConfig;
const Database = require('../../lib/database').Database;
exports.testDB = new Database(testConfig.knexConfig);

View File

@ -48,8 +48,12 @@ class Database {
module.exports.Database = Database; module.exports.Database = Database;
module.exports.init = function () { module.exports.init = function (newDB) {
if (newDB) {
db = newDB;
} else {
db = new Database(); db = new Database();
}
db.knex.raw('select 1 from dual') db.knex.raw('select 1 from dual')
.catch(error => { .catch(error => {
LOGGER.error('Initial database connection failed: %s', error.stack); LOGGER.error('Initial database connection failed: %s', error.stack);