Public: anyone can read this; the crew can change it.
A test pins changing quotas and capacity: an unknown key is refused, and nothing in the batch is applied #7110
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/7104-a-test-pins-changing-quotas-and-capacity → main 1 commit fork
1 file
changed +68
−0
test/quota-capacity-keys.test.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** | |
| 2 | * An unknown quota or capacity key is refused by name, and a refusal changes | |
| 3 | * nothing: a batch with one unknown key applies none of it, and a caller who | |
| 4 | * is not a forge admin is turned away before any key is looked at. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, 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 { changeCapacity, listCapacity } from "../src/actions/capacity.ts"; | |
| 12 | import { changeQuotas } from "../src/actions/quotas.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { getQuotas } from "../src/settings/quotas.ts"; | |
| 16 | ||
| 17 | describe("quota and capacity keys", () => { | |
| 18 | let dataDir: string; | |
| 19 | let app: App; | |
| 20 | let admin: User; | |
| 21 | let member: User; | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-keys-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | for (const username of ["alice", "bob"]) { | |
| 27 | const made = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username, | |
| 31 | email: `${username}@forge.local`, | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: username === "alice", | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(made.error); | |
| 36 | if (username === "alice") admin = made.value; | |
| 37 | else member = made.value; | |
| 38 | } | |
| 39 | }); | |
| 40 | afterAll(async () => { | |
| 41 | await rm(dataDir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | test("an unknown quota key refuses the whole batch, the known key too", () => { | |
| 45 | const before = getQuotas(app.db); | |
| 46 | const res = changeQuotas(app, admin, { repositories: 7, nonsense: 3 }); | |
| 47 | expect(res).toEqual({ ok: false, error: "no_such_quota" }); | |
| 48 | expect(getQuotas(app.db)).toEqual(before); | |
| 49 | // Order does not matter: the unknown key first is the same answer. | |
| 50 | expect(changeQuotas(app, admin, { nonsense: 3, apps: 2 })).toEqual({ ok: false, error: "no_such_quota" }); | |
| 51 | expect(getQuotas(app.db)).toEqual(before); | |
| 52 | }); | |
| 53 | ||
| 54 | test("an unknown capacity key is no_such_setting and the read is unchanged", () => { | |
| 55 | const before = listCapacity(app, admin); | |
| 56 | expect(changeCapacity(app, admin, "no.such.key", "3")).toEqual({ ok: false, error: "no_such_setting" }); | |
| 57 | expect(listCapacity(app, admin)).toEqual(before); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a non-admin is refused before any key is looked at", () => { | |
| 61 | const before = getQuotas(app.db); | |
| 62 | expect(changeQuotas(app, member, { nonsense: 1 })).toEqual({ ok: false, error: "not_found" }); | |
| 63 | expect(changeQuotas(app, undefined, { repositories: 1 })).toEqual({ ok: false, error: "not_found" }); | |
| 64 | expect(changeCapacity(app, member, "no.such.key", "3")).toEqual({ ok: false, error: "not_found" }); | |
| 65 | expect(changeCapacity(app, undefined, "no.such.key", null)).toEqual({ ok: false, error: "not_found" }); | |
| 66 | expect(getQuotas(app.db)).toEqual(before); | |
| 67 | }); | |
| 68 | }); |
new above
offline