Public: anyone can read this; the crew can change it.
A test pins an app's custom domains in Settings: add, check, delete, a repository admin's only #7042
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/7023-a-test-pins-an-app-s-custom-domains-in-s → main 1 commit fork 1 thread · 1 unresolved
1 file
changed +162
−0
test/settings-app-domains.test.ts added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | /** An app's custom domains in Settings: add, check, delete — a repository admin's only (#7023). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 8 | import type { DomainResolver } from "../src/apps/domains.ts"; | |
| 9 | import { createApp as insertApp } from "../src/core/apps.ts"; | |
| 10 | import { getDomain } from "../src/core/domains.ts"; | |
| 11 | import { addTeamMember, createOrg, createTeam } from "../src/core/orgs.ts"; | |
| 12 | import { createRepo } from "../src/core/repos.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | const HOST = "shop.example.com"; | |
| 20 | const PROOF = `_forge-verify.${HOST}`; | |
| 21 | ||
| 22 | class FakeDns implements DomainResolver { | |
| 23 | readonly txt = new Map<string, string[][]>(); | |
| 24 | resolveTxt(name: string): Promise<string[][]> { | |
| 25 | const found = this.txt.get(name); | |
| 26 | return found === undefined | |
| 27 | ? Promise.reject(Object.assign(new Error("no data"), { code: "ENODATA" })) | |
| 28 | : Promise.resolve(found); | |
| 29 | } | |
| 30 | resolveCname(): Promise<string[]> { | |
| 31 | return Promise.reject(Object.assign(new Error("no data"), { code: "ENODATA" })); | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | let dataDir = ""; | |
| 36 | let app: App, servers: Servers, runtime: AppsRuntime; | |
| 37 | const dns = new FakeDns(); | |
| 38 | const cookies: Record<string, string> = {}; | |
| 39 | ||
| 40 | const signIn = async (username: string): Promise<string> => { | |
| 41 | const res = await fetch(`${servers.url}/signin`, { | |
| 42 | method: "POST", | |
| 43 | body: new URLSearchParams({ username, password: "a-good-passphrase" }), | |
| 44 | redirect: "manual", | |
| 45 | }); | |
| 46 | return (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 47 | }; | |
| 48 | ||
| 49 | /** The form as the page posts it: session cookie, same-origin Origin. */ | |
| 50 | const post = (who: string, path: string, form: Record<string, string> = {}, origin?: string) => | |
| 51 | fetch(`${servers.url}${path}`, { | |
| 52 | method: "POST", | |
| 53 | headers: { cookie: cookies[who]!, origin: origin ?? new URL(servers.url).origin }, | |
| 54 | body: new URLSearchParams(form), | |
| 55 | redirect: "manual", | |
| 56 | }); | |
| 57 | const flash = (res: Response): string => { | |
| 58 | const raw = /forge_flash=([^;]*)/.exec(res.headers.get("set-cookie") ?? "")?.[1] ?? ""; | |
| 59 | return raw === "" ? "" : (JSON.parse(decodeURIComponent(raw)) as { message: string }).message; | |
| 60 | }; | |
| 61 | const DOMAINS = "/acme/shop/settings/app/domains"; | |
| 62 | const rows = () => | |
| 63 | app.db.query<{ host: string }, []>("SELECT host FROM app_domain").all().map((r) => r.host); | |
| 64 | ||
| 65 | beforeAll(async () => { | |
| 66 | dataDir = await mkdtemp(join(tmpdir(), "forge-set-domains-")); | |
| 67 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 68 | servers = await serve(app); | |
| 69 | runtime = new AppsRuntime(app, { | |
| 70 | domain: "apps.test", | |
| 71 | scheme: "http", | |
| 72 | publicPort: servers.port, | |
| 73 | forgeUrl: servers.url, | |
| 74 | concurrency: 1, | |
| 75 | dns, | |
| 76 | }); | |
| 77 | await runtime.start(); | |
| 78 | const make = async (username: string) => { | |
| 79 | const u = await createUser(app.db, { | |
| 80 | origin: "seed", | |
| 81 | trusted: true, | |
| 82 | username, | |
| 83 | email: `${username}@forge.local`, | |
| 84 | password: "a-good-passphrase", | |
| 85 | adminIfFirst: true, | |
| 86 | }); | |
| 87 | if (!u.ok) throw new Error(u.error); | |
| 88 | return u.value; | |
| 89 | }; | |
| 90 | const founder = await make("founder"); | |
| 91 | const dev = await make("dev"); | |
| 92 | await make("mallory"); | |
| 93 | const org = createOrg(app.db, "acme", founder.id); | |
| 94 | if (!org.ok) throw new Error("org"); | |
| 95 | const writers = createTeam(app.db, org.value.id, "Writers", "write"); | |
| 96 | if (!writers.ok) throw new Error("team"); | |
| 97 | addTeamMember(app.db, writers.value, dev.id); | |
| 98 | for (const [name, visibility] of [["shop", "public"], ["vault", "private"]] as const) { | |
| 99 | const repo = await createRepo(app.db, app.reposRoot, { ownerId: org.value.id, name, visibility }); | |
| 100 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 101 | const row = insertApp(app.db, { repoId: repo.value.id, slug: name, access: "public" }); | |
| 102 | if (!row.ok) throw new Error(row.error); | |
| 103 | } | |
| 104 | for (const who of ["founder", "dev", "mallory"]) cookies[who] = await signIn(who); | |
| 105 | }, 30_000); | |
| 106 | ||
| 107 | afterAll(async () => { | |
| 108 | await runtime?.stop(); | |
| 109 | servers?.stop(); | |
| 110 | app?.close(); | |
| 111 | if (dataDir) await rm(dataDir, { recursive: true, force: true }); | |
| 112 | }); | |
| 113 | ||
| 114 | test("an admin adds a domain, checks it before and after the proof, and deletes it", async () => { | |
| 115 | const added = await post("founder", DOMAINS, { host: HOST }); | |
| 116 | expect(added.status).toBe(303); | |
| 117 | expect(added.headers.get("location")).toBe("/acme/shop/settings/app"); | |
| 118 | const said = flash(added); | |
| 119 | expect(said).toContain(`${HOST} is added`); | |
| 120 | expect(said).toContain(`a TXT record at ${PROOF}`); | |
| 121 | expect(rows()).toEqual([HOST]); | |
| 122 | ||
| 123 | const early = flash(await post("founder", `${DOMAINS}/${HOST}/check`)); | |
| 124 | expect(early).not.toBe(""); | |
| 125 | expect(getDomain(app.db, HOST)?.verifiedAt).toBeNull(); | |
| 126 | ||
| 127 | dns.txt.set(PROOF, [[`forge-verify=${getDomain(app.db, HOST)!.token}`]]); | |
| 128 | await post("founder", `${DOMAINS}/${HOST}/check`); | |
| 129 | expect(getDomain(app.db, HOST)?.verifiedAt).not.toBeNull(); | |
| 130 | ||
| 131 | const gone = await post("founder", `${DOMAINS}/${HOST}/delete`); | |
| 132 | expect(flash(gone)).toBe(`${HOST} is removed. The forge no longer answers there.`); | |
| 133 | expect(rows()).toEqual([]); | |
| 134 | expect(flash(await post("founder", `${DOMAINS}/${HOST}/delete`))).toBe("No domain by that name."); | |
| 135 | }); | |
| 136 | ||
| 137 | test("a host that is not a hostname is refused with the page's words", async () => { | |
| 138 | const words = "A domain is a hostname: letters, digits and hyphens in at least two labels, like app.example.com."; | |
| 139 | for (const host of ["a b", "http://x", "192.168.0.1", "localhost", ""]) { | |
| 140 | expect(flash(await post("founder", DOMAINS, { host }))).toBe(words); | |
| 141 | } | |
| 142 | expect(flash(await post("founder", DOMAINS, { host: "forge.test" }))).toBe( | |
| 143 | "That name is the forge's own to hand out.", | |
| 144 | ); | |
| 145 | expect(rows()).toEqual([]); | |
| 146 | }); | |
| 147 | ||
| 148 | test("a cross-origin post is refused and adds nothing", async () => { | |
| 149 | const res = await post("founder", DOMAINS, { host: HOST }, "https://evil.example"); | |
| 150 | expect(res.status).toBe(403); | |
| 151 | expect(rows()).toEqual([]); | |
| 152 | }); | |
| 153 | ||
| 154 | test("a writer who is not an admin is refused, as is a stranger who cannot read the repository: both are told not found", async () => { | |
| 155 | // The settings door tells a non-admin nothing: the same answer as no repository. | |
| 156 | for (const path of [DOMAINS, `${DOMAINS}/${HOST}/check`, `${DOMAINS}/${HOST}/delete`]) { | |
| 157 | expect((await post("dev", path, { host: HOST })).status).toBe(404); | |
| 158 | } | |
| 159 | const hidden = await post("mallory", "/acme/vault/settings/app/domains", { host: HOST }); | |
| 160 | expect(hidden.status).toBe(404); | |
| 161 | expect(rows()).toEqual([]); | |
| 162 | }); |
new above
offline
After the first delete, the second assertion depends on the earlier flash; fine, but add one post as the founder with an over-long host (more than 253 bytes) to pin the length cap as well.