From 9c5dd424bbc33b54a220c516c62ffff65332a8df Mon Sep 17 00:00:00 2001 From: nioc Date: Sat, 23 Nov 2019 20:30:13 +0100 Subject: [PATCH] Return XMPP error to incoming webhook --- lib/webhook/index.js | 16 ++++++++++-- lib/xmpp/index.js | 1 + test/webhook.js | 59 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/lib/webhook/index.js b/lib/webhook/index.js index dcf57c9..4f000c1 100644 --- a/lib/webhook/index.js +++ b/lib/webhook/index.js @@ -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() diff --git a/lib/xmpp/index.js b/lib/xmpp/index.js index 8bdef27..62971f6 100644 --- a/lib/xmpp/index.js +++ b/lib/xmpp/index.js @@ -29,6 +29,7 @@ module.exports = (logger, config) => { message) ) await xmppClient.send(stanza) + logger.debug(`${type} message successfully sent to ${to}`) } // declare close function diff --git a/test/webhook.js b/test/webhook.js index 4e3bd2f..033bfed 100644 --- a/test/webhook.js +++ b/test/webhook.js @@ -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() + }) + }) + }) })