From b80a87ba015b8cd99d49ca2226ef8fa1498a3697 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Mon, 5 Jun 2017 22:45:14 -0700 Subject: [PATCH] Add integration test for global bans --- integration_test/db/globalban.js | 91 +++++++++++++++++++ .../regressions/checkban-blank-name.js | 11 +-- integration_test/testutil/config.js | 14 +++ integration_test/testutil/db.js | 4 + src/database.js | 8 +- 5 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 integration_test/db/globalban.js create mode 100644 integration_test/testutil/config.js create mode 100644 integration_test/testutil/db.js diff --git a/integration_test/db/globalban.js b/integration_test/db/globalban.js new file mode 100644 index 00000000..37a091b5 --- /dev/null +++ b/integration_test/db/globalban.js @@ -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'); + }); + }); + }); + }); +}); diff --git a/integration_test/regressions/checkban-blank-name.js b/integration_test/regressions/checkban-blank-name.js index 9976a944..0b3d9907 100644 --- a/integration_test/regressions/checkban-blank-name.js +++ b/integration_test/regressions/checkban-blank-name.js @@ -1,13 +1,11 @@ const assert = require('assert'); const KickbanModule = require('../../lib/channel/kickban'); -const db = require('../../lib/database'); +const database = require('../../lib/database'); const dbChannels = require('../../lib/database/channels'); const Promise = require('bluebird'); -const Config = require('../../lib/config'); const ChannelModule = require('../../lib/channel/module'); const Flags = require('../../lib/flags'); -const TestConfig = require('../../integration-test-config.json'); -require('../../lib/counters'); +const testDB = require('../testutil/db').testDB; function randomString(length) { const chars = 'abcdefgihkmnpqrstuvwxyz0123456789'; @@ -18,8 +16,7 @@ function randomString(length) { return str; } -Config.set('mysql.password', TestConfig.mysql.password); -db.init(); +database.init(testDB); describe('onPreUserJoin Ban Check', () => { const channelName = `test_${randomString(20)}`; @@ -127,4 +124,4 @@ describe('onPreUserJoin Ban Check', () => { done(); }); }); -}); \ No newline at end of file +}); diff --git a/integration_test/testutil/config.js b/integration_test/testutil/config.js new file mode 100644 index 00000000..0b681cb5 --- /dev/null +++ b/integration_test/testutil/config.js @@ -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')); diff --git a/integration_test/testutil/db.js b/integration_test/testutil/db.js new file mode 100644 index 00000000..0abb5097 --- /dev/null +++ b/integration_test/testutil/db.js @@ -0,0 +1,4 @@ +const testConfig = require('./config').testConfig; +const Database = require('../../lib/database').Database; + +exports.testDB = new Database(testConfig.knexConfig); diff --git a/src/database.js b/src/database.js index 63e160e7..402afe68 100644 --- a/src/database.js +++ b/src/database.js @@ -48,8 +48,12 @@ class Database { module.exports.Database = Database; -module.exports.init = function () { - db = new Database(); +module.exports.init = function (newDB) { + if (newDB) { + db = newDB; + } else { + db = new Database(); + } db.knex.raw('select 1 from dual') .catch(error => { LOGGER.error('Initial database connection failed: %s', error.stack);