- /api (v1) answers 410 API_V1_REMOVED, except provider webhooks (/api/webhooks/*, /api/whatsapp/webhook) and links in sent messages (308s, one-click POST). 43 v1 route files and deprecation.ts removed; /docs redirects to /docs/v2; API_DOCUMENTATION.md points there. - Read-only and impersonation lists name v2 paths only. The campus scope route test now runs over v2 routes (v2CampusScope.test.ts). - Passwords hashed with Argon2id (@node-rs/argon2); bcrypt hashes still sign in and are re-hashed at the next sign-in. - e2e on /api/v2 only (v2() helper); Playwright waits on /api/v2. - MIGRATING-TO-2.md with the full v1 -> v2 table. - Two documented response shapes corrected. Backend 690 tests and e2e 102/102 (PayFast sandbox skipped) pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
4.6 KiB
TypeScript
87 lines
4.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/v2/auth/signup`, { data: { name: "Webhook Person", email, password: "E2e-password-123", phone: "0820000123", acceptTerms: true } });
|
|
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");
|
|
});
|
|
});
|