Files
crosscode-platform/apps/events/e2e/webhooks.spec.ts
T
joshuaandClaude Opus 5.5 51dc908630 Outbound webhooks (1.48.0)
Site settings -> Webhooks: admins add https endpoints and choose event
types (registration created/paid/cancelled, payment received/refunded,
ticket checked in, account created, event published). Events are
noticed by a Prisma client extension so every write path is covered,
re-checked before sending, and delivered as signed JSON
(X-CrossEvents-Signature, HMAC-SHA256) with retries, a 30-day delivery
log, Send test and Resend. Public https addresses only; each endpoint
is added to the operator register; kids and volunteer tickets are never
sent. New `webhooks` job. Migration 20261028160000_webhooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:00:59 +02:00

71 lines
3.6 KiB
TypeScript

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<void>(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");
});
});