Skip to content

Commit f179fa9

Browse files
chore(tests): update webhook tests
1 parent f08d300 commit f179fa9

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

tests/api-resources/webhooks.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,55 @@ const client = new ImageKit({
1111
});
1212

1313
describe('resource webhooks', () => {
14-
test.skip('unwrap', async () => {
14+
test.skip('unwrap', () => {
1515
const key = 'whsec_c2VjcmV0Cg==';
1616
const payload =
1717
'{"id":"id","type":"video.transformation.accepted","created_at":"2019-12-27T18:11:19.117Z","data":{"asset":{"url":"https://example.com"},"transformation":{"type":"video-transformation","options":{"audio_codec":"aac","auto_rotate":true,"format":"mp4","quality":0,"stream_protocol":"HLS","variants":["string"],"video_codec":"h264"}}},"request":{"url":"https://example.com","x_request_id":"x_request_id","user_agent":"user_agent"}}';
1818
const msgID = '1';
1919
const timestamp = new Date();
20-
const wh = new Webhook(key);
20+
const wh = new Webhook('whsec_c2VjcmV0Cg==');
2121
const signature = wh.sign(msgID, timestamp, payload);
2222
const headers: Record<string, string> = {
2323
'webhook-signature': signature,
2424
'webhook-id': msgID,
2525
'webhook-timestamp': String(Math.floor(timestamp.getTime() / 1000)),
2626
};
2727
client.webhooks.unwrap(payload, { headers, key });
28+
client.withOptions({ webhookSecret: key }).webhooks.unwrap(payload, { headers });
29+
client.withOptions({ webhookSecret: 'whsec_aaaaaaaaaa==' }).webhooks.unwrap(payload, { headers, key });
2830
expect(() => {
2931
const wrongKey = 'whsec_aaaaaaaaaa==';
3032
client.webhooks.unwrap(payload, { headers, key: wrongKey });
3133
}).toThrow('No matching signature found');
34+
expect(() => {
35+
const wrongKey = 'whsec_aaaaaaaaaa==';
36+
client.withOptions({ webhookSecret: wrongKey }).webhooks.unwrap(payload, { headers });
37+
}).toThrow('No matching signature found');
3238
expect(() => {
3339
const badSig = wh.sign(msgID, timestamp, 'some other payload');
3440
client.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-signature': badSig }, key });
3541
}).toThrow('No matching signature found');
42+
expect(() => {
43+
const badSig = wh.sign(msgID, timestamp, 'some other payload');
44+
client
45+
.withOptions({ webhookSecret: key })
46+
.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-signature': badSig } });
47+
}).toThrow('No matching signature found');
3648
expect(() => {
3749
client.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-timestamp': '5' }, key });
3850
}).toThrow('Message timestamp too old');
51+
expect(() => {
52+
client
53+
.withOptions({ webhookSecret: key })
54+
.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-timestamp': '5' } });
55+
}).toThrow('Message timestamp too old');
3956
expect(() => {
4057
client.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-id': 'wrong' }, key });
4158
}).toThrow('No matching signature found');
59+
expect(() => {
60+
client
61+
.withOptions({ webhookSecret: key })
62+
.webhooks.unwrap(payload, { headers: { ...headers, 'webhook-id': 'wrong' } });
63+
}).toThrow('No matching signature found');
4264
});
4365
});

0 commit comments

Comments
 (0)