-
Notifications
You must be signed in to change notification settings - Fork 124
/
test.js
370 lines (291 loc) · 9.61 KB
/
test.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const test = require('tape')
const crypto = require('crypto')
const handler = require('./')
const through2 = require('through2')
const series = require('run-series')
function signBlob (key, blob) {
return `sha1=${crypto.createHmac('sha1', key).update(blob).digest('hex')}`
}
function mkReq (url, method) {
const req = through2()
req.method = method || 'POST'
req.url = url
req.headers = {
'x-hub-signature': 'bogus',
'x-github-event': 'bogus',
'x-github-delivery': 'bogus'
}
return req
}
function mkRes () {
const res = {
writeHead: function (statusCode, headers) {
res.$statusCode = statusCode
res.$headers = headers
},
end: function (content) {
res.$end = content
}
}
return res
}
test('handler without full options throws', (t) => {
t.plan(4)
t.equal(typeof handler, 'function', 'handler exports a function')
t.throws(handler, /must provide an options object/, 'throws if no options')
t.throws(handler.bind(null, {}), /must provide a 'path' option/, 'throws if no path option')
t.throws(handler.bind(null, { path: '/' }), /must provide a 'secret' option/, 'throws if no secret option')
})
test('handler without full options throws in array', (t) => {
t.plan(2)
t.throws(handler.bind(null, [{}]), /must provide a 'path' option/, 'throws if no path option')
t.throws(handler.bind(null, [{ path: '/' }]), /must provide a 'secret' option/, 'throws if no secret option')
})
test('handler ignores invalid urls', (t) => {
const options = { path: '/some/url', secret: 'bogus' }
const h = handler(options)
t.plan(6)
h(mkReq('/'), mkRes(), (err) => {
t.error(err)
t.ok(true, 'request was ignored')
})
// near match
h(mkReq('/some/url/'), mkRes(), (err) => {
t.error(err)
t.ok(true, 'request was ignored')
})
// partial match
h(mkReq('/some'), mkRes(), (err) => {
t.error(err)
t.ok(true, 'request was ignored')
})
})
test('handler ingores non-POST requests', (t) => {
const options = { path: '/some/url', secret: 'bogus' }
const h = handler(options)
t.plan(4)
h(mkReq('/some/url', 'GET'), mkRes(), (err) => {
t.error(err)
t.ok(true, 'request was ignored')
})
h(mkReq('/some/url?test=param', 'GET'), mkRes(), (err) => {
t.error(err)
t.ok(true, 'request was ignored')
})
})
test('handler accepts valid urls', (t) => {
const options = { path: '/some/url', secret: 'bogus' }
const h = handler(options)
t.plan(1)
h(mkReq('/some/url'), mkRes(), (err) => {
t.error(err)
t.fail(false, 'should not call')
})
setTimeout(t.ok.bind(t, true, 'done'))
})
test('handler accepts valid urls in Array', (t) => {
const options = [{ path: '/some/url', secret: 'bogus' }, { path: '/someOther/url', secret: 'bogus' }]
const h = handler(options)
t.plan(1)
h(mkReq('/some/url'), mkRes(), (err) => {
t.error(err)
t.fail(false, 'should not call')
})
h(mkReq('/someOther/url'), mkRes(), (err) => {
t.error(err)
t.fail(false, 'should not call')
})
setTimeout(t.ok.bind(t, true, 'done'))
})
test('handler can reject events', (t) => {
const acceptableEvents = {
undefined: undefined,
'a string equal to the event': 'bogus',
'a string equal to *': '*',
'an array containing the event': ['bogus'],
'an array containing *': ['not-bogus', '*']
}
const unacceptableEvents = {
'a string not equal to the event or *': 'not-bogus',
'an array not containing the event or *': ['not-bogus']
}
const acceptable = Object.keys(acceptableEvents)
const unacceptable = Object.keys(unacceptableEvents)
const acceptableTests = acceptable.map((events) => {
return acceptableReq.bind(null, events)
})
const unacceptableTests = unacceptable.map((events) => {
return unacceptableReq.bind(null, events)
})
t.plan(acceptable.length + unacceptable.length)
series(acceptableTests.concat(unacceptableTests))
function acceptableReq (events, callback) {
const h = handler({
path: '/some/url',
secret: 'bogus',
events: acceptableEvents[events]
})
h(mkReq('/some/url'), mkRes(), (err) => {
t.error(err)
t.fail(false, 'should not call')
})
setTimeout(() => {
t.ok(true, 'accepted because options.events was ' + events)
callback()
})
}
function unacceptableReq (events, callback) {
const h = handler({
path: '/some/url',
secret: 'bogus',
events: unacceptableEvents[events]
})
h.on('error', () => {})
h(mkReq('/some/url'), mkRes(), (err) => {
t.ok(err, 'rejected because options.events was ' + events)
callback()
})
}
})
// because we don't inherit in a traditional way
test('handler is an EventEmitter', (t) => {
t.plan(5)
const h = handler({ path: '/', secret: 'bogus' })
t.equal(typeof h.on, 'function', 'has h.on()')
t.equal(typeof h.emit, 'function', 'has h.emit()')
t.equal(typeof h.removeListener, 'function', 'has h.removeListener()')
h.on('ping', (pong) => {
t.equal(pong, 'pong', 'got event')
})
h.emit('ping', 'pong')
t.throws(h.emit.bind(h, 'error', new Error('threw an error')), /threw an error/, 'acts like an EE')
})
test('handler accepts a signed blob', (t) => {
t.plan(4)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const h = handler({ path: '/', secret: 'bogus' })
const req = mkReq('/')
const res = mkRes()
req.headers['x-hub-signature'] = signBlob('bogus', json)
req.headers['x-github-event'] = 'push'
h.on('push', (event) => {
t.deepEqual(event, { event: 'push', id: 'bogus', payload: obj, url: '/', host: undefined, protocol: undefined, path: '/' })
t.equal(res.$statusCode, 200, 'correct status code')
t.deepEqual(res.$headers, { 'content-type': 'application/json' })
t.equal(res.$end, '{"ok":true}', 'got correct content')
})
h(req, res, (err) => {
t.error(err)
t.fail(true, 'should not get here!')
})
process.nextTick(() => {
req.end(json)
})
})
test('handler accepts multi blob in Array', (t) => {
t.plan(4)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const h = handler([{ path: '/', secret: 'bogus' }, { path: '/some/url', secret: 'bogus' }])
const req = mkReq('/some/url')
const res = mkRes()
req.headers['x-hub-signature'] = signBlob('bogus', json)
req.headers['x-github-event'] = 'push'
h.on('push', (event) => {
t.deepEqual(event, { event: 'push', id: 'bogus', payload: obj, url: '/some/url', host: undefined, protocol: undefined, path: '/some/url' })
t.equal(res.$statusCode, 200, 'correct status code')
t.deepEqual(res.$headers, { 'content-type': 'application/json' })
t.equal(res.$end, '{"ok":true}', 'got correct content')
})
h(req, res, (err) => {
t.error(err)
t.fail(true, 'should not get here!')
})
process.nextTick(() => {
req.end(json)
})
})
test('handler accepts a signed blob with alt event', (t) => {
t.plan(4)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const h = handler({ path: '/', secret: 'bogus' })
const req = mkReq('/')
const res = mkRes()
req.headers['x-hub-signature'] = signBlob('bogus', json)
req.headers['x-github-event'] = 'issue'
h.on('push', (event) => {
t.fail(true, 'should not get here!')
})
h.on('issue', (event) => {
t.deepEqual(event, { event: 'issue', id: 'bogus', payload: obj, url: '/', host: undefined, protocol: undefined, path: '/' })
t.equal(res.$statusCode, 200, 'correct status code')
t.deepEqual(res.$headers, { 'content-type': 'application/json' })
t.equal(res.$end, '{"ok":true}', 'got correct content')
})
h(req, res, (err) => {
t.error(err)
t.fail(true, 'should not get here!')
})
process.nextTick(() => {
req.end(json)
})
})
test('handler rejects a badly signed blob', (t) => {
t.plan(6)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const h = handler({ path: '/', secret: 'bogus' })
const req = mkReq('/')
const res = mkRes()
req.headers['x-hub-signature'] = signBlob('bogus', json)
// break signage by a tiny bit
req.headers['x-hub-signature'] = '0' + req.headers['x-hub-signature'].substring(1)
h.on('error', (err, _req) => {
t.ok(err, 'got an error')
t.strictEqual(_req, req, 'was given original request object')
t.equal(res.$statusCode, 400, 'correct status code')
t.deepEqual(res.$headers, { 'content-type': 'application/json' })
t.equal(res.$end, '{"error":"X-Hub-Signature does not match blob signature"}', 'got correct content')
})
h.on('push', (event) => {
t.fail(true, 'should not get here!')
})
h(req, res, (err) => {
t.ok(err, 'got error on callback')
})
process.nextTick(() => {
req.end(json)
})
})
test('handler responds on a bl error', (t) => {
t.plan(4)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const h = handler({ path: '/', secret: 'bogus' })
const req = mkReq('/')
const res = mkRes()
req.headers['x-hub-signature'] = signBlob('bogus', json)
req.headers['x-github-event'] = 'issue'
h.on('push', (event) => {
t.fail(true, 'should not get here!')
})
h.on('issue', (event) => {
t.fail(true, 'should never get here!')
})
h.on('error', (err) => {
t.ok(err, 'got an error')
t.equal(res.$statusCode, 400, 'correct status code')
})
h(req, res, (err) => {
t.ok(err)
})
res.end = () => {
t.equal(res.$statusCode, 400, 'correct status code')
}
req.write('{')
process.nextTick(() => {
req.emit('error', new Error('simulated explosion'))
})
})