Skip to content

Commit 5af2ba6

Browse files
committed
Add the first test case!
Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 0ba4abb commit 5af2ba6

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

__tests__/index.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const index = require('../GitGitGadget/index')
2+
3+
process.env['GITHUB_WEBHOOK_SECRET'] = 'for-testing'
4+
5+
test('reject requests other than webhook payloads', async () => {
6+
const context = {
7+
log: jest.fn(),
8+
req: {
9+
method: 'GET',
10+
headers: {
11+
'content-type': 'text/plain'
12+
}
13+
},
14+
done: jest.fn()
15+
}
16+
17+
const expectInvalidWebhook = async (message) => {
18+
context.log.mockClear()
19+
context.done.mockClear()
20+
expect(await index(context, context.req)).toBeUndefined()
21+
expect(context.log).toHaveBeenCalledTimes(1)
22+
expect(context.log.mock.calls[0][0]).toEqual(`Caught Error: ${message}`)
23+
expect(context.res).toEqual({
24+
body: `Not a valid GitHub webhook: Error: ${message}`,
25+
status: 403
26+
})
27+
expect(context.done).toHaveBeenCalledTimes(1)
28+
}
29+
30+
await expectInvalidWebhook('Unexpected content type: text/plain')
31+
32+
context.req.method = 'POST'
33+
context.req.headers = {
34+
'content-type': 'text/plain'
35+
}
36+
await expectInvalidWebhook('Unexpected content type: text/plain')
37+
38+
context.req.headers['content-type'] = 'application/json'
39+
await expectInvalidWebhook('Missing X-Hub-Signature')
40+
41+
context.req.headers['x-hub-signature'] = 'invalid'
42+
await expectInvalidWebhook('Unexpected X-Hub-Signature format: invalid')
43+
44+
context.req.headers['x-hub-signature'] = 'sha1=incorrect'
45+
context.req.rawBody = '# empty'
46+
await expectInvalidWebhook('Incorrect X-Hub-Signature')
47+
})

0 commit comments

Comments
 (0)