-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebhook.js
225 lines (207 loc) · 6.62 KB
/
webhook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
'use strict'
/* eslint-disable n/handle-callback-err */
/* eslint-disable prefer-regex-literals */
process.env.NODE_ENV = 'production'
require('chai').should()
const sinon = require('sinon')
const fs = require('fs')
const request = require('request')
describe('Webhook component', () => {
let logger, config, xmppSendStub, xmpp, baseUrl, webhook, logFile, options
before('Setup', () => {
// create default logger
logger = require('./../lib/logger')()
// get configuration
config = require('./../lib/config')(logger, './test/config.json')
// update logger with configuration
logger.updateConfig(config.logger)
// mock xmpp component
xmpp = {
send: null
}
// configure webhook
baseUrl = 'http://localhost:' + config.listener.port
webhook = require('./../lib/webhook')(logger, config, xmpp)
logFile = config.listener.log.path + config.listener.log.filename
})
beforeEach('Reset XMPP stub history and request option', function () {
// mock xmpp component
xmppSendStub = sinon.stub().resolves()
xmpp.send = xmppSendStub
options = {
method: 'POST',
url: baseUrl + config.listener.path + '/',
auth: {
user: 'login1',
pass: '1pass'
}
}
})
after('Close listener', (done) => {
webhook.close()
done()
})
describe('POST without authorization', () => {
it('Should return 401 and be logged', (done) => {
request.post(baseUrl, (error, response, body) => {
response.statusCode.should.equal(401)
fs.readFile(logFile, 'utf8', (err, data) => {
if (err) {
throw err
}
data.should.match(new RegExp('"POST / HTTP/1.1" 401 '))
done()
})
})
})
})
describe('Wrong method (GET)', () => {
it('Should return 405', (done) => {
request.get(baseUrl, { auth: options.auth }, (error, response, body) => {
response.statusCode.should.equal(405)
done()
})
})
})
describe('POST unknown webhook', () => {
it('Should return 404 and be logged', (done) => {
options.url += 'unknown'
request(options, (error, response, body) => {
response.statusCode.should.equal(404)
fs.readFile(logFile, 'utf8', (err, data) => {
if (err) {
throw err
}
data.should.match(new RegExp('"POST ' + config.listener.path + '/unknown HTTP/1.1" 404 '))
done()
})
})
})
})
describe('POST dummy webhook', () => {
it('Should return 204', (done) => {
options.url += 'dummy'
request(options, (error, response, body) => {
response.statusCode.should.equal(204)
done()
})
})
})
describe('POST missing destination webhook', () => {
it('Should return 400 and error detail', (done) => {
options.url += 'w1'
request(options, (error, response, body) => {
response.statusCode.should.equal(400)
response.body.should.equal('Destination not found')
done()
})
})
})
describe('POST missing message webhook', () => {
it('Should return 400 and error detail', (done) => {
options.json = {
destination: 'destination'
}
options.url += 'w1'
request(options, (error, response, body) => {
response.statusCode.should.equal(400)
response.body.should.equal('Message not found')
done()
})
})
})
describe('POST valid webhook (send message)', () => {
it('Should return 200 and send XMPP message', (done) => {
options.json = {
destination: 'destination',
message: 'This is a message'
}
options.url += 'w1'
request(options, (error, response, body) => {
response.statusCode.should.equal(200)
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 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 = {
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(200)
sinon.assert.calledOnce(xmppSendStub)
const args = xmppSendStub.args[0]
args.should.have.length(3)
args[0].should.equal('[email protected]')
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()
})
})
})
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('[email protected]')
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()
})
})
})
})