2017-03-16 06:44:03 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const KickbanModule = require('../../lib/channel/kickban');
|
2017-06-06 05:45:14 +00:00
|
|
|
const database = require('../../lib/database');
|
2017-03-16 06:44:03 +00:00
|
|
|
const dbChannels = require('../../lib/database/channels');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const ChannelModule = require('../../lib/channel/module');
|
|
|
|
const Flags = require('../../lib/flags');
|
2017-06-06 05:45:14 +00:00
|
|
|
const testDB = require('../testutil/db').testDB;
|
2017-03-16 06:44:03 +00:00
|
|
|
|
|
|
|
function randomString(length) {
|
|
|
|
const chars = 'abcdefgihkmnpqrstuvwxyz0123456789';
|
|
|
|
let str = '';
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
str += chars[Math.floor(Math.random() * chars.length)];
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-06-06 05:45:14 +00:00
|
|
|
database.init(testDB);
|
2017-03-16 06:44:03 +00:00
|
|
|
|
|
|
|
describe('onPreUserJoin Ban Check', () => {
|
|
|
|
const channelName = `test_${randomString(20)}`;
|
|
|
|
const bannedIP = '1.1.1.1';
|
|
|
|
const bannedName = 'troll';
|
|
|
|
const mockChannel = {
|
|
|
|
name: channelName,
|
|
|
|
modules: {},
|
|
|
|
is(flag) {
|
|
|
|
return flag === Flags.C_REGISTERED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const module = new KickbanModule(mockChannel);
|
|
|
|
before(done => {
|
|
|
|
dbChannels.ban(channelName, bannedIP, bannedName, '', '', () => {
|
|
|
|
dbChannels.ban(channelName, bannedIP, '', '', '', () => {
|
|
|
|
dbChannels.ban(channelName, '*', bannedName, '', '', () => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
after(done => {
|
|
|
|
dbChannels.deleteBans(channelName, null, () => {
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles a banned IP with a different name', done => {
|
|
|
|
const user = {
|
|
|
|
getName() {
|
|
|
|
return 'anotherTroll';
|
|
|
|
},
|
|
|
|
|
2018-06-04 04:55:41 +00:00
|
|
|
realip: bannedIP,
|
|
|
|
|
|
|
|
kick() {
|
|
|
|
}
|
2017-03-16 06:44:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.onUserPreJoin(user, null, (error, res) => {
|
|
|
|
assert.equal(error, null, `Unexpected error: ${error}`);
|
|
|
|
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles a banned name with a different IP', done => {
|
|
|
|
const user = {
|
|
|
|
getName() {
|
|
|
|
return 'troll';
|
|
|
|
},
|
|
|
|
|
2018-06-04 04:55:41 +00:00
|
|
|
realip: '5.5.5.5',
|
|
|
|
|
|
|
|
kick() {
|
|
|
|
}
|
2017-03-16 06:44:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.onUserPreJoin(user, null, (error, res) => {
|
|
|
|
assert.equal(error, null, `Unexpected error: ${error}`);
|
|
|
|
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles a banned IP with a blank name', done => {
|
|
|
|
const user = {
|
|
|
|
getName() {
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
|
2018-06-04 04:55:41 +00:00
|
|
|
realip: bannedIP,
|
|
|
|
|
|
|
|
kick() {
|
|
|
|
}
|
2017-03-16 06:44:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.onUserPreJoin(user, null, (error, res) => {
|
|
|
|
assert.equal(error, null, `Unexpected error: ${error}`);
|
|
|
|
assert.equal(res, ChannelModule.DENY, 'Expected user to be banned');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles a non-banned IP with a blank name', done => {
|
|
|
|
const user = {
|
|
|
|
getName() {
|
|
|
|
return '';
|
|
|
|
},
|
|
|
|
|
|
|
|
realip: '5.5.5.5'
|
|
|
|
};
|
|
|
|
|
|
|
|
module.onUserPreJoin(user, null, (error, res) => {
|
|
|
|
assert.equal(error, null, `Unexpected error: ${error}`);
|
|
|
|
assert.equal(res, ChannelModule.PASSTHROUGH, 'Expected user not to be banned');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles a non-banned IP with a non-banned name', done => {
|
|
|
|
const user = {
|
|
|
|
getName() {
|
|
|
|
return 'some_user';
|
|
|
|
},
|
|
|
|
|
|
|
|
realip: '5.5.5.5'
|
|
|
|
};
|
|
|
|
|
|
|
|
module.onUserPreJoin(user, null, (error, res) => {
|
|
|
|
assert.equal(error, null, `Unexpected error: ${error}`);
|
|
|
|
assert.equal(res, ChannelModule.PASSTHROUGH, 'Expected user not to be banned');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2017-06-06 05:45:14 +00:00
|
|
|
});
|