Public: anyone can read this; the crew can change it.
A claim link that was already used says so in plain words and where to sign in #14692
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14679-a-claim-link-that-was-already-used-says → main 3 commits fork
5 files
changed +117
−15
src/web/pages/claim.ts +5 −4
| @@ -132,9 +132,11 @@ function dead( | ||
| 132 | 132 | const says = { |
| 133 | 133 | unknown: gone, |
| 134 | 134 | used: { |
| 135 | status: 410, | |
| 136 | title: "This link was already used", | |
| 137 | text: `If it was you, sign in; if not, ask ${ask} to send you a new one.`, | |
| 135 | // A second click is not an error: the person is told it is theirs and where to sign in. | |
| 136 | // (A POST that tried to claim with it again stays 410: it did not succeed.) | |
| 137 | status: ctx.request.method === "GET" ? 200 : 410, | |
| 138 | title: "This forge is already yours", | |
| 139 | text: "You already used this link to make your account. Sign in to carry on.", | |
| 138 | 140 | }, |
| 139 | 141 | expired: { |
| 140 | 142 | status: 410, |
| @@ -155,7 +157,6 @@ function dead( | ||
| 155 | 157 | // The person who started this forge themselves has no sender to ask. |
| 156 | 158 | const mine: Partial<Record<typeof state, string>> = { |
| 157 | 159 | unknown: "This link does not work any more. Start the forge again from the home page.", |
| 158 | used: "If it was you, sign in. Otherwise start a forge of your own from the home page.", | |
| 159 | 160 | expired: "Start the forge again from the home page.", |
| 160 | 161 | revoked: "You withdrew this link. Start the forge again from the home page.", |
| 161 | 162 | }; |
test/claim-page-words.test.ts +6 −5
| @@ -116,13 +116,14 @@ describe("the claim page in plain words", () => { | ||
| 116 | 116 | } |
| 117 | 117 | }); |
| 118 | 118 | |
| 119 | test("a used link answers 410 in plain words", async () => { | |
| 119 | test("a used link answers 200: it is already yours, and where to sign in", async () => { | |
| 120 | 120 | setStored({ e: new Date(Date.now() + 3_600_000).toISOString(), u: new Date().toISOString() }); |
| 121 | 121 | const res = await get(); |
| 122 | expect(res.status).toBe(410); | |
| 122 | expect(res.status).toBe(200); | |
| 123 | 123 | const text = await res.text(); |
| 124 | expect(text).toContain("This link was already used"); | |
| 125 | expect(text).toContain("ask alice to send you a new one"); | |
| 124 | expect(text).toContain("This forge is already yours"); | |
| 125 | expect(text).toContain('href="/signin"'); | |
| 126 | expect(text).not.toContain("alice"); | |
| 126 | 127 | expect(text).not.toMatch(/token|\b4\d\d\b/i); |
| 127 | 128 | }); |
| 128 | 129 | |
| @@ -136,7 +137,7 @@ describe("the claim page in plain words", () => { | ||
| 136 | 137 | const res = await fetch(`${childServers.url}/claim`, { headers: { cookie }, redirect: "manual" }); |
| 137 | 138 | expect(res.status).toBe(303); |
| 138 | 139 | expect(res.headers.get("location")).toBe("/"); |
| 139 | expect((await get()).status).toBe(410); | |
| 140 | expect((await get()).status).toBe(200); | |
| 140 | 141 | }); |
| 141 | 142 | |
| 142 | 143 | test("a made-up code still only meets the not-a-forge-link answer", async () => { |
test/claim-used-link.test.ts added +103
| @@ -0,0 +1,103 @@ | ||
| 1 | /** | |
| 2 | * A claim link opened twice is not an error (#14679): a used one says the forge | |
| 3 | * is already yours and where to sign in; expired and unknown ones say so | |
| 4 | * differently, and an unknown one names no one. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { childDirOf } from "../src/actions/spawn.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { firstBoot } from "../src/spawn/first-boot.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | describe("a claim link opened again", () => { | |
| 21 | let dir = ""; | |
| 22 | let parent: App; | |
| 23 | let ps: Servers; | |
| 24 | let child: App; | |
| 25 | let cs: Servers; | |
| 26 | let token = ""; | |
| 27 | ||
| 28 | const get = () => fetch(`${cs.url}/claim`); | |
| 29 | const post = (t: string) => | |
| 30 | fetch(`${cs.url}/claim`, { method: "POST", headers: { origin: cs.url }, body: new URLSearchParams({ token: t }) }); | |
| 31 | const setStored = (patch: Record<string, unknown>) => { | |
| 32 | const row = child.db.query<{ value: string }, []>("SELECT value FROM user_setting WHERE key = 'spawn_claim'").get(); | |
| 33 | child.db | |
| 34 | .query("UPDATE user_setting SET value = ?1 WHERE key = 'spawn_claim'") | |
| 35 | .run(JSON.stringify({ ...JSON.parse(row!.value), ...patch })); | |
| 36 | }; | |
| 37 | const hour = 3_600_000; | |
| 38 | ||
| 39 | beforeAll(async () => { | |
| 40 | dir = await mkdtemp(join(tmpdir(), "forge-claim-used-")); | |
| 41 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 42 | const alice = await createUser(parent.db, { | |
| 43 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 44 | password: "a-long-claim-passphrase", isAdmin: true, | |
| 45 | }); | |
| 46 | if (!alice.ok) throw new Error("user"); | |
| 47 | const rw = createToken(parent.db, alice.value.id, "rw", "rw").token; | |
| 48 | ps = await serve(parent); | |
| 49 | const made = await fetch(`${ps.url}/api/v1/forges`, { | |
| 50 | method: "POST", | |
| 51 | headers: { authorization: `Bearer ${rw}`, "content-type": "application/json" }, | |
| 52 | body: JSON.stringify({ name: "maria", apps: [], dryRun: false }), | |
| 53 | }); | |
| 54 | const body = (await made.json()) as { claimUrl: string; bodyId: string }; | |
| 55 | token = body.claimUrl.split("#")[1] ?? ""; | |
| 56 | child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); | |
| 57 | await firstBoot(child); | |
| 58 | cs = await serve(child); | |
| 59 | }, 60_000); | |
| 60 | ||
| 61 | afterAll(async () => { | |
| 62 | cs.stop(); | |
| 63 | child.close(); | |
| 64 | ps.stop(); | |
| 65 | parent.close(); | |
| 66 | await rm(dir, { recursive: true, force: true }); | |
| 67 | }); | |
| 68 | ||
| 69 | test("fresh: the form", async () => { | |
| 70 | const res = await get(); | |
| 71 | expect(res.status).toBe(200); | |
| 72 | expect(await res.text()).toContain("This forge is yours"); | |
| 73 | }); | |
| 74 | ||
| 75 | test("used: already yours, with a sign-in link, no error, no names", async () => { | |
| 76 | setStored({ e: new Date(Date.now() + hour).toISOString(), u: new Date().toISOString() }); | |
| 77 | const res = await get(); | |
| 78 | expect(res.status).toBe(200); | |
| 79 | const text = await res.text(); | |
| 80 | expect(text).toContain("This forge is already yours"); | |
| 81 | expect(text).toContain("Sign in to carry on"); | |
| 82 | expect(text).not.toContain("alice"); | |
| 83 | // Posting it again claims nothing. | |
| 84 | expect((await post(token)).status).toBe(410); | |
| 85 | }); | |
| 86 | ||
| 87 | test("expired: says so differently, and offers a new link, not sign-in", async () => { | |
| 88 | setStored({ e: new Date(Date.now() - hour).toISOString(), u: null }); | |
| 89 | const text = await (await get()).text(); | |
| 90 | expect(text).toContain("This link has expired"); | |
| 91 | expect(text).not.toContain("already yours"); | |
| 92 | expect(text).not.toContain("Sign in to carry on"); | |
| 93 | }); | |
| 94 | ||
| 95 | test("unknown: a made-up code is a plain 404 that names no one", async () => { | |
| 96 | const res = await post("x".repeat(43)); | |
| 97 | expect(res.status).toBe(404); | |
| 98 | const text = await res.text(); | |
| 99 | expect(text).toContain("This link does not work"); | |
| 100 | expect(text).not.toMatch(/alice|maria|already yours|expired/); | |
| 101 | expect((await post("short")).status).toBe(404); | |
| 102 | }); | |
| 103 | }); |
test/forge-link-landing.test.ts +2 −5
| @@ -138,11 +138,8 @@ describe("a forge link's landing page", () => { | ||
| 138 | 138 | expect(main(expired).match(/has expired/g)?.length).toBe(1); |
| 139 | 139 | setStored({ e: future(), u: new Date().toISOString() }); |
| 140 | 140 | const used = await (await get()).text(); |
| 141 | expect(used).toContain("This link was already used"); | |
| 142 | expect(main(used).match(/already used/g)?.length).toBe(1); | |
| 143 | expect(used).toContain("Alice <script>"); | |
| 144 | sendersName(""); | |
| 145 | expect(await (await get()).text()).toContain("ask alice to send you a new one"); | |
| 141 | expect(used).toContain("This forge is already yours"); | |
| 142 | expect(main(used).match(/already yours/g)?.length).toBe(1); | |
| 146 | 143 | setStored({ u: null }); |
| 147 | 144 | }); |
| 148 | 145 |
test/spawn-first-boot.test.ts +1 −1
| @@ -350,7 +350,7 @@ describe("a spawned child's first boot", () => { | ||
| 350 | 350 | email: "[email protected]", |
| 351 | 351 | }); |
| 352 | 352 | expect(res.status).toBe(410); |
| 353 | expect(await res.text()).toContain("This link was already used"); | |
| 353 | expect(await res.text()).toContain("This forge is already yours"); | |
| 354 | 354 | expect(getUserByName(child.db, "mallory")).toBeUndefined(); |
| 355 | 355 | }); |
| 356 | 356 |
new above
offline