|
| 1 | +import { env } from "cloudflare:test"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { relayWebhookEvent, type RelayEvent } from "../src/lib/relay"; |
| 4 | +import { putInstanceSetting } from "../src/lib/db"; |
| 5 | +import { fetcherFailing, fetcherReturning, SAMPLE_GUID, SAMPLE_TENANT_NAME } from "./helpers"; |
| 6 | + |
| 7 | +function sampleEvent(): RelayEvent { |
| 8 | + return { |
| 9 | + id: "00000000-0000-4000-8000-000000000000", |
| 10 | + tenant_id: "00000000-0000-4000-8000-000000000000", |
| 11 | + tenant_name: SAMPLE_TENANT_NAME, |
| 12 | + guid: SAMPLE_GUID, |
| 13 | + received_at: "2026-07-03T00:00:00.000Z", |
| 14 | + event_type: "false_positive_report", |
| 15 | + payload_json: '{"reportType":"false_positive_report","url":"https://login.example.com"}', |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +function fetcherCapturing(captured: { url?: string; body?: string }): typeof fetch { |
| 20 | + return (async (url: any, init: any) => { |
| 21 | + captured.url = String(url); |
| 22 | + captured.body = String(init?.body ?? ""); |
| 23 | + return new Response("ok", { status: 200 }); |
| 24 | + }) as typeof fetch; |
| 25 | +} |
| 26 | + |
| 27 | +function fetcherRefusing(): typeof fetch { |
| 28 | + return (async () => { |
| 29 | + throw new Error("relay fetch must not be called when disabled"); |
| 30 | + }) as typeof fetch; |
| 31 | +} |
| 32 | + |
| 33 | +describe("webhook relay", () => { |
| 34 | + it("skips without calling out when no relay URL is configured", async () => { |
| 35 | + const outcome = await relayWebhookEvent(env, sampleEvent(), fetcherRefusing()); |
| 36 | + expect(outcome).toEqual({ status: "skipped" }); |
| 37 | + }); |
| 38 | + |
| 39 | + it("POSTs the event as JSON to the configured URL", async () => { |
| 40 | + await putInstanceSetting(env.DB, "false_positive_relay_url", "https://hooks.example.test/relay"); |
| 41 | + const captured: { url?: string; body?: string } = {}; |
| 42 | + const outcome = await relayWebhookEvent(env, sampleEvent(), fetcherCapturing(captured)); |
| 43 | + expect(outcome).toEqual({ status: "sent", httpStatus: 200 }); |
| 44 | + expect(captured.url).toBe("https://hooks.example.test/relay"); |
| 45 | + const sent = JSON.parse(captured.body ?? "{}"); |
| 46 | + expect(sent.source).toBe("checkdeploymanager"); |
| 47 | + expect(sent.kind).toBe("webhook_event"); |
| 48 | + expect(sent.event.tenant_name).toBe(SAMPLE_TENANT_NAME); |
| 49 | + expect(sent.event.event_type).toBe("false_positive_report"); |
| 50 | + // The payload travels verbatim as a string, never parsed by the relay. |
| 51 | + expect(typeof sent.event.payload_json).toBe("string"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("rejects non-https relay URLs", async () => { |
| 55 | + await putInstanceSetting(env.DB, "false_positive_relay_url", "http://insecure.example.test/hook"); |
| 56 | + const outcome = await relayWebhookEvent(env, sampleEvent(), fetcherRefusing()); |
| 57 | + expect(outcome).toEqual({ |
| 58 | + status: "failed", |
| 59 | + error: "relay URL must start with https://", |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it("reports failure on network errors and non-2xx responses", async () => { |
| 64 | + await putInstanceSetting(env.DB, "false_positive_relay_url", "https://hooks.example.test/relay"); |
| 65 | + const network = await relayWebhookEvent(env, sampleEvent(), fetcherFailing()); |
| 66 | + expect(network.status).toBe("failed"); |
| 67 | + const http = await relayWebhookEvent(env, sampleEvent(), fetcherReturning("nope", 500)); |
| 68 | + expect(http).toEqual({ status: "failed", error: "HTTP 500" }); |
| 69 | + }); |
| 70 | +}); |
0 commit comments