|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +process.env.TURSO_DATABASE_URL = "file::memory:"; |
| 5 | + |
| 6 | +const { |
| 7 | + db, |
| 8 | + deleteProjectWebhook, |
| 9 | + ensureSchema, |
| 10 | + setProjectWebhookActive, |
| 11 | +} = await import("../lib/db.ts"); |
| 12 | +const { dispatchEvent } = await import("../lib/webhooks.ts"); |
| 13 | + |
| 14 | +await ensureSchema(); |
| 15 | + |
| 16 | +async function insertEndpoint(projectId, suffix) { |
| 17 | + const id = `endpoint-${suffix}`; |
| 18 | + await db().execute({ |
| 19 | + sql: `INSERT INTO webhook_endpoints (id, project_id, url, secret, events) |
| 20 | + VALUES (?, ?, ?, ?, '["*"]')`, |
| 21 | + args: [id, projectId, `https://hooks.example.test/${suffix}`, `whsec_${suffix}`], |
| 22 | + }); |
| 23 | + return id; |
| 24 | +} |
| 25 | + |
| 26 | +test("project webhook targets pause and resume without losing configuration", async () => { |
| 27 | + const id = await insertEndpoint("project-pause", "pause"); |
| 28 | + const originalFetch = globalThis.fetch; |
| 29 | + const calls = []; |
| 30 | + globalThis.fetch = async (...args) => { |
| 31 | + calls.push(args); |
| 32 | + return new Response(null, { status: 204 }); |
| 33 | + }; |
| 34 | + |
| 35 | + try { |
| 36 | + assert.equal(await setProjectWebhookActive(id, "project-pause", false), true); |
| 37 | + await dispatchEvent("project-pause", "build.finished", { ok: true }); |
| 38 | + assert.equal(calls.length, 0); |
| 39 | + |
| 40 | + const paused = await db().execute({ |
| 41 | + sql: `SELECT url, secret, events, active FROM webhook_endpoints WHERE id = ?`, |
| 42 | + args: [id], |
| 43 | + }); |
| 44 | + assert.deepEqual( |
| 45 | + { |
| 46 | + url: String(paused.rows[0].url), |
| 47 | + secret: String(paused.rows[0].secret), |
| 48 | + events: String(paused.rows[0].events), |
| 49 | + active: Number(paused.rows[0].active), |
| 50 | + }, |
| 51 | + { |
| 52 | + url: "https://hooks.example.test/pause", |
| 53 | + secret: "whsec_pause", |
| 54 | + events: '["*"]', |
| 55 | + active: 0, |
| 56 | + }, |
| 57 | + ); |
| 58 | + |
| 59 | + assert.equal(await setProjectWebhookActive(id, "project-pause", true), true); |
| 60 | + await dispatchEvent("project-pause", "build.finished", { ok: true }); |
| 61 | + assert.equal(calls.length, 1); |
| 62 | + } finally { |
| 63 | + globalThis.fetch = originalFetch; |
| 64 | + } |
| 65 | +}); |
| 66 | + |
| 67 | +test("project scoping prevents cross-project changes", async () => { |
| 68 | + const id = await insertEndpoint("project-owner", "scoped"); |
| 69 | + |
| 70 | + assert.equal(await setProjectWebhookActive(id, "project-other", false), false); |
| 71 | + assert.equal(await deleteProjectWebhook(id, "project-other"), false); |
| 72 | + |
| 73 | + const endpoint = await db().execute({ |
| 74 | + sql: `SELECT active FROM webhook_endpoints WHERE id = ?`, |
| 75 | + args: [id], |
| 76 | + }); |
| 77 | + assert.equal(Number(endpoint.rows[0].active), 1); |
| 78 | +}); |
| 79 | + |
| 80 | +test("deleting a project webhook also deletes its delivery history", async () => { |
| 81 | + const id = await insertEndpoint("project-delete", "delete"); |
| 82 | + await db().execute({ |
| 83 | + sql: `INSERT INTO webhook_deliveries |
| 84 | + (id, endpoint_id, event_type, payload, idempotency_key, status) |
| 85 | + VALUES (?, ?, ?, ?, ?, 'failed')`, |
| 86 | + args: ["delivery-delete", id, "build.failed", "{}", "event-delete"], |
| 87 | + }); |
| 88 | + |
| 89 | + assert.equal(await deleteProjectWebhook(id, "project-delete"), true); |
| 90 | + assert.equal(await deleteProjectWebhook(id, "project-delete"), false); |
| 91 | + |
| 92 | + const endpoints = await db().execute({ |
| 93 | + sql: `SELECT 1 FROM webhook_endpoints WHERE id = ?`, |
| 94 | + args: [id], |
| 95 | + }); |
| 96 | + const deliveries = await db().execute({ |
| 97 | + sql: `SELECT 1 FROM webhook_deliveries WHERE endpoint_id = ?`, |
| 98 | + args: [id], |
| 99 | + }); |
| 100 | + assert.equal(endpoints.rows.length, 0); |
| 101 | + assert.equal(deliveries.rows.length, 0); |
| 102 | +}); |
0 commit comments