|
| 1 | +/** |
| 2 | + * Auth-collection signup paths fire the same hook lifecycle as the |
| 3 | + * records flow: |
| 4 | + * - /register → beforeCreate + afterCreate |
| 5 | + * - /anonymous → afterCreate (no body to validate) |
| 6 | + * - oauth signup → afterCreate |
| 7 | + * |
| 8 | + * Pre-v0.11.1 the auth path bypassed core/records.ts and never hit |
| 9 | + * runBeforeHook/runAfterHook. This regression test pins the new |
| 10 | + * behaviour. |
| 11 | + */ |
| 12 | +import { describe, expect, it, beforeEach, afterEach } from "bun:test"; |
| 13 | +import { mkdtempSync, rmSync } from "fs"; |
| 14 | +import { tmpdir } from "os"; |
| 15 | +import { join } from "path"; |
| 16 | + |
| 17 | +import { initDb, closeDb, getDb } from "../db/client.ts"; |
| 18 | +import { runMigrations } from "../db/migrate.ts"; |
| 19 | +import { setLogsDir } from "../core/file-logger.ts"; |
| 20 | +import { hooks as hooksTable } from "../db/schema.ts"; |
| 21 | +import { invalidateHookCache } from "../core/hooks.ts"; |
| 22 | +import { createCollection } from "../core/collections.ts"; |
| 23 | +import { makeAuthPlugin } from "../api/auth.ts"; |
| 24 | + |
| 25 | +const SECRET = "test-secret-auth-hooks"; |
| 26 | +let tmpDir: string; |
| 27 | + |
| 28 | +beforeEach(async () => { |
| 29 | + tmpDir = mkdtempSync(join(tmpdir(), "vaultbase-auth-hooks-")); |
| 30 | + setLogsDir(tmpDir); |
| 31 | + initDb(":memory:"); |
| 32 | + await runMigrations(); |
| 33 | +}); |
| 34 | + |
| 35 | +afterEach(() => { |
| 36 | + invalidateHookCache(); |
| 37 | + closeDb(); |
| 38 | + try { rmSync(tmpDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 50 }); } catch { /* swallow */ } |
| 39 | +}); |
| 40 | + |
| 41 | +async function installHook(collection: string, event: string, code: string): Promise<void> { |
| 42 | + const now = Math.floor(Date.now() / 1000); |
| 43 | + await getDb().insert(hooksTable).values({ |
| 44 | + id: crypto.randomUUID(), |
| 45 | + name: `${collection}-${event}`, |
| 46 | + collection_name: collection, |
| 47 | + event, |
| 48 | + code, |
| 49 | + enabled: 1, |
| 50 | + created_at: now, |
| 51 | + updated_at: now, |
| 52 | + }); |
| 53 | + invalidateHookCache(); |
| 54 | +} |
| 55 | + |
| 56 | +/** Wait for the after-hook fire-and-forget microtask to settle. */ |
| 57 | +async function flushAfterHook(): Promise<void> { |
| 58 | + await new Promise((r) => setTimeout(r, 50)); |
| 59 | +} |
| 60 | + |
| 61 | +describe("auth signup hook lifecycle", () => { |
| 62 | + it("/register fires beforeCreate + afterCreate", async () => { |
| 63 | + await createCollection({ |
| 64 | + name: "users", type: "auth", |
| 65 | + fields: JSON.stringify([{ name: "marker", type: "text" }]), |
| 66 | + view_rule: null, |
| 67 | + }); |
| 68 | + // Mark hooks via globalThis side channel — cheap way to assert |
| 69 | + // they ran without DOM/spies. |
| 70 | + (globalThis as Record<string, unknown>)["_beforeHits"] = 0; |
| 71 | + (globalThis as Record<string, unknown>)["_afterHits"] = 0; |
| 72 | + await installHook("users", "beforeCreate", `globalThis._beforeHits = (globalThis._beforeHits || 0) + 1;`); |
| 73 | + await installHook("users", "afterCreate", `globalThis._afterHits = (globalThis._afterHits || 0) + 1;`); |
| 74 | + |
| 75 | + const app = makeAuthPlugin(SECRET); |
| 76 | + const res = await app.handle(new Request("http://localhost/auth/users/register", { |
| 77 | + method: "POST", |
| 78 | + headers: { "content-type": "application/json" }, |
| 79 | + body: JSON.stringify({ email: "alice@x.com", password: "hunter2!!hunter2!!" }), |
| 80 | + })); |
| 81 | + expect(res.status).toBe(200); |
| 82 | + await flushAfterHook(); |
| 83 | + expect((globalThis as Record<string, unknown>)["_beforeHits"]).toBe(1); |
| 84 | + expect((globalThis as Record<string, unknown>)["_afterHits"]).toBe(1); |
| 85 | + }); |
| 86 | + |
| 87 | + it("/anonymous fires afterCreate", async () => { |
| 88 | + await createCollection({ |
| 89 | + name: "users", type: "auth", |
| 90 | + fields: JSON.stringify([]), view_rule: null, |
| 91 | + }); |
| 92 | + // Anonymous auth is opt-in by default — enable for this test. |
| 93 | + const { settings } = await import("../db/schema.ts"); |
| 94 | + await getDb().insert(settings).values({ key: "auth.anonymous.enabled", value: "1", updated_at: Math.floor(Date.now() / 1000) }); |
| 95 | + |
| 96 | + (globalThis as Record<string, unknown>)["_anonHits"] = 0; |
| 97 | + await installHook("users", "afterCreate", `globalThis._anonHits = (globalThis._anonHits || 0) + 1;`); |
| 98 | + |
| 99 | + const app = makeAuthPlugin(SECRET); |
| 100 | + const res = await app.handle(new Request("http://localhost/auth/users/anonymous", { method: "POST" })); |
| 101 | + expect(res.status).toBe(200); |
| 102 | + await flushAfterHook(); |
| 103 | + expect((globalThis as Record<string, unknown>)["_anonHits"]).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it("global hooks (collection_name='') fire on auth signup too", async () => { |
| 107 | + await createCollection({ |
| 108 | + name: "users", type: "auth", |
| 109 | + fields: JSON.stringify([]), view_rule: null, |
| 110 | + }); |
| 111 | + (globalThis as Record<string, unknown>)["_globalHits"] = 0; |
| 112 | + await installHook("", "afterCreate", `globalThis._globalHits = (globalThis._globalHits || 0) + 1;`); |
| 113 | + |
| 114 | + const app = makeAuthPlugin(SECRET); |
| 115 | + await app.handle(new Request("http://localhost/auth/users/register", { |
| 116 | + method: "POST", |
| 117 | + headers: { "content-type": "application/json" }, |
| 118 | + body: JSON.stringify({ email: "bob@x.com", password: "hunter2!!hunter2!!" }), |
| 119 | + })); |
| 120 | + await flushAfterHook(); |
| 121 | + expect((globalThis as Record<string, unknown>)["_globalHits"]).toBe(1); |
| 122 | + }); |
| 123 | +}); |
0 commit comments