import assert from "node:assert/strict"; import { timingSafeEqual } from "node:crypto"; import { test } from "node:test"; import { SESSION_COOKIE } from "../contract/limits.ts"; import { d1, migrated } from "./d1.ts"; Object.assign(crypto.subtle, { timingSafeEqual: (a: ArrayBufferView, b: ArrayBufferView) => timingSafeEqual(a, b) }); const { createBackend } = await import("../index.ts"); function setup() { const sqlite = migrated(); const sent: { to: string; text: string }[] = []; const env = { DB: d1(sqlite), LOGIN_LIMITER: { limit: async () => ({ success: true }) }, EMAIL: { send: async (message: { to: string; text: string }) => void sent.push(message) }, EMAIL_FROM: "noreply@refs.test", PUBLIC_ORIGIN: "https://refs.test", ADMIN_EMAILS: "admin@refs.test", } as unknown as Env; const pending: Promise[] = []; const ctx = { waitUntil: (promise: Promise) => pending.push(promise) } as unknown as ExecutionContext; const backend = createBackend(env, ctx); const refsAs = (token: string) => backend.refs(new Request("https://refs.test/", { headers: { cookie: `${SESSION_COOKIE}=${token}` } })); return { sqlite, sent, pending, gate: backend.gate(), refsAs }; } test("sign-up opens the account at once, and a reset link sets a new password one time", async () => { const { sqlite, sent, pending, gate, refsAs } = setup(); const signedUp = await gate.signUp({ name: "Dana", handle: "dana", email: "Dana@Example.com", password: "first-password", ip: "1" }); assert.ok(signedUp.ok); assert.equal((await refsAs(signedUp.value.token)).viewer.person?.email, "dana@example.com"); assert.equal(sqlite.prepare("SELECT count(*) AS n FROM namespaces WHERE slug = 'dana' AND kind = 'user'").get()!.n, 1); assert.ok((await gate.requestPasswordReset({ email: "nobody@example.com", ip: "1" })).ok); assert.ok((await gate.requestPasswordReset({ email: "dana@example.com", ip: "1" })).ok); await Promise.all(pending); const link = sent.find((mail) => mail.to === "dana@example.com")!.text.match(/reset-password\/([0-9a-f]+)/)!; assert.equal(sent.filter((mail) => mail.to === "nobody@example.com").length, 0); const reset = await gate.resetPassword({ token: link[1], password: "second-password" }); assert.ok(reset.ok); assert.equal((await refsAs(signedUp.value.token)).viewer.person, null); assert.ok((await gate.signIn({ email: "dana@example.com", password: "second-password", ip: "1" })).ok); assert.equal((await gate.resetPassword({ token: link[1], password: "third-password" })).ok, false); sqlite.close(); }); test("a person left on the old waitlist gets their namespace when they sign in", async () => { const { sqlite, gate } = setup(); const signedUp = await gate.signUp({ name: "Eve", handle: "eve", email: "eve@example.com", password: "eve-password", ip: "1" }); assert.ok(signedUp.ok); sqlite.exec("DELETE FROM memberships; DELETE FROM namespaces; UPDATE users SET status = 'waitlisted'"); assert.ok((await gate.signIn({ email: "eve@example.com", password: "eve-password", ip: "1" })).ok); assert.equal(sqlite.prepare("SELECT status FROM users").get()!.status, "active"); assert.equal(sqlite.prepare("SELECT count(*) AS n FROM namespaces WHERE slug = 'eve'").get()!.n, 1); sqlite.close(); }); test("only admins open the overview, and it counts people and 30 days", async () => { const { sqlite, gate, refsAs } = setup(); const admin = await gate.signUp({ name: "Admin", handle: "boss", email: "admin@refs.test", password: "admin-password", ip: "1" }); const person = await gate.signUp({ name: "Finn", handle: "finn", email: "finn@example.com", password: "finn-password", ip: "1" }); assert.ok(admin.ok && person.ok); await assert.rejects((await refsAs(person.value.token)).adminOverview(), /not_found/); const overview = await (await refsAs(admin.value.token)).adminOverview(); assert.equal(overview.totals.people, 2); assert.equal(overview.totals.signups30d, 2); assert.equal(overview.days.length, 30); assert.equal(overview.days.at(-1)!.signups, 2); assert.deepEqual(overview.people.map((row) => row.handle).sort(), ["boss", "finn"]); sqlite.close(); });