Skip to content
refs
History
74 lines · 4.1 KBRaw
1import assert from "node:assert/strict";
2import { timingSafeEqual } from "node:crypto";
3import { test } from "node:test";
4import { SESSION_COOKIE } from "../contract/limits.ts";
5import { d1, migrated } from "./d1.ts";
6
7Object.assign(crypto.subtle, { timingSafeEqual: (a: ArrayBufferView, b: ArrayBufferView) => timingSafeEqual(a, b) });
8
9const { createBackend } = await import("../index.ts");
10
11function setup() {
12 const sqlite = migrated();
13 const sent: { to: string; text: string }[] = [];
14 const env = {
15 DB: d1(sqlite),
16 LOGIN_LIMITER: { limit: async () => ({ success: true }) },
17 EMAIL: { send: async (message: { to: string; text: string }) => void sent.push(message) },
18 EMAIL_FROM: "noreply@refs.test",
19 PUBLIC_ORIGIN: "https://refs.test",
20 ADMIN_EMAILS: "admin@refs.test",
21 } as unknown as Env;
22 const pending: Promise<unknown>[] = [];
23 const ctx = { waitUntil: (promise: Promise<unknown>) => pending.push(promise) } as unknown as ExecutionContext;
24 const backend = createBackend(env, ctx);
25 const refsAs = (token: string) => backend.refs(new Request("https://refs.test/", { headers: { cookie: `${SESSION_COOKIE}=${token}` } }));
26 return { sqlite, sent, pending, gate: backend.gate(), refsAs };
27}
28
29test("sign-up opens the account at once, and a reset link sets a new password one time", async () => {
30 const { sqlite, sent, pending, gate, refsAs } = setup();
31 const signedUp = await gate.signUp({ name: "Dana", handle: "dana", email: "Dana@Example.com", password: "first-password", ip: "1" });
32 assert.ok(signedUp.ok);
33 assert.equal((await refsAs(signedUp.value.token)).viewer.person?.email, "dana@example.com");
34 assert.equal(sqlite.prepare("SELECT count(*) AS n FROM namespaces WHERE slug = 'dana' AND kind = 'user'").get()!.n, 1);
35
36 assert.ok((await gate.requestPasswordReset({ email: "nobody@example.com", ip: "1" })).ok);
37 assert.ok((await gate.requestPasswordReset({ email: "dana@example.com", ip: "1" })).ok);
38 await Promise.all(pending);
39 const link = sent.find((mail) => mail.to === "dana@example.com")!.text.match(/reset-password\/([0-9a-f]+)/)!;
40 assert.equal(sent.filter((mail) => mail.to === "nobody@example.com").length, 0);
41
42 const reset = await gate.resetPassword({ token: link[1], password: "second-password" });
43 assert.ok(reset.ok);
44 assert.equal((await refsAs(signedUp.value.token)).viewer.person, null);
45 assert.ok((await gate.signIn({ email: "dana@example.com", password: "second-password", ip: "1" })).ok);
46 assert.equal((await gate.resetPassword({ token: link[1], password: "third-password" })).ok, false);
47 sqlite.close();
48});
49
50test("a person left on the old waitlist gets their namespace when they sign in", async () => {
51 const { sqlite, gate } = setup();
52 const signedUp = await gate.signUp({ name: "Eve", handle: "eve", email: "eve@example.com", password: "eve-password", ip: "1" });
53 assert.ok(signedUp.ok);
54 sqlite.exec("DELETE FROM memberships; DELETE FROM namespaces; UPDATE users SET status = 'waitlisted'");
55 assert.ok((await gate.signIn({ email: "eve@example.com", password: "eve-password", ip: "1" })).ok);
56 assert.equal(sqlite.prepare("SELECT status FROM users").get()!.status, "active");
57 assert.equal(sqlite.prepare("SELECT count(*) AS n FROM namespaces WHERE slug = 'eve'").get()!.n, 1);
58 sqlite.close();
59});
60
61test("only admins open the overview, and it counts people and 30 days", async () => {
62 const { sqlite, gate, refsAs } = setup();
63 const admin = await gate.signUp({ name: "Admin", handle: "boss", email: "admin@refs.test", password: "admin-password", ip: "1" });
64 const person = await gate.signUp({ name: "Finn", handle: "finn", email: "finn@example.com", password: "finn-password", ip: "1" });
65 assert.ok(admin.ok && person.ok);
66 await assert.rejects((await refsAs(person.value.token)).adminOverview(), /not_found/);
67 const overview = await (await refsAs(admin.value.token)).adminOverview();
68 assert.equal(overview.totals.people, 2);
69 assert.equal(overview.totals.signups30d, 2);
70 assert.equal(overview.days.length, 30);
71 assert.equal(overview.days.at(-1)!.signups, 2);
72 assert.deepEqual(overview.people.map((row) => row.handle).sort(), ["boss", "finn"]);
73 sqlite.close();
74});