init commit

This commit is contained in:
rainbownapkin 2021-12-06 19:56:40 -05:00
parent ae639426d0
commit 7a491681cc
257 changed files with 95524 additions and 80 deletions

108
test/db/aliases.js Normal file
View file

@ -0,0 +1,108 @@
const assert = require('assert');
const sinon = require('sinon');
const TestUtilDB = require('../testutil/db');
const AliasesDB = require('../../lib/db/aliases').AliasesDB;
describe('AliasesDB', () => {
let mockTx, mockDB, aliasesDB;
beforeEach(() => {
mockTx = new TestUtilDB.MockTx();
mockDB = new TestUtilDB.MockDB(mockTx);
aliasesDB = new AliasesDB(mockDB);
});
describe('#addAlias', () => {
it('adds a new alias', () => {
const ip = '1.2.3.4';
const name = 'foo';
sinon.stub(mockTx, 'table').withArgs('aliases').returns(mockTx);
sinon.stub(mockTx, 'where').withArgs({ ip, name }).returns(mockTx);
const del = sinon.stub(mockTx, 'del').resolves();
const insert = sinon.stub(mockTx, 'insert').resolves();
return aliasesDB.addAlias(ip, name).then(() => {
assert(del.called, 'Expected old alias to be purged');
assert(insert.called, 'Expected new alias to be inserted');
const record = insert.getCall(0).args[0];
assert.strictEqual(record.ip, ip);
assert.strictEqual(record.name, name);
assert(typeof record.time === 'number', 'Expected time field to be a number');
});
});
});
describe('#getAliasesByIP', () => {
it('retrieves aliases by full IP', () => {
const ip = '1.2.3.4';
const rows = [
{ ip, name: 'foo' },
{ ip, name: 'bar' }
];
sinon.stub(mockTx, 'table').withArgs('aliases').returns(mockTx);
sinon.stub(mockTx, 'where').withArgs({ ip }).returns(mockTx);
sinon.stub(mockTx, 'select').returns(mockTx);
sinon.stub(mockTx, 'distinct').withArgs('name').returns(mockTx);
sinon.stub(mockTx, 'orderBy').withArgs('time', 'desc').returns(mockTx);
sinon.stub(mockTx, 'limit').withArgs(5).resolves(rows);
return aliasesDB.getAliasesByIP(ip).then(names => {
assert.deepStrictEqual(names.sort(), ['bar', 'foo']);
});
});
it('retrieves aliases by IPv4 range', () => {
const ip = '1.2.3';
const rows = [
{ ip: ip + '.4', name: 'foo' },
{ ip: ip + '.5', name: 'bar' }
];
sinon.stub(mockTx, 'table').withArgs('aliases').returns(mockTx);
sinon.stub(mockTx, 'where').withArgs('ip', 'LIKE', `${ip}.%`).returns(mockTx);
sinon.stub(mockTx, 'select').returns(mockTx);
sinon.stub(mockTx, 'distinct').withArgs('name').returns(mockTx);
sinon.stub(mockTx, 'orderBy').withArgs('time', 'desc').returns(mockTx);
sinon.stub(mockTx, 'limit').withArgs(5).resolves(rows);
return aliasesDB.getAliasesByIP(ip).then(names => {
assert.deepStrictEqual(names.sort(), ['bar', 'foo']);
});
});
it('retrieves aliases by IPv6 range', () => {
const ip = '1:2:3';
const rows = [
{ ip: ip + '::4', name: 'foo' },
{ ip: ip + '::5', name: 'bar' }
];
sinon.stub(mockTx, 'table').withArgs('aliases').returns(mockTx);
const where = sinon.stub(mockTx, 'where')
.withArgs('ip', 'LIKE', `${ip}:%`).returns(mockTx);
sinon.stub(mockTx, 'select').returns(mockTx);
sinon.stub(mockTx, 'distinct').withArgs('name').returns(mockTx);
sinon.stub(mockTx, 'orderBy').withArgs('time', 'desc').returns(mockTx);
sinon.stub(mockTx, 'limit').withArgs(5).resolves(rows);
return aliasesDB.getAliasesByIP(ip).then(names => {
assert(where.called, 'Expected WHERE LIKE clause');
assert.deepStrictEqual(names.sort(), ['bar', 'foo']);
});
});
});
describe('#getIPsByName', () => {
it('retrieves IPs by name', () => {
const name = 'foo';
const rows = [
{ name, ip: '1.2.3.4' },
{ name, ip: '5.6.7.8' }
];
sinon.stub(mockTx, 'table').withArgs('aliases').returns(mockTx);
sinon.stub(mockTx, 'select').withArgs('ip').returns(mockTx);
sinon.stub(mockTx, 'where').withArgs({ name }).resolves(rows);
return aliasesDB.getIPsByName(name).then(ips => {
assert.deepStrictEqual(ips.sort(), ['1.2.3.4', '5.6.7.8']);
});
});
});
});

84
test/db/globalban.js Normal file
View file

@ -0,0 +1,84 @@
const assert = require('assert');
const sinon = require('sinon');
const TestUtilDB = require('../testutil/db');
const GlobalBanDB = require('../../lib/db/globalban').GlobalBanDB;
describe('GlobalBanDB', () => {
let mockTx, mockDB, globalBanDB;
beforeEach(() => {
mockTx = new TestUtilDB.MockTx();
mockDB = new TestUtilDB.MockDB(mockTx);
globalBanDB = new GlobalBanDB(mockDB);
});
describe('#listGlobalBans', () => {
it('lists bans from the database', () => {
const expected = [
{ ip: '1.2.3.4', reason: 'spam' },
{ ip: '5.6', reason: 'ham' }
];
sinon.stub(mockTx, 'table').withArgs('global_bans').returns(mockTx);
sinon.stub(mockTx, 'select').resolves(expected);
return globalBanDB.listGlobalBans().then(bans => {
assert.deepStrictEqual(bans, expected);
});
});
});
describe('#addGlobalIPBan', () => {
it('adds a new ban', () => {
const input = { ip: '1.2.3.4', reason: 'spam' };
sinon.stub(mockTx, 'table').withArgs('global_bans').returns(mockTx);
const insert = sinon.stub(mockTx, 'insert').withArgs(input).resolves();
return globalBanDB.addGlobalIPBan(input.ip, input.reason).then(() => {
assert(insert.called, 'Expected insert to be called');
});
});
it('updates the ban reason for an existing ban', () => {
const input = { ip: '1.2.3.4', reason: 'spam' };
sinon.stub(mockTx, 'table').withArgs('global_bans').returns(mockTx);
const err = new Error();
err.code = 'ER_DUP_ENTRY';
const insert = sinon.stub(mockTx, 'insert').withArgs(input).rejects(err);
const where = sinon.stub(mockTx, 'where').withArgs({ ip: input.ip }).returns(mockTx);
const update = sinon.stub(mockTx, 'update').withArgs({ reason: input.reason }).resolves();
return globalBanDB.addGlobalIPBan(input.ip, input.reason).then(() => {
assert(insert.called, 'Expected insert to be called');
assert(where.called, 'Expected where({ ip }) to be called');
assert(update.called, 'Expected update({ reason }) to be called');
});
});
it('doesn\'t call update for a non-ER_DUP_ENTRY error', () => {
const input = { ip: '1.2.3.4', reason: 'spam' };
sinon.stub(mockTx, 'table').withArgs('global_bans').returns(mockTx);
const err = new Error('explosions');
const insert = sinon.stub(mockTx, 'insert').withArgs(input).rejects(err);
return globalBanDB.addGlobalIPBan(input.ip, input.reason).then(() => {
assert(false, 'Expected an error');
}).catch(error => {
assert.strictEqual(error, err, 'Expected error to match');
assert(insert.called, 'Expected insert to be called');
});
});
});
describe('#removeGlobalIPBan', () => {
it('removes a global ban', () => {
const ip = '1.2.3.4';
sinon.stub(mockTx, 'table').withArgs('global_bans').returns(mockTx);
const where = sinon.stub(mockTx, 'where').returns(mockTx);
const del = sinon.stub(mockTx, 'del').resolves();
return globalBanDB.removeGlobalIPBan(ip).then(() => {
assert(where.called, 'Expected where({ ip }) to be called');
assert(del.called, 'Expected del to be called');
});
});
});
});