Skip to content

Commit 5d3699c

Browse files
committed
tests: verify that /test is handled correctly
Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 568a2c7 commit 5d3699c

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

__tests__/index.test.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
const index = require('../GitGitGadget/index')
2+
const crypto = require('crypto')
3+
const stream = require('stream')
4+
const https = require('https')
25

36
process.env['GITHUB_WEBHOOK_SECRET'] = 'for-testing'
7+
process.env['GITGITGADGET_TRIGGER_TOKEN'] = 'token-for-testing'
48

59
test('reject requests other than webhook payloads', async () => {
610
const context = {
@@ -45,3 +49,94 @@ test('reject requests other than webhook payloads', async () => {
4549
context.req.rawBody = '# empty'
4650
await expectInvalidWebhook('Incorrect X-Hub-Signature')
4751
})
52+
53+
jest.mock('https')
54+
55+
const mockRequest = {
56+
write: jest.fn(),
57+
end: jest.fn()
58+
}
59+
https.request = jest.fn().mockImplementation((options, cb) => {
60+
const s = new stream()
61+
s.setEncoding = jest.fn()
62+
cb(s)
63+
s.emit('data', '{}')
64+
s.emit('end')
65+
return mockRequest
66+
})
67+
68+
const makeContext = (body, headers) => {
69+
const rawBody = JSON.stringify(body)
70+
const sha256 = crypto.createHmac('sha256', process.env['GITHUB_WEBHOOK_SECRET']).update(rawBody).digest('hex')
71+
return {
72+
log: jest.fn(),
73+
req: {
74+
body,
75+
headers: {
76+
'content-type': 'application/json',
77+
'x-hub-signature-256': `sha256=${sha256}`,
78+
...headers || {}
79+
},
80+
method: 'POST',
81+
rawBody
82+
},
83+
done: jest.fn()
84+
}
85+
}
86+
87+
const testIssueComment = (comment, fn) => {
88+
const repoOwner = 'gitgitgadget'
89+
const number = 0x70756c6c
90+
const context = makeContext({
91+
action: 'created',
92+
comment: {
93+
body: comment,
94+
html_url: `https://github.com/${repoOwner}/git/pull/${number}`,
95+
id: 0x636f6d6d656e74,
96+
user: {
97+
login: 'alice wonderland'
98+
}
99+
},
100+
installation: {
101+
id: 123
102+
},
103+
issue: {
104+
number
105+
},
106+
repository: {
107+
name: 'git',
108+
owner: {
109+
login: repoOwner
110+
}
111+
}
112+
}, {
113+
'x-github-event': 'issue_comment'
114+
})
115+
116+
test(`test ${comment}`, async () => {
117+
try {
118+
expect(await index(context, context.req)).toBeUndefined()
119+
await fn(context)
120+
expect(context.done).toHaveBeenCalledTimes(1)
121+
} catch (e) {
122+
context.log.mock.calls.forEach(e => console.log(e[0]))
123+
throw e;
124+
}
125+
})
126+
}
127+
128+
testIssueComment('/test', async (context) => {
129+
expect(context.done).toHaveBeenCalledTimes(1)
130+
expect(context.res).toEqual({
131+
body: 'Okay!'
132+
})
133+
expect(mockRequest.write).toHaveBeenCalledTimes(1)
134+
expect(JSON.parse(mockRequest.write.mock.calls[0][0])).toEqual({
135+
definition: {
136+
id: 3
137+
},
138+
sourceBranch: 'refs/pull/1886743660/head',
139+
parameters: '{"pr.comment.id":27988538471837300}'
140+
})
141+
expect(mockRequest.end).toHaveBeenCalledTimes(1)
142+
})

0 commit comments

Comments
 (0)