import { beforeEach, describe, expect, it, vi } from "vitest"; // Mock the Auth.js session so the chokepoint is exercised without a server. vi.mock("@/auth/config", () => ({ auth: vi.fn(), })); import { auth } from "@/auth/config"; import { GET } from "@/app/api/admin/route"; import type { Mock } from "vitest"; const authMock = auth as unknown as Mock; describe("/api/admin chokepoint", () => { beforeEach(() => vi.clearAllMocks()); it("401 for anonymous (no session)", async () => { authMock.mockResolvedValue(null); const res = await GET(); expect(res.status).toBe(401); const body = (await res.json()) as { code: string }; expect(body.code).toBe("UNAUTHORIZED"); }); it("403 for a volunteer on /admin", async () => { authMock.mockResolvedValue({ user: { systemRole: "volunteer", email: "jordan@example.com" }, }); const res = await GET(); expect(res.status).toBe(403); const body = (await res.json()) as { code: string }; expect(body.code).toBe("FORBIDDEN"); }); it("200 for an admin on /admin", async () => { authMock.mockResolvedValue({ user: { systemRole: "admin", email: "admin@example.com" }, }); const res = await GET(); expect(res.status).toBe(200); const body = (await res.json()) as { ok: boolean; role: string }; expect(body.ok).toBe(true); expect(body.role).toBe("admin"); }); });