-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.js
More file actions
213 lines (193 loc) · 6.15 KB
/
index.test.js
File metadata and controls
213 lines (193 loc) · 6.15 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
const mockTriggerWorkflowDispatch = jest.fn(async (_context, _token, owner, repo, workflow_id, ref, inputs) => {
expect(`${owner}/${repo}`).toEqual('gitgitgadget-workflows/gitgitgadget-workflows')
expect(workflow_id).toEqual('sync-ref.yml')
expect(ref).toEqual('main')
expect(inputs).toEqual({ ref: 'refs/heads/next' })
return { html_url: '<the URL to the workflow run>'}
})
jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
triggerWorkflowDispatch: mockTriggerWorkflowDispatch
}))
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).toHaveBeenCalledTimes(1)
}
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).toHaveBeenCalledTimes(1)
} catch (e) {
context.log.mock.calls.forEach(e => console.log(e[0]))
throw e;
}
})
}
testIssueComment('/test', async (context) => {
expect(context.done).toHaveBeenCalledTimes(1)
expect(context.res).toEqual({
body: 'Okay!'
})
expect(mockRequest.write).toHaveBeenCalledTimes(1)
expect(JSON.parse(mockRequest.write.mock.calls[0][0])).toEqual({
definition: {
id: 3
},
sourceBranch: 'refs/pull/1886743660/head',
parameters: '{"pr.comment.id":27988538471837300}'
})
expect(mockRequest.end).toHaveBeenCalledTimes(1)
})
testIssueComment('/verify-repository', 'nope', (context) => {
expect(context.done).toHaveBeenCalledTimes(1)
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).toHaveBeenCalledTimes(1)
} 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 run>'
})
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget-workflows',
'gitgitgadget-workflows',
'sync-ref.yml',
'main', {
ref: 'refs/heads/next'
}
])
})