|
| 1 | +import { createServer } from "http"; |
| 2 | +import { readFileSync } from "fs"; |
| 3 | + |
| 4 | +import fetch from "node-fetch"; |
| 5 | +import { sign } from "@octokit/webhooks-methods"; |
| 6 | + |
| 7 | +import { Webhooks, createNodeMiddleware } from "../../src"; |
| 8 | + |
| 9 | +const pushEventPayloadString = readFileSync( |
| 10 | + "test/fixtures/push-payload.json", |
| 11 | + "utf-8" |
| 12 | +); |
| 13 | +let signatureSha256: string; |
1 | 14 | describe("Deprecations", () => { |
2 | | - test("there are currently no deprecations", () => {}); |
| 15 | + beforeAll(async () => { |
| 16 | + signatureSha256 = await sign( |
| 17 | + { secret: "mySecret", algorithm: "sha256" }, |
| 18 | + pushEventPayloadString |
| 19 | + ); |
| 20 | + }); |
| 21 | + test("onUnhandledRequest", async () => { |
| 22 | + const spy = jest.spyOn(console, "error"); |
| 23 | + const webhooks = new Webhooks({ |
| 24 | + secret: "mySecret", |
| 25 | + }); |
| 26 | + |
| 27 | + const server = createServer( |
| 28 | + createNodeMiddleware(webhooks, { |
| 29 | + onUnhandledRequest(_request, response) { |
| 30 | + response.writeHead(404); |
| 31 | + response.end("nope"); |
| 32 | + }, |
| 33 | + }) |
| 34 | + ).listen(); |
| 35 | + |
| 36 | + // @ts-expect-error complains about { port } although it's included in returned AddressInfo interface |
| 37 | + const { port } = server.address(); |
| 38 | + |
| 39 | + await fetch(`http://localhost:${port}/api/github/webhooks`, { |
| 40 | + method: "PUT", |
| 41 | + headers: { |
| 42 | + "X-GitHub-Delivery": "123e4567-e89b-12d3-a456-426655440000", |
| 43 | + "X-GitHub-Event": "push", |
| 44 | + "X-Hub-Signature-256": signatureSha256, |
| 45 | + }, |
| 46 | + body: "invalid", |
| 47 | + }); |
| 48 | + |
| 49 | + expect(spy).toBeCalledWith( |
| 50 | + "[@octokit/webhooks] `onUnhandledRequest()` is deprecated and will be removed in a future release of `@octokit/webhooks`" |
| 51 | + ); |
| 52 | + spy.mockClear(); |
| 53 | + server.close(); |
| 54 | + }); |
| 55 | + |
| 56 | + test("webhooks.verify(payload, signature) with object payload", async () => { |
| 57 | + const spy = jest.spyOn(console, "error"); |
| 58 | + const secret = "mysecret"; |
| 59 | + const webhooks = new Webhooks({ secret }); |
| 60 | + |
| 61 | + await webhooks.verify( |
| 62 | + JSON.parse(pushEventPayloadString), |
| 63 | + await sign({ secret, algorithm: "sha256" }, pushEventPayloadString) |
| 64 | + ); |
| 65 | + expect(spy).toBeCalledWith( |
| 66 | + "[@octokit/webhooks] Passing a JSON payload object to `verify()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`" |
| 67 | + ); |
| 68 | + spy.mockClear(); |
| 69 | + }); |
| 70 | + |
| 71 | + test("webhooks.verifyAndReceive(payload, signature) with object payload", async () => { |
| 72 | + const spy = jest.spyOn(console, "error"); |
| 73 | + const secret = "mysecret"; |
| 74 | + const webhooks = new Webhooks({ secret }); |
| 75 | + |
| 76 | + await webhooks.verifyAndReceive({ |
| 77 | + id: "123e456", |
| 78 | + name: "push", |
| 79 | + payload: JSON.parse(pushEventPayloadString), |
| 80 | + signature: await sign( |
| 81 | + { secret, algorithm: "sha256" }, |
| 82 | + pushEventPayloadString |
| 83 | + ), |
| 84 | + }); |
| 85 | + expect(spy).toBeCalledWith( |
| 86 | + "[@octokit/webhooks] Passing a JSON payload object to `verifyAndReceive()` is deprecated and the functionality will be removed in a future release of `@octokit/webhooks`" |
| 87 | + ); |
| 88 | + spy.mockClear(); |
| 89 | + }); |
3 | 90 | }); |
0 commit comments