Public: anyone can read this; the crew can change it.
Tests for the stop on a proposal that adds a secret #17758
Merged 7h 19m ago, live in production
sold/leaf-editor-5 → main 0 commits
1 file
changed +200
−0
test/propose-secret-stop.test.ts added +200
| @@ -0,0 +1,200 @@ | ||
| 1 | /** | |
| 2 | * A proposal that adds what looks like a secret stops once and names the key; one tick sends it. | |
| 3 | * The three ways to propose: the web's to another forge, the terminal's to another forge, and the | |
| 4 | * terminal's to a fork here. Two forges of this file's own, and a fake API in front of the other one. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { setRole } from "../src/actions/roles.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { listOrigins } from "../src/core/origins.ts"; | |
| 15 | import { listPulls } from "../src/core/pulls.ts"; | |
| 16 | import { getRepo } from "../src/core/repos.ts"; | |
| 17 | import type { User } from "../src/core/users.ts"; | |
| 18 | import { blobForRender } from "../src/git/read.ts"; | |
| 19 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 20 | import { forgetProviders } from "../src/origins/providers/index.ts"; | |
| 21 | import { createForgeApp } from "../src/tui/forge.ts"; | |
| 22 | import { workspaceScreen } from "../src/tui/screens/workspace.ts"; | |
| 23 | import type { TuiIdentity } from "../src/tui/session.ts"; | |
| 24 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 25 | import { clearViewCache } from "../src/view/cache.ts"; | |
| 26 | import { originSource } from "../src/view/origin.ts"; | |
| 27 | import { newFakeState, serveFakeForge, type FakeForge } from "./fake-origin.ts"; | |
| 28 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 29 | import { quietLogs } from "./helpers/globals.ts"; | |
| 30 | import { waitFor } from "./helpers/poll.ts"; | |
| 31 | import { tokenFor } from "./helpers/tokens.ts"; | |
| 32 | import { settleApp, settleUntil } from "./helpers/tui.ts"; | |
| 33 | import { signUpProven } from "./helpers/verified.ts"; | |
| 34 | ||
| 35 | // A proposal clones and pushes over git; a loaded check machine needs more than 5 s. | |
| 36 | const GIT_MS = 60_000; | |
| 37 | const PASSWORD = "a-long-passphrase"; | |
| 38 | const LANG = "password: \n"; | |
| 39 | ||
| 40 | quietLogs(); | |
| 41 | ||
| 42 | let dirs: string[] = []; | |
| 43 | let app: App; | |
| 44 | let up: App; | |
| 45 | let servers: Servers; | |
| 46 | let upServers: Servers; | |
| 47 | let fake: FakeForge; | |
| 48 | let alice: User; | |
| 49 | let kim: User; | |
| 50 | let cookie = ""; | |
| 51 | let browse = ""; | |
| 52 | ||
| 53 | const pullsSent = (): number => fake.state.requests.filter((r) => r.method === "POST" && r.path.endsWith("/octo/gears/pulls")).length; | |
| 54 | ||
| 55 | const post = (path: string, form: Record<string, string>): Promise<Response> => | |
| 56 | fetch(`${servers.url}${path}`, { method: "POST", headers: { cookie, origin: new URL(servers.url).origin }, body: new URLSearchParams(form), redirect: "manual" }); | |
| 57 | ||
| 58 | async function account(on: App, username: string, admin = false): Promise<User> { | |
| 59 | const made = await (admin ? seedAdmin : signUpProven)(on, { username, email: `${username}@forge.test`, password: PASSWORD }); | |
| 60 | if (!made.ok) throw new Error(`no ${username}`); | |
| 61 | return made.value; | |
| 62 | } | |
| 63 | ||
| 64 | async function repo(on: App, owner: User, name: string): Promise<void> { | |
| 65 | const made = await createRepo(on, owner, { name, visibility: "public" }); | |
| 66 | if (!made.ok) throw new Error(`no ${name}`); | |
| 67 | const seeded = await commitToBranch(on, owner, made.value, { message: "seed", files: [{ path: "lang.yml", content: LANG }] }); | |
| 68 | if (!seeded.ok) throw new Error(`${name} has no files`); | |
| 69 | } | |
| 70 | ||
| 71 | /** The terminal, as one person. */ | |
| 72 | function boot(user: User) { | |
| 73 | const identity: TuiIdentity = { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }; | |
| 74 | const { app: tui, session } = createForgeApp(app, identity, { terminal: headlessTerminal(120, 40), exitHooks: false }); | |
| 75 | tui.start(); | |
| 76 | const press = async (...keys: string[]): Promise<void> => { | |
| 77 | for (const k of keys) tui.dispatch({ type: "key", key: k.replace("ctrl+", ""), ctrl: k.startsWith("ctrl+"), alt: false, shift: false }); | |
| 78 | await settleApp(tui); | |
| 79 | }; | |
| 80 | return { tui, session, press }; | |
| 81 | } | |
| 82 | ||
| 83 | beforeAll(async () => { | |
| 84 | dirs = [await mkdtemp(join(tmpdir(), "forge-stop-")), await mkdtemp(join(tmpdir(), "forge-stop-up-"))]; | |
| 85 | ||
| 86 | up = createApp({ dataDir: dirs[1]!, port: 0 }); | |
| 87 | upServers = await serve(up); | |
| 88 | const octo = await account(up, "octo", true); | |
| 89 | await account(up, "me"); | |
| 90 | if (!setRole(up, octo, "me", "member").ok) throw new Error("role failed"); | |
| 91 | const token = tokenFor(up, "me", "admin"); | |
| 92 | await repo(up, octo, "gears"); | |
| 93 | const forked = await fetch(`${upServers.url}/api/v1/repos/octo/gears/forks`, { | |
| 94 | method: "POST", | |
| 95 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 96 | body: "{}", | |
| 97 | }); | |
| 98 | expect(forked.status).toBe(201); | |
| 99 | ||
| 100 | const state = newFakeState("forgejo", "me", token); | |
| 101 | state.repos.push({ | |
| 102 | id: 1, | |
| 103 | fullName: "octo/gears", | |
| 104 | description: "gears", | |
| 105 | cloneUrl: `${upServers.url}/octo/gears.git`, | |
| 106 | htmlUrl: `${upServers.url}/octo/gears`, | |
| 107 | defaultBranch: "main", | |
| 108 | push: false, | |
| 109 | admin: false, | |
| 110 | }); | |
| 111 | state.files.set("octo/gears", new Map([["lang.yml", LANG]])); | |
| 112 | state.branches.set("octo/gears", [{ name: "main", sha: "a".repeat(40) }]); | |
| 113 | fake = serveFakeForge(state); | |
| 114 | ||
| 115 | app = createApp({ dataDir: dirs[0]!, port: 0 }); | |
| 116 | servers = await serve(app); | |
| 117 | alice = await account(app, "alice", true); | |
| 118 | kim = await account(app, "kim"); | |
| 119 | if (!setRole(app, alice, "kim", "member").ok) throw new Error("role failed"); | |
| 120 | await repo(app, alice, "beacon"); | |
| 121 | const signedIn = await fetch(`${servers.url}/signin`, { method: "POST", headers: { origin: new URL(servers.url).origin }, body: new URLSearchParams({ username: "alice", password: PASSWORD }), redirect: "manual" }); | |
| 122 | cookie = (signedIn.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 123 | forgetProviders(dirs[0]!); | |
| 124 | expect((await post("/settings/origins/token", { host: fake.url, token })).status).toBe(303); | |
| 125 | browse = `/origins/${listOrigins(app.db, alice.id)[0]!.id}/r/octo/gears`; | |
| 126 | clearViewCache(); | |
| 127 | }, GIT_MS); | |
| 128 | ||
| 129 | afterAll(async () => { | |
| 130 | fake.stop(); | |
| 131 | servers.stop(); | |
| 132 | upServers.stop(); | |
| 133 | app.close(); | |
| 134 | up.close(); | |
| 135 | forgetProviders(dirs[0]!); | |
| 136 | for (const d of dirs) await rm(d, { recursive: true, force: true }); | |
| 137 | }); | |
| 138 | ||
| 139 | test("the web's proposal to another forge", async () => { | |
| 140 | const form = { ref: "main", path: "de.json", creating: "1", content: '{ "password": "Passwort" }\n', message: "words", branch: "alice/words", pullTitle: "Words" }; | |
| 141 | const before = pullsSent(); | |
| 142 | const page = await (await post(`${browse}/contribute`, form)).text(); | |
| 143 | expect(page).toContain("password in de.json looks like a secret"); | |
| 144 | expect(pullsSent()).toBe(before); | |
| 145 | const notsecret = (/name="notsecret" value="([^"]*)"/.exec(page)?.[1] ?? "").replaceAll(""", '"'); | |
| 146 | expect((await post(`${browse}/contribute`, { ...form, notsecret })).status).toBe(303); | |
| 147 | expect(pullsSent()).toBe(before + 1); | |
| 148 | }, GIT_MS); | |
| 149 | ||
| 150 | test("the terminal's proposal to another forge", async () => { | |
| 151 | const src = originSource(app, listOrigins(app.db, alice.id)[0]!, "octo/gears"); | |
| 152 | if (!src.ok) throw new Error("no source"); | |
| 153 | const { tui, session, press } = boot(alice); | |
| 154 | try { | |
| 155 | await settleApp(tui); | |
| 156 | tui.push(workspaceScreen(session, tui.ctx, src.value, { open: "lang.yml", focus: "file" })); | |
| 157 | await settleUntil(tui, "lang.yml"); | |
| 158 | const before = pullsSent(); | |
| 159 | await press("e", "end", ..."Passwort", "esc", "c"); | |
| 160 | await settleUntil(tui, "Propose a change"); | |
| 161 | await press("ctrl+s"); | |
| 162 | await settleUntil(tui, "looks like a secret"); | |
| 163 | expect(pullsSent()).toBe(before); | |
| 164 | await press(" ", "ctrl+s"); | |
| 165 | await waitFor(() => pullsSent() === before + 1, { every: 50, within: 30_000 }); | |
| 166 | expect(pullsSent()).toBe(before + 1); | |
| 167 | } finally { | |
| 168 | tui.stop(); | |
| 169 | } | |
| 170 | }, GIT_MS); | |
| 171 | ||
| 172 | test("a reader's proposal from the terminal, to a fork here", async () => { | |
| 173 | const { tui, press } = boot(kim); | |
| 174 | const open = () => listPulls(app.db, getRepo(app.db, "alice", "beacon")!.id, { state: "open" }); | |
| 175 | try { | |
| 176 | await settleApp(tui); | |
| 177 | await press("/"); | |
| 178 | await press(..."alice/beacon"); | |
| 179 | await press("enter"); | |
| 180 | await press("enter"); | |
| 181 | await settleUntil(tui, "alice/beacon"); | |
| 182 | await press("t"); | |
| 183 | await press(..."langyml"); | |
| 184 | await press("enter"); | |
| 185 | await press("e", "end"); | |
| 186 | await press(..."Passwort"); | |
| 187 | await press("esc", "c"); | |
| 188 | await settleUntil(tui, "Propose a change"); | |
| 189 | await press("ctrl+s"); | |
| 190 | await settleUntil(tui, "looks like a secret"); | |
| 191 | expect(open()).toHaveLength(0); | |
| 192 | await press(" ", "ctrl+s"); | |
| 193 | await settleUntil(tui, "opened pull #"); | |
| 194 | expect(open()).toHaveLength(1); | |
| 195 | const blob = await blobForRender(join(app.reposRoot, "kim", "beacon.git"), open()[0]!.headBranch, "lang.yml", 1 << 20); | |
| 196 | expect(blob.kind === "text" && new TextDecoder().decode(blob.body)).toBe("password: Passwort\n"); | |
| 197 | } finally { | |
| 198 | tui.stop(); | |
| 199 | } | |
| 200 | }, GIT_MS); |
new above
offline