-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest.js
More file actions
175 lines (151 loc) · 4.6 KB
/
test.js
File metadata and controls
175 lines (151 loc) · 4.6 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
import fs from 'node:fs'
import path from 'node:path'
import crypto from 'node:crypto'
import { test } from 'node:test'
import assert from 'node:assert'
import { fileURLToPath } from 'node:url'
import webhook from './github-webhook.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
function signBlob (key, blob) {
return `sha1=${crypto.createHmac('sha1', key).update(blob).digest('hex')}`
}
async function makeRequest (server, { method = 'POST', path = '/', headers = {}, body = '{}' }) {
return new Promise((resolve, reject) => {
server.listen(0, '127.0.0.1', () => {
const { port } = server.address()
const url = new URL(path, `http://127.0.0.1:${port}`)
fetch(url, {
method,
headers: {
'Content-Type': 'application/json',
...headers
},
body
})
.then(async (res) => {
const text = await res.text()
let json
try {
json = JSON.parse(text)
} catch {}
server.close()
resolve({ status: res.status, headers: res.headers, text, json })
})
.catch((err) => {
server.close()
reject(err)
})
})
})
}
test('invalid url gets 404', async () => {
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
const server = webhook(options)
const res = await makeRequest(server, {
path: '/',
headers: {
'X-Hub-Signature': signBlob('foofaa', '{}'),
'X-Github-Event': 'issues',
'X-Github-Delivery': '123abc'
},
body: '{}'
})
assert.strictEqual(res.status, 404)
assert.ok(res.headers.get('content-type').includes('json'))
})
test('valid url, incomplete data gets 400', async () => {
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
const server = webhook(options)
const res = await makeRequest(server, {
path: '/webhook',
headers: {
'X-Github-Event': 'issues',
'X-Github-Delivery': '123abc'
},
body: '{}'
})
assert.strictEqual(res.status, 400)
assert.ok(res.headers.get('content-type').includes('json'))
})
test('valid url, complete data gets 200', async () => {
const options = { port: 0, path: '/webhook', secret: 'foofaa' }
const server = webhook(options)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const id = '123abc'
const eventType = 'issues'
const eventPromise = new Promise((resolve) => {
server.webhookHandler.on(eventType, (event) => {
delete event.host
resolve(event)
})
})
const res = await makeRequest(server, {
path: '/webhook',
headers: {
'X-Hub-Signature': signBlob('foofaa', json),
'X-Github-Event': eventType,
'X-Github-Delivery': id
},
body: json
})
assert.strictEqual(res.status, 200)
assert.ok(res.headers.get('content-type').includes('json'))
const event = await eventPromise
assert.strictEqual(event.event, eventType)
assert.strictEqual(event.id, id)
assert.deepStrictEqual(event.payload, obj)
})
test('valid request triggers rule', { skip: process.platform === 'win32' }, async () => {
const tmpfile = path.join(__dirname, '__test_data.' + Math.random())
const eventType = 'issues'
const options = {
port: 0,
path: '/webhook',
secret: 'foofaa',
rules: [
{
event: eventType,
match: 'some == xxgithub',
exec: ['sh', '-c', `echo "w00t!" > ${tmpfile}2`]
},
{
event: eventType,
match: 'some == github',
exec: `echo "w00t!" > ${tmpfile}`
}
]
}
const server = webhook(options)
const obj = { some: 'github', object: 'with', properties: true }
const json = JSON.stringify(obj)
const id = '123abc'
const eventPromise = new Promise((resolve) => {
server.webhookHandler.on(eventType, (event) => {
delete event.host
resolve(event)
})
})
const res = await makeRequest(server, {
path: '/webhook',
headers: {
'X-Hub-Signature': signBlob('foofaa', json),
'X-Github-Event': eventType,
'X-Github-Delivery': id
},
body: json
})
assert.strictEqual(res.status, 200)
const event = await eventPromise
assert.strictEqual(event.event, eventType)
assert.strictEqual(event.id, id)
assert.deepStrictEqual(event.payload, obj)
// Wait for the rule to execute
await new Promise((resolve) => setTimeout(resolve, 200))
const data = fs.readFileSync(tmpfile, 'utf8')
assert.strictEqual(data, 'w00t!\n')
// Verify second rule did not trigger
assert.throws(() => fs.statSync(`${tmpfile}2`), { code: 'ENOENT' })
// Cleanup
fs.unlinkSync(tmpfile)
})