Cotton code
78 lines · 5.1 KB · TypeScriptRaw
/** * THE NAME AFTER THE CODE (NEXT-SIGNIN, owner, 28 Nov 2026). The code step asked "Your name" even of a number that * already had an account. Now a phone that says `name_after` sends the code alone: the code is proved first, a number * with no account is answered `name_needed` (nothing made), and the same device sends the same code again with the * name. A number with an account goes straight in. The code's answer is the same for every number (audit A2), and a * phone from before (the name with the code) signs in as it always did. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { ADMIN, DATABASE_ID, http, OWNER_PHONE, PERSONAL_TABLES, PERSONAL_TOOLS } from "./personal.ts";const personFor = (slug: string, phone: string) =>  runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance: unknown) => (instance as { store: { personByPhone(p: string): { name: string | null } | null } }).store.personByPhone(phone));/** A personal database nobody has signed in to yet: the owner's number has no account in it. */async function fresh(slug: string): Promise<void> {  expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200);  expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: PERSONAL_TABLES, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200);}type Answer = { name_needed?: boolean; credential?: string; person?: { role: string; name: string | null }; code?: string };const code = async (slug: string, device: string) => (await http<{ challenge_id: string; known?: boolean }>(slug, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: device })).body;const signIn = (slug: string, body: Record<string, unknown>) => http<Answer>(slug, "POST", "/v1/sign-in", body);describe("the name is asked after the code, and only of a new account", () => {  it("a new number proves its code, is asked its name, and is made with it; then it goes straight in", async () => {    const slug = "name-after-new";    await fresh(slug);    const first = await code(slug, "phone-a");    expect(first.known).toBe(false);    const proved = await signIn(slug, { challenge_id: first.challenge_id, code: "123456", device_id: "phone-a", name_after: true });    expect(proved.status, JSON.stringify(proved.body)).toBe(200);    expect(proved.body).toEqual({ name_needed: true });    expect(await personFor(slug, OWNER_PHONE)).toBeNull();    // A wrong code is still wrong: the name step never stands in for the code.    const wrong = await signIn(slug, { challenge_id: first.challenge_id, code: "654321", device_id: "phone-a", name_after: true, name: "Asha Invented" });    expect(wrong.status).toBe(401);    // Another device cannot use the proof.    const other = await signIn(slug, { challenge_id: first.challenge_id, code: "123456", device_id: "phone-b", name_after: true, name: "Asha Invented" });    expect(other.status).toBe(401);    const named = await signIn(slug, { challenge_id: first.challenge_id, code: "123456", device_id: "phone-a", name_after: true, name: "Asha Invented" });    expect(named.status, JSON.stringify(named.body)).toBe(200);    expect(named.body.credential).toBeTruthy();    expect(named.body.person?.role).toBe("owner");    expect((await personFor(slug, OWNER_PHONE))?.name).toBe("Asha Invented");    // The proof is spent once the account is made: the same code again is refused.    expect((await signIn(slug, { challenge_id: first.challenge_id, code: "123456", device_id: "phone-a", name_after: true, name: "Other Name" })).status).toBe(401);    // The account stands: the next sign-in, on another phone, goes straight in with no name asked.    const second = await code(slug, "phone-c");    expect(second.known).toBe(false);    const straight = await signIn(slug, { challenge_id: second.challenge_id, code: "123456", device_id: "phone-c", name_after: true });    expect(straight.status).toBe(200);    expect(straight.body.name_needed).toBeUndefined();    expect(straight.body.credential).toBeTruthy();    expect((await personFor(slug, OWNER_PHONE))?.name).toBe("Asha Invented");  });  it("a phone from before sends the name with the code and signs in as it did", async () => {    const slug = "name-after-old";    await fresh(slug);    const first = await code(slug, "old-phone");    const signed = await signIn(slug, { challenge_id: first.challenge_id, code: "123456", device_id: "old-phone", name: "Ravi Invented" });    expect(signed.status).toBe(200);    expect(signed.body.credential).toBeTruthy();    expect((await personFor(slug, OWNER_PHONE))?.name).toBe("Ravi Invented");    // And with no name at all, as an old build whose field was left empty could.    const slug2 = "name-after-old-blank";    await fresh(slug2);    const blank = await code(slug2, "old-phone");    const bare = await signIn(slug2, { challenge_id: blank.challenge_id, code: "123456", device_id: "old-phone" });    expect(bare.status).toBe(200);    expect(bare.body.credential).toBeTruthy();  });});