35 lines · 2.1 KB · TypeScriptRaw
/** * The admin token (ROUTES-AUDIT 17 and 18, 23 Nov 2026): compared in constant time, and the only key to the routes * that give an account space or lift its caps; an owner token minted on the account is refused there. */import { describe, expect, it } from "vitest";import { isAdminToken } from "../src/http.ts";import { ADMIN, http, mint, personalObject } from "./personal.ts";describe("isAdminToken", () => { it("matches the token alone: not a prefix, a longer one, another, or anything when none is set", () => { const env = { ADMIN_TOKEN: "test-admin-token" }; expect(isAdminToken(env, "test-admin-token")).toBe(true); for (const t of ["test-admin-toke", "test-admin-token!", "Test-admin-token", "", null, undefined]) expect(isAdminToken(env, t), String(t)).toBe(false); expect(isAdminToken({}, "test-admin-token")).toBe(false); expect(isAdminToken({ ADMIN_TOKEN: "" }, "")).toBe(false); });});describe("the space and cap routes", () => { it("take the admin token, and refuse an owner token minted on the account itself", async () => { const slug = "admin-only-space"; const { credential } = await personalObject(slug); const owner = await mint(slug, credential, "Invented assistant", ["owner"]); const asOwner = { authorization: `Bearer ${owner}` }; for (const [path, body] of [["/v1/admin/space/bought", { bytes: 2 * 1024 ** 4 }], ["/v1/admin/space/mode", { mode: "off" }], ["/v1/admin/media/cap", { bytes: 8 * 1024 ** 4 }]] as const) { const refused = await http<{ code: string }>(slug, "POST", path, body, asOwner); expect([refused.status, refused.body.code], path).toEqual([403, "admin_only"]); } expect((await http(slug, "POST", "/v1/admin/space/bought", { bytes: 10 * 1024 ** 3 }, ADMIN)).status).toBe(200); expect((await http(slug, "POST", "/v1/admin/space/mode", { mode: null }, ADMIN)).status).toBe(200); // A prefix of the admin token is no admin token. expect((await http(slug, "POST", "/v1/admin/space/bought", { bytes: null }, { authorization: "Bearer test-admin-toke" })).status).toBe(403); });});