3cd932e1a1
- Auth.js v5 Credentials provider (magic-link) + JWT session carrying systemRole - src/auth: roles, permissions (feature-scoped matrix), magic-link dev console transport - requirePermission(user, perm) chokepoint -> AuthError(401/403) - Routes: /api/auth/[...nextauth], /api/dev/magic-link, /api/dev/complete, /api/admin, /login - Auto-activate new emails in dev (AUTO_ACTIVATE_NEW_USERS, flip to require approval) - Acceptance pnpm test src/auth green: 401 anonymous, 403 volunteer on /admin, 200 admin - Verified at runtime against Postgres on :5433
46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
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");
|
|
});
|
|
});
|