|
| 1 | +import { expect } from "@jest/globals"; |
1 | 2 | import WebhookEventType from "../objects/WebhookEventType.js";
|
2 | 3 | import { verifyAndParseWebhook } from "../webhooks.js";
|
3 | 4 |
|
4 | 5 | describe("Webhooks", () => {
|
5 | 6 | test("should verify and parse webhook data", async () => {
|
6 |
| - const eventType = WebhookEventType.NODE_STATUS; |
7 |
| - const eventId = "1615c8be5aa44e429eba700db2ed8ca5"; |
8 |
| - const entityId = "lightning_node:01882c25-157a-f96b-0000-362d42b64397"; |
9 |
| - const timeStamp = new Date("2023-05-17T23:56:47.874449+00:00"); |
10 | 7 | const data = `{"event_type": "NODE_STATUS", "event_id": "1615c8be5aa44e429eba700db2ed8ca5", "timestamp": "2023-05-17T23:56:47.874449+00:00", "entity_id": "lightning_node:01882c25-157a-f96b-0000-362d42b64397"}`;
|
11 |
| - const hexdigest = |
| 8 | + const hexDigest = |
12 | 9 | "62a8829aeb48b4142533520b1f7f86cdb1ee7d718bf3ea15bc1c662d4c453b74";
|
13 | 10 | const webhookSecret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX";
|
14 | 11 |
|
15 | 12 | const webhook = await verifyAndParseWebhook(
|
16 |
| - Buffer.from(data, "utf-8"), |
17 |
| - hexdigest, |
| 13 | + Buffer.from(data), |
| 14 | + hexDigest, |
18 | 15 | webhookSecret,
|
19 | 16 | );
|
20 | 17 |
|
21 |
| - expect(webhook.entity_id).toBe(entityId); |
22 |
| - expect(webhook.event_id).toBe(eventId); |
23 |
| - expect(webhook.event_type).toBe(eventType); |
24 |
| - expect(webhook.timestamp).toEqual(timeStamp); |
| 18 | + expect(webhook.entity_id).toBe( |
| 19 | + "lightning_node:01882c25-157a-f96b-0000-362d42b64397", |
| 20 | + ); |
| 21 | + expect(webhook.event_id).toBe("1615c8be5aa44e429eba700db2ed8ca5"); |
| 22 | + expect(webhook.event_type).toBe(WebhookEventType.NODE_STATUS); |
| 23 | + expect(webhook.timestamp).toEqual( |
| 24 | + new Date("2023-05-17T23:56:47.874449+00:00"), |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + test.each([ |
| 29 | + ["wrong length", "deadbeef"], |
| 30 | + ["is incorrect", "a".repeat(64)], |
| 31 | + ["is not hex", "NotAHexValue"], |
| 32 | + [ |
| 33 | + "has extra bytes", |
| 34 | + "62a8829aeb48b4142533520b1f7f86cdb1ee7d718bf3ea15bc1c662d4c453b74" + "qq", |
| 35 | + ], |
| 36 | + ])("should error when hex digest %s", async (name, digest) => { |
| 37 | + const data = `{"event_type": "NODE_STATUS", "event_id": "1615c8be5aa44e429eba700db2ed8ca5", "timestamp": "2023-05-17T23:56:47.874449+00:00", "entity_id": "lightning_node:01882c25-157a-f96b-0000-362d42b64397"}`; |
| 38 | + const webhookSecret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX"; |
| 39 | + |
| 40 | + expect.assertions(1); |
| 41 | + await expect( |
| 42 | + verifyAndParseWebhook(Buffer.from(data), digest, webhookSecret), |
| 43 | + ).rejects.toThrow("Webhook message hash does not match signature"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("should throw on invalid signature", async () => { |
| 47 | + const data = `{"event_type": "NODE_STATUS", "event_id": "1615c8be5aa44e429eba700db2ed8ca5", "timestamp": "2023-05-17T23:56:47.874449+00:00", "entity_id": "lightning_node:01882c25-157a-f96b-0000-362d42b64397"}`; |
| 48 | + const invalidHex = |
| 49 | + "62a8829aeb48b4142533520b1f7f86cdb1ee7d718bf3ea15bc1c662d4c453b70"; |
| 50 | + const webhookSecret = "3gZ5oQQUASYmqQNuEk0KambNMVkOADDItIJjzUlAWjX"; |
| 51 | + |
| 52 | + await expect( |
| 53 | + verifyAndParseWebhook( |
| 54 | + new TextEncoder().encode(data), |
| 55 | + invalidHex, |
| 56 | + webhookSecret, |
| 57 | + ), |
| 58 | + ).rejects.toThrow("Webhook message hash does not match signature"); |
25 | 59 | });
|
26 | 60 | });
|
0 commit comments