import http from "http"; import crypto from "crypto"; import { test, expect, loginAs, API } from "./fixtures"; // Outbound webhooks (1.48.0): an admin adds an endpoint (a receiver this test runs on localhost), // sends a test, and a new account is delivered — signed with the endpoint's secret. type Received = { headers: http.IncomingHttpHeaders; body: string }; const RECEIVER_PORT = 5199; test.describe.serial("outbound webhooks", () => { const received: Received[] = []; let server: http.Server; let secret = ""; test.beforeAll(async () => { server = http.createServer((req, res) => { let body = ""; req.on("data", c => { body += c; }); req.on("end", () => { received.push({ headers: req.headers, body }); res.writeHead(200).end("ok"); }); }); await new Promise(resolve => server.listen(RECEIVER_PORT, resolve)); }); test.afterAll(async () => { await new Promise(resolve => server.close(resolve)); }); const signed = (r: Received) => { const m = /^t=(\d+),v1=([0-9a-f]{64})$/.exec(String(r.headers["x-crossevents-signature"])); return !!m && crypto.createHmac("sha256", secret).update(`${m[1]}.${r.body}`).digest("hex") === m[2]; }; test("an admin adds a webhook and sends a test", async ({ page }) => { await loginAs(page, "admin"); await page.goto("/dashboard/admin/settings?tab=webhooks"); await page.getByRole("button", { name: "+ Add webhook" }).click(); await page.getByLabel("Name").fill("E2E receiver"); await page.getByLabel("Address (https)").fill(`http://localhost:${RECEIVER_PORT}/hook`); await page.getByText("Account created", { exact: true }).click(); await page.getByText("Registration made", { exact: true }).click(); await page.getByRole("button", { name: "Add webhook" }).click(); await expect(page.getByTestId("webhook-secret")).toBeVisible(); secret = (await page.getByTestId("webhook-secret").locator("code").first().innerText()).trim(); expect(secret).toMatch(/^whsec_/); await page.getByRole("button", { name: "I've saved it" }).click(); const endpoint = page.getByTestId("webhook-endpoint").filter({ hasText: "E2E receiver" }); await endpoint.getByRole("button", { name: "Send test" }).click(); await expect(page.getByRole("status")).toContainText("Test delivered (answered 200)"); const testHook = received.find(r => r.headers["x-crossevents-event"] === "webhook.test"); expect(testHook && signed(testHook)).toBe(true); }); test("a new account is sent, signed, and shows in the delivery log", async ({ page, request }) => { const email = `webhook-${Date.now()}@test.local`; const res = await request.post(`${API}/api/users`, { data: { name: "Webhook Person", email, password: "E2e-password-123", phoneNumber: "0820000123" } }); expect(res.ok()).toBe(true); await expect.poll(() => received.filter(r => r.headers["x-crossevents-event"] === "user.created").length, { timeout: 15000 }).toBeGreaterThan(0); const hook = received.find(r => r.headers["x-crossevents-event"] === "user.created")!; expect(signed(hook)).toBe(true); const body = JSON.parse(hook.body); expect(body).toMatchObject({ type: "user.created", data: { person: { name: "Webhook Person", email } } }); expect(body.id).toMatch(/^evt_/); await loginAs(page, "admin"); await page.goto("/dashboard/admin/settings?tab=webhooks"); const endpoint = page.getByTestId("webhook-endpoint").filter({ hasText: "E2E receiver" }); await endpoint.getByRole("button", { name: "Delivery log" }).click(); await expect(endpoint.getByTestId("webhook-delivery").filter({ hasText: "user.created" })).toContainText("succeeded"); }); test("the guide explains the signature and shows an example of each event", async ({ page }) => { await loginAs(page, "admin"); await page.goto("/dashboard/admin/settings?tab=webhooks"); const guide = page.getByTestId("webhook-guide"); const toggle = guide.getByRole("button", { name: /How webhooks work/ }); await expect(toggle).toBeVisible(); if ((await toggle.getAttribute("aria-expanded")) !== "true") await toggle.click(); await expect(guide).toContainText("X-CrossEvents-Signature"); await guide.getByRole("button", { name: /registration.paid/ }).click(); const example = guide.getByTestId("webhook-example-registration.paid"); await expect(example).toContainText("\"type\": \"registration.paid\""); await expect(example).toContainText("\"amountPaid\""); await guide.getByRole("button", { name: "PHP" }).click(); await expect(guide).toContainText("hash_hmac"); }); });