Public: anyone can read this; the crew can change it.
A test pins the app limit: the account at its limit is refused, a forge admin never is, and no limit asks nothing #6835
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/6822-a-test-pins-the-app-limit-the-account-at → main 1 commit fork
1 file
changed +77
−0
test/app-limit.test.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | /** | |
| 2 | * The app limit (`actions/app_limit.ts`) called directly: an account at the | |
| 3 | * `apps` quota is refused, a forge admin never is, and no quota asks nothing. | |
| 4 | */ | |
| 5 | import { afterAll, expect, setDefaultTimeout, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { appLimitWords, requireAppRoom } from "../src/actions/app_limit.ts"; | |
| 10 | import { createApp as createServer } from "../src/app.ts"; | |
| 11 | import { createApp } from "../src/core/apps.ts"; | |
| 12 | import { createRepo } from "../src/core/repos.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { setQuota } from "../src/settings/quotas.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | setDefaultTimeout(30_000); | |
| 19 | ||
| 20 | const dirs: string[] = []; | |
| 21 | afterAll(async () => { | |
| 22 | for (const d of dirs) await rm(d, { recursive: true, force: true }); | |
| 23 | }); | |
| 24 | ||
| 25 | /** A plain account and an admin, with `owned` apps under the plain one. */ | |
| 26 | async function world(owned: number) { | |
| 27 | const dir = await mkdtemp(join(tmpdir(), "forge-app-limit-")); | |
| 28 | dirs.push(dir); | |
| 29 | const app = createServer({ dataDir: dir, port: 0 }); | |
| 30 | const mk = async (username: string, isAdmin: boolean) => { | |
| 31 | const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, isAdmin }); | |
| 32 | if (!made.ok) throw new Error("user"); | |
| 33 | return made.value; | |
| 34 | }; | |
| 35 | const admin = await mk("boss", true); | |
| 36 | const user = await mk("maker", false); | |
| 37 | for (let i = 0; i < owned; i++) { | |
| 38 | const repo = await createRepo(app.db, app.reposRoot, { ownerId: user.id, name: `r${i}` }); | |
| 39 | if (!repo.ok) throw new Error(repo.error); | |
| 40 | const made = createApp(app.db, { repoId: repo.value.id, slug: `s${i}`, access: "public" }); | |
| 41 | if (!made.ok) throw new Error(made.error); | |
| 42 | } | |
| 43 | return { app, admin, user }; | |
| 44 | } | |
| 45 | ||
| 46 | test("at the limit a non-admin is refused; under it, ok", async () => { | |
| 47 | const { app, admin, user } = await world(2); | |
| 48 | setQuota(app.db, "apps", 2, admin.id); | |
| 49 | const full = requireAppRoom(app, user, user.id); | |
| 50 | expect(full.ok).toBe(false); | |
| 51 | if (!full.ok) expect(full.error).toBe("app_limit"); | |
| 52 | setQuota(app.db, "apps", 3, admin.id); | |
| 53 | expect(requireAppRoom(app, user, user.id).ok).toBe(true); | |
| 54 | }); | |
| 55 | ||
| 56 | test("a forge admin at or over the limit is never refused", async () => { | |
| 57 | const { app, admin, user } = await world(2); | |
| 58 | setQuota(app.db, "apps", 1, admin.id); | |
| 59 | expect(requireAppRoom(app, user, user.id).ok).toBe(false); | |
| 60 | expect(requireAppRoom(app, admin, user.id).ok).toBe(true); | |
| 61 | setQuota(app.db, "apps", 2, admin.id); | |
| 62 | expect(requireAppRoom(app, admin, user.id).ok).toBe(true); | |
| 63 | }); | |
| 64 | ||
| 65 | test("with no quota set any count is ok", async () => { | |
| 66 | const { app, user } = await world(3); | |
| 67 | expect(requireAppRoom(app, user, user.id).ok).toBe(true); | |
| 68 | }); | |
| 69 | ||
| 70 | test("the words name the count and never exceed the limit", async () => { | |
| 71 | const { app, admin, user } = await world(2); | |
| 72 | setQuota(app.db, "apps", 2, admin.id); | |
| 73 | expect(appLimitWords(app, user.id)).toContain("2 of 2 apps"); | |
| 74 | setQuota(app.db, "apps", 1, admin.id); | |
| 75 | expect(appLimitWords(app, user.id)).toContain("1 of 1 apps"); | |
| 76 | expect(appLimitWords(app, user.id)).not.toContain("2 of"); | |
| 77 | }); |
new above
offline