-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.js
More file actions
308 lines (286 loc) · 9.52 KB
/
index.test.js
File metadata and controls
308 lines (286 loc) · 9.52 KB
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
const mockTriggerWorkflowDispatch = jest.fn(async (_context, _token, _owner, _repo, workflow_id, ref, inputs) => {
return { html_url: `<the URL to the workflow ${workflow_id} run on ${ref} with inputs ${JSON.stringify(inputs)}>` }
})
jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
triggerWorkflowDispatch: mockTriggerWorkflowDispatch,
listWorkflowRuns: jest.fn(async (_context, _token, _owner, _repo, workflow_id, branch, status) => {
if (workflow_id === 'update-prs.yml' && branch === 'main' && status === 'queued') {
// pretend that `update-prs` is clogged up, for whatever reason
return [
{ id: 1, head_branch: 'main', status: 'queued', html_url: '<the URL to the workflow run>' },
{ id: 2, head_branch: 'main', status: 'queued', html_url: '<another URL to the workflow run>' }
]
}
return []
})
}))
const index = require('../GitGitGadget/index')
const crypto = require('crypto')
const stream = require('stream')
const https = require('https')
afterEach(() => {
jest.clearAllMocks();
})
process.env['GITHUB_WEBHOOK_SECRET'] = 'for-testing'
process.env['GITGITGADGET_TRIGGER_TOKEN'] = 'token-for-testing'
test('reject requests other than webhook payloads', async () => {
const context = {
log: jest.fn(),
req: {
method: 'GET',
headers: {
'content-type': 'text/plain'
}
},
done: jest.fn()
}
const expectInvalidWebhook = async (message) => {
context.log.mockClear()
context.done.mockClear()
expect(await index(context, context.req)).toBeUndefined()
expect(context.log).toHaveBeenCalledTimes(1)
expect(context.log.mock.calls[0][0]).toEqual(`Caught Error: ${message}`)
expect(context.res).toEqual({
body: `Not a valid GitHub webhook: Error: ${message}`,
status: 403
})
expect(context.done).not.toHaveBeenCalled()
}
await expectInvalidWebhook('Unexpected content type: text/plain')
context.req.method = 'POST'
context.req.headers = {
'content-type': 'text/plain'
}
await expectInvalidWebhook('Unexpected content type: text/plain')
context.req.headers['content-type'] = 'application/json'
await expectInvalidWebhook('Missing X-Hub-Signature')
context.req.headers['x-hub-signature-256'] = 'invalid'
await expectInvalidWebhook('Unexpected X-Hub-Signature format: invalid')
context.req.headers['x-hub-signature-256'] = 'sha256=incorrect'
context.req.rawBody = '# empty'
await expectInvalidWebhook('Incorrect X-Hub-Signature')
})
jest.mock('https')
const mockRequest = {
write: jest.fn(),
end: jest.fn()
}
https.request = jest.fn().mockImplementation((options, cb) => {
const s = new stream()
s.setEncoding = jest.fn()
cb(s)
s.emit('data', '{}')
s.emit('end')
return mockRequest
})
const makeContext = (body, headers) => {
const rawBody = JSON.stringify(body)
const sha256 = crypto.createHmac('sha256', process.env['GITHUB_WEBHOOK_SECRET']).update(rawBody).digest('hex')
return {
log: jest.fn(),
req: {
body,
headers: {
'content-type': 'application/json',
'x-hub-signature-256': `sha256=${sha256}`,
...headers || {}
},
method: 'POST',
rawBody
},
done: jest.fn()
}
}
const testIssueComment = (comment, repoOwner, fn) => {
if (!fn) {
fn = repoOwner
repoOwner = undefined
}
repoOwner ||= 'gitgitgadget'
const number = 0x70756c6c
const context = makeContext({
action: 'created',
comment: {
body: comment,
html_url: `https://github.com/${repoOwner}/git/pull/${number}`,
id: 0x636f6d6d656e74,
user: {
login: 'alice wonderland'
}
},
installation: {
id: 123
},
issue: {
number
},
repository: {
name: 'git',
owner: {
login: repoOwner
}
}
}, {
'x-github-event': 'issue_comment'
})
test(`test ${comment}`, async () => {
try {
expect(await index(context, context.req)).toBeUndefined()
await fn(context)
expect(context.done).not.toHaveBeenCalled()
} catch (e) {
context.log.mock.calls.forEach(e => console.log(e[0]))
throw e;
}
})
}
testIssueComment('/test', async (context) => {
expect(context.done).not.toHaveBeenCalled()
expect(context.res).toEqual({
body: 'Okay, triggered <the URL to the workflow handle-pr-comment.yml run on main with inputs {"pr-comment-url":"https://github.com/gitgitgadget/git/pull/1886743660"}>!'
})
expect(mockRequest.write).not.toHaveBeenCalled()
expect(mockRequest.end).not.toHaveBeenCalled()
})
testIssueComment('/verify-repository', 'nope', (context) => {
expect(context.done).not.toHaveBeenCalled()
expect(context.res).toEqual({
body: 'Refusing to work on a repository other than gitgitgadget/git or git/git',
'status': 403,
})
expect(mockRequest.write).not.toHaveBeenCalled()
expect(mockRequest.end).not.toHaveBeenCalled()
})
const testWebhookPayload = (testLabel, gitHubEvent, payload, fn) => {
const context = makeContext(payload, {
'x-github-event': gitHubEvent
})
test(testLabel, async () => {
try {
expect(await index(context, context.req)).toBeUndefined()
await fn(context)
expect(context.done).not.toHaveBeenCalled()
} catch (e) {
context.log.mock.calls.forEach(e => console.log(e[0]))
throw e;
}
})
}
testWebhookPayload('react to `next` being pushed to git/git', 'push', {
ref: 'refs/heads/next',
repository: {
full_name: 'git/git',
owner: {
login: 'git'
}
}
}, (context) => {
expect(context.res).toEqual({
body: 'push(refs/heads/next): triggered <the URL to the workflow sync-ref.yml run on main with inputs {"ref":"refs/heads/next"}>'
})
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'sync-ref.yml',
'main', {
ref: 'refs/heads/next'
}
])
})
testWebhookPayload('react to `seen` being pushed to git/git', 'push', {
ref: 'refs/heads/seen',
repository: {
full_name: 'git/git',
owner: {
login: 'git'
}
}
}, (context) => {
expect(context.res).toEqual({
body: [
'push(refs/heads/seen):',
'triggered <the URL to the workflow sync-ref.yml run on main with inputs {"ref":"refs/heads/seen"}>',
'and <the URL to the workflow update-mail-to-commit-notes.yml run on main with inputs undefined>'
].join(' ')
})
// we expect `update-prs` _not_ to be triggered here because we pretend that it is already queued
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(2)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'sync-ref.yml',
'main', {
ref: 'refs/heads/seen'
}
])
expect(mockTriggerWorkflowDispatch.mock.calls[1]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'update-mail-to-commit-notes.yml',
'main',
undefined
])
})
testWebhookPayload('react to `lore-1` being pushed to https://github.com/gitgitgadget/git-mailing-list-mirror', 'push', {
ref: 'refs/heads/lore-1',
repository: {
full_name: 'gitgitgadget/git-mailing-list-mirror',
owner: {
login: 'gitgitgadget'
}
}
}, (context) => {
expect(context.res).toEqual({
body: [
'push(refs/heads/lore-1 in gitgitgadget/git-mailing-list-mirror):',
'triggered <the URL to the workflow handle-new-mails.yml run on main with inputs undefined>'
].join(' ')
})
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'handle-new-mails.yml',
'main',
undefined
])
})
testWebhookPayload('react to PR push', 'pull_request', {
action: 'synchronize',
pull_request: {
html_url: 'https://github.com/gitgitgadget/git/pull/1956',
},
repository: {
full_name: 'gitgitgadget/git',
owner: {
login: 'gitgitgadget'
}
}
}, (context) => {
expect(context.res).toEqual({
body: [
'Okay, triggered <the URL to the workflow handle-pr-push.yml run on main with inputs',
'{"pr-url":"https://github.com/gitgitgadget/git/pull/1956"}>!'
].join(' ')
})
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'handle-pr-push.yml',
'main', {
'pr-url': 'https://github.com/gitgitgadget/git/pull/1956',
}
])
})