Skip to content

Commit

Permalink
Add XMPP test
Browse files Browse the repository at this point in the history
  • Loading branch information
nioc committed Oct 20, 2019
1 parent 5b968af commit 42e0d1c
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"env": {
"commonjs": true,
"es6": true,
"node": true
"node": true,
"mocha": true
},
"extends": [
"standard"
Expand Down
126 changes: 126 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"mocha": "^6.2.1",
"mock-require": "^3.0.3",
"nodemon": "^1.19.3",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"sinon": "^7.5.0"
}
}
2 changes: 1 addition & 1 deletion test/config.json
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",
Expand Down
126 changes: 126 additions & 0 deletions test/xmpp.js
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()
})
})
})
})

0 comments on commit 42e0d1c

Please sign in to comment.