|
1 | 1 | const index = require('../GitGitGadget/index')
|
| 2 | +const crypto = require('crypto') |
| 3 | +const stream = require('stream') |
| 4 | +const https = require('https') |
2 | 5 |
|
3 | 6 | process.env['GITHUB_WEBHOOK_SECRET'] = 'for-testing'
|
| 7 | +process.env['GITGITGADGET_TRIGGER_TOKEN'] = 'token-for-testing' |
4 | 8 |
|
5 | 9 | test('reject requests other than webhook payloads', async () => {
|
6 | 10 | const context = {
|
@@ -45,3 +49,94 @@ test('reject requests other than webhook payloads', async () => {
|
45 | 49 | context.req.rawBody = '# empty'
|
46 | 50 | await expectInvalidWebhook('Incorrect X-Hub-Signature')
|
47 | 51 | })
|
| 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