Return XMPP error to incoming webhook

This commit is contained in:
nioc 2019-11-23 20:30:13 +01:00
parent 18f8df74c0
commit 9c5dd424bb
3 changed files with 70 additions and 6 deletions

View file

@ -87,7 +87,13 @@ module.exports = (logger, config, xmpp) => {
// send message
logger.trace(`Send to ${destination} (group:${type}) following message :\r\n${message}`)
xmpp.send(destination, message, type)
return res.status(200).send({ 'status': 'ok', destination })
.then(() => {
return res.status(200).send({ 'status': 'ok', destination })
})
.catch(() => {
return res.status(500).send('Could not send message')
})
break
case 'send_xmpp_template':
@ -101,7 +107,13 @@ module.exports = (logger, config, xmpp) => {
// send message
logger.trace(`Send to ${webhook.args.destination} (group:${webhook.args.type}) following message :\r\n${msg}`)
xmpp.send(webhook.args.destination, msg, webhook.args.type)
return res.status(200).send('ok')
.then(() => {
return res.status(200).send('ok')
})
.catch(() => {
return res.status(500).send('Could not send message')
})
break
default:
return res.status(204).send()

View file

@ -29,6 +29,7 @@ module.exports = (logger, config) => {
message)
)
await xmppClient.send(stanza)
logger.debug(`${type} message successfully sent to ${to}`)
}
// declare close function

View file

@ -21,11 +21,9 @@ describe('Webhook component', () => {
logger.updateConfig(config.logger)
// mock xmpp component
xmppSendStub = sinon.stub()
xmpp = {
send: xmppSendStub
send: null
}
// configure webhook
baseUrl = 'http://localhost:' + config.listener.port
webhook = require('./../lib/webhook')(logger, config, xmpp)
@ -33,7 +31,9 @@ describe('Webhook component', () => {
})
beforeEach('Reset XMPP stub history and request option', function () {
xmppSendStub.resetHistory()
// mock xmpp component
xmppSendStub = sinon.stub().resolves()
xmpp.send = xmppSendStub
options = {
method: 'POST',
url: baseUrl + config.listener.path + '/',
@ -144,6 +144,28 @@ describe('Webhook component', () => {
})
})
describe('POST valid webhook (send message) with XMPP error', () => {
it('Should return 200 and send XMPP message', (done) => {
xmppSendStub = sinon.stub().rejects()
xmpp.send = xmppSendStub
options.json = {
destination: 'destination',
message: 'This is a message'
}
options.url += 'w1'
request(options, (error, response, body) => {
response.statusCode.should.equal(500)
sinon.assert.calledOnce(xmppSendStub)
const args = xmppSendStub.args[0]
args.should.have.length(3)
args[0].should.equal(options.json.destination)
args[1].should.equal(options.json.message)
args[2].should.equal('chat')
done()
})
})
})
describe('POST valid webhook (send template)', () => {
it('Should return 200 and send XMPP message', (done) => {
options.json = {
@ -170,4 +192,33 @@ describe('Webhook component', () => {
})
})
})
describe('POST valid webhook (send template) with XMPP error', () => {
it('Should return 200 and send XMPP message', (done) => {
xmppSendStub = sinon.stub().rejects()
xmpp.send = xmppSendStub
options.json = {
title: 'This is a title',
message: 'This is a message',
evalMatches: [
{
metric: 'metric',
value: 'value'
}
],
imageUrl: 'https://domain.ltd:port/path/image'
}
options.url += 'grafana'
request(options, (error, response, body) => {
response.statusCode.should.equal(500)
sinon.assert.calledOnce(xmppSendStub)
const args = xmppSendStub.args[0]
args.should.have.length(3)
args[0].should.equal('grafana@conference.domain-xmpp.ltd')
args[1].should.equal('This is a title\r\nThis is a message\r\nmetric: value\r\nhttps://domain.ltd:port/path/image')
args[2].should.equal('groupchat')
done()
})
})
})
})