Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
A test proves /admin settings and moderators survive a relay restart on the same FORGE_APP_DATA #143
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/136-a-test-proves-admin-settings-and-moderat → main 4 commits fork
1 file
changed +59
−0
Software/so101/src/admin-restart.test.ts added +59
| @@ -0,0 +1,59 @@ | ||
| 1 | import { afterEach, expect, test } from "bun:test"; | |
| 2 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { sign } from "./identity.ts"; | |
| 6 | import { Lobby } from "./lobby.ts"; | |
| 7 | import { startRelay, type RunningRelay } from "./relay.ts"; | |
| 8 | ||
| 9 | const KEY = "k".repeat(64); | |
| 10 | const later = Date.now() + 600_000; | |
| 11 | const as = (name: string, role: "user" | "admin") => { | |
| 12 | const identity = `id-${name}|${name}|${role}|${later}`; | |
| 13 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; | |
| 14 | }; | |
| 15 | const form = (headers: Record<string, string>, body: Record<string, string>) => ({ method: "POST", headers: { ...headers, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(body), redirect: "manual" as const }); | |
| 16 | ||
| 17 | let dir = ""; | |
| 18 | let relay: RunningRelay | undefined; | |
| 19 | let lobby: Lobby | undefined; | |
| 20 | afterEach(async () => { | |
| 21 | await relay?.close(); | |
| 22 | lobby?.close(); | |
| 23 | if (dir) rmSync(dir, { recursive: true, force: true }); | |
| 24 | dir = ""; | |
| 25 | relay = lobby = undefined; | |
| 26 | }); | |
| 27 | ||
| 28 | async function boot() { | |
| 29 | dir ||= mkdtempSync(join(tmpdir(), "so101-admin-restart-")); | |
| 30 | lobby = new Lobby({ identityKey: KEY, adminFile: join(dir, "admin.json") }); | |
| 31 | relay = await startRelay({ port: 0, simulation: "http://127.0.0.1:1", lobby }); | |
| 32 | return `http://127.0.0.1:${relay.port}`; | |
| 33 | } | |
| 34 | ||
| 35 | test("a setting, a moderator and the log entries survive a relay restart on the same data dir", async () => { | |
| 36 | const owner = as("trevato", "admin"); | |
| 37 | const bob = as("bob", "user"); | |
| 38 | let base = await boot(); | |
| 39 | const saved = await fetch(`${base}/admin`, form(owner, { action: "save", announcement: "back at five" })); | |
| 40 | expect(saved.status).toBe(303); | |
| 41 | expect((await fetch(`${base}/admin`, form(owner, { action: "mod-add", name: "bob" }))).status).toBe(303); | |
| 42 | ||
| 43 | await relay!.close(); | |
| 44 | lobby!.close(); | |
| 45 | base = await boot(); | |
| 46 | ||
| 47 | const html = await (await fetch(`${base}/admin`, { headers: owner })).text(); | |
| 48 | expect(html).toContain("back at five"); | |
| 49 | expect(html).toMatch(/<b>trevato<\/b> announcement/); | |
| 50 | expect(html).toMatch(/<b>trevato<\/b> moderators → bob/); | |
| 51 | ||
| 52 | const seen = await fetch(`${base}/admin`, { headers: bob }); | |
| 53 | expect(seen.status).toBe(200); | |
| 54 | expect(await seen.text()).toContain("read-only"); | |
| 55 | expect((await fetch(`${base}/admin`, form(bob, { action: "save", announcement: "hijacked" }))).status).toBe(403); | |
| 56 | const after = await (await fetch(`${base}/admin`, { headers: owner })).text(); | |
| 57 | expect(after).toContain("back at five"); | |
| 58 | expect(after).not.toContain("hijacked"); | |
| 59 | }); |
new above
offline