-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest01WebhookHub.js
More file actions
191 lines (163 loc) · 5.27 KB
/
Copy pathtest01WebhookHub.js
File metadata and controls
191 lines (163 loc) · 5.27 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
import crypto from 'crypto';
import fetch from 'node-fetch';
import {
buildDummyRtmsWebhook,
parseArgs,
signZoomWebhook
} from './dummyRtms.js';
const args = parseArgs(process.argv.slice(2));
const target = args.target || process.env.WEBHOOK_TEST_URL || 'http://127.0.0.1:4400/webhook';
const secret = args.secret || process.env.WEBHOOK_TEST_SECRET || process.env.ZOOM_SECRET_TOKEN || 'testsecrettoken';
const wrongSecret = args.wrongSecret || 'wrongsecret';
const staleSeconds = Number(args.staleSeconds || 1000);
const results = [];
await testHealth();
await testUrlValidation();
await testValidSignedWebhook();
await testDuplicateRtmsWebhook();
await testInvalidSignature();
await testStaleTimestamp();
await testMalformedPayload();
const failed = results.filter((result) => !result.ok);
for (const result of results) {
const marker = result.ok ? 'PASS' : 'FAIL';
console.log(`${marker} ${result.name} status=${result.status}${result.reason ? ` reason=${result.reason}` : ''}`);
}
if (failed.length > 0) {
console.error(`01 webhook hub tester failed: ${failed.length}/${results.length}`);
process.exit(1);
}
console.log(`01 webhook hub tester passed: ${results.length}/${results.length}`);
async function testHealth() {
const healthUrl = new URL('/health', target).toString();
const response = await fetch(healthUrl);
const body = await readJson(response);
record({
name: 'health',
status: response.status,
ok: response.status === 200 && body?.ok === true && body?.service === 'centralized-webhook-hub'
});
}
async function testUrlValidation() {
const plainToken = `plain-${Date.now()}`;
const response = await postJson(target, {
event: 'endpoint.url_validation',
payload: { plainToken }
});
const body = await readJson(response);
const expectedEncryptedToken = crypto
.createHmac('sha256', secret)
.update(plainToken)
.digest('hex');
record({
name: 'url_validation',
status: response.status,
ok: response.status === 200 &&
body?.plainToken === plainToken &&
body?.encryptedToken === expectedEncryptedToken
});
}
async function testValidSignedWebhook() {
const body = buildDummyRtmsWebhook({
event: 'meeting.rtms_started',
region: 'IAD',
streamId: `test01-valid-${Date.now()}`
});
const response = await postSignedWebhook(body, secret);
record({
name: 'valid_signed_webhook',
status: response.status,
ok: response.status === 204
});
}
async function testInvalidSignature() {
const body = buildDummyRtmsWebhook({
event: 'meeting.rtms_started',
region: 'IAD',
streamId: `test01-invalid-signature-${Date.now()}`
});
const response = await postSignedWebhook(body, wrongSecret);
const responseBody = await readJson(response);
record({
name: 'invalid_signature_rejected',
status: response.status,
reason: responseBody?.reason,
ok: response.status === 401 && responseBody?.reason === 'invalid_x_zm_signature'
});
}
async function testDuplicateRtmsWebhook() {
const body = buildDummyRtmsWebhook({
event: 'meeting.rtms_started',
region: 'IAD',
streamId: `test01-duplicate-${Date.now()}`,
eventTs: Date.now()
});
const firstResponse = await postSignedWebhook(body, secret);
const secondResponse = await postSignedWebhook(body, secret);
record({
name: 'duplicate_rtms_webhook_dropped',
status: secondResponse.status,
reason: secondResponse.headers.get('x-rtms-duplicate') || undefined,
ok: firstResponse.status === 204 &&
secondResponse.status === 204 &&
secondResponse.headers.get('x-rtms-duplicate') === 'true'
});
}
async function testStaleTimestamp() {
const body = buildDummyRtmsWebhook({
event: 'meeting.rtms_started',
region: 'IAD',
streamId: `test01-stale-${Date.now()}`
});
const staleTimestamp = Math.floor(Date.now() / 1000) - staleSeconds;
const response = await postSignedWebhook(body, secret, staleTimestamp);
const responseBody = await readJson(response);
record({
name: 'stale_timestamp_rejected',
status: response.status,
reason: responseBody?.reason,
ok: response.status === 401 && responseBody?.reason === 'stale_x_zm_request_timestamp'
});
}
async function testMalformedPayload() {
const response = await postJson(target, { hello: 'world' });
const body = await readJson(response);
record({
name: 'malformed_payload_rejected',
status: response.status,
reason: body?.error,
ok: response.status === 400 && body?.error === 'missing_event_or_payload'
});
}
async function postSignedWebhook(body, signingSecret, timestamp) {
const bodyText = JSON.stringify(body);
const signed = signZoomWebhook(bodyText, signingSecret, timestamp);
return fetch(target, {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-zm-request-timestamp': signed.timestamp,
'x-zm-signature': signed.signature
},
body: bodyText
});
}
async function postJson(url, body) {
return fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body)
});
}
async function readJson(response) {
const text = await response.text();
if (!text) return null;
try {
return JSON.parse(text);
} catch {
return { raw: text };
}
}
function record(result) {
results.push(result);
}