-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
258 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"logger": { | ||
"level": "debug", | ||
"level": "error", | ||
"file": { | ||
"active": true, | ||
"pattern": "%d %p %m", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
process.env.NODE_ENV = 'production' | ||
|
||
const should = require('chai').should() | ||
const sinon = require('sinon') | ||
const EventEmitter = require('events').EventEmitter | ||
const mock = require('mock-require') | ||
|
||
// create default logger | ||
let logger = require('./../lib/logger')() | ||
|
||
// get configuration | ||
let config = require('./../lib/config')(logger, './test/config.json') | ||
|
||
// update logger with configuration | ||
logger.updateConfig(config.logger) | ||
|
||
// mock simple-xmpp module | ||
const simpleXmppEvents = new EventEmitter() | ||
let xmppJoinStub = sinon.stub() | ||
mock('simple-xmpp', { | ||
connect: () => {}, | ||
join: xmppJoinStub, | ||
on: (eventName, callback) => { | ||
simpleXmppEvents.on(eventName, callback) | ||
}, | ||
getRoster: () => {} | ||
}) | ||
|
||
// mock outgoing | ||
let outgoingStub = sinon.stub() | ||
mock('./../lib/outgoing', outgoingStub) | ||
|
||
describe('XMPP component', () => { | ||
beforeEach('Reset outgoing stub history', function () { | ||
outgoingStub.resetHistory() | ||
}) | ||
|
||
describe('Connect to XMPP server', () => { | ||
it('Should connect to XMPP server and join rooms when application start', (done) => { | ||
require('./../lib/xmpp')(logger, config) | ||
simpleXmppEvents.emit('online', { jid: { user: 'bot' } }) | ||
sinon.assert.called(xmppJoinStub) | ||
let roomsLength = config.xmpp.rooms.length | ||
sinon.assert.callCount(xmppJoinStub, roomsLength) | ||
for (let index = 0; index < roomsLength; index++) { | ||
const args = xmppJoinStub.args[index] | ||
args.should.have.length(2) | ||
args[0].should.equal(config.xmpp.rooms[index].id + '/' + 'bot') | ||
if (config.xmpp.rooms[index].password === null) { | ||
should.equal(args[1], null) | ||
} else { | ||
args[1].should.equal(config.xmpp.rooms[index].password) | ||
} | ||
} | ||
done() | ||
}) | ||
}) | ||
|
||
describe('Bot receive a message from someone', () => { | ||
it('Should trigger outgoing webhook with valid arguments', (done) => { | ||
simpleXmppEvents.emit('chat', 'someone', 'This is the message text') | ||
sinon.assert.calledOnce(outgoingStub) | ||
const args = outgoingStub.args[0] | ||
args.should.have.length(8) | ||
args[3].should.equal('someone') | ||
args[4].should.equal('someone') | ||
args[5].should.equal('This is the message text') | ||
args[6].should.equal(false) | ||
args[7].should.equal('w1') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('Bot receive a message from himself in a room', () => { | ||
it('Should not trigger outgoing webhook', (done) => { | ||
simpleXmppEvents.emit('groupchat', '[email protected]', 'bot', 'This is the message text', null) | ||
sinon.assert.notCalled(outgoingStub) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('Bot receive a message in an unknown room', () => { | ||
it('Should not trigger outgoing webhook', (done) => { | ||
simpleXmppEvents.emit('groupchat', '[email protected]', 'someone', 'This is the message text', null) | ||
sinon.assert.notCalled(outgoingStub) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('Bot receive an old message in a room', () => { | ||
it('Should not trigger outgoing webhook', (done) => { | ||
simpleXmppEvents.emit('groupchat', '[email protected]', 'someone', 'This is the message text', 'stamp') | ||
sinon.assert.notCalled(outgoingStub) | ||
done() | ||
}) | ||
}) | ||
|
||
describe('Bot receive a message in a room', () => { | ||
it('Should trigger outgoing webhook with valid arguments', (done) => { | ||
simpleXmppEvents.emit('groupchat', '[email protected]', 'someone', 'This is the message text', null) | ||
sinon.assert.calledOnce(outgoingStub) | ||
const args = outgoingStub.args[0] | ||
args.should.have.length(8) | ||
args[3].should.equal('someone') | ||
args[4].should.equal('[email protected]') | ||
args[5].should.equal('This is the message text') | ||
args[6].should.equal(true) | ||
args[7].should.equal('w1') | ||
done() | ||
}) | ||
}) | ||
|
||
describe('XMPP server send an error', () => { | ||
it('Should log error only', (done) => { | ||
let error = 'This the error text' | ||
simpleXmppEvents.emit('error', error) | ||
require('fs').readFile(config.logger.file.path + config.logger.file.filename, 'utf8', (err, data) => { | ||
if (err) { | ||
throw err | ||
} | ||
data.should.match(new RegExp(error + '\n$')) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |