Spaces:
Runtime error
Runtime error
refactor: update server to use ESM, refine product card availability UI, and improve test assertions.
eaf6f0e | import { describe, it, expect, vi, beforeEach } from "vitest"; | |
| import { isAdminEmail, getAdminSession } from "../admin"; | |
| import { auth } from "../auth"; | |
| vi.mock("../auth", () => ({ | |
| auth: { | |
| api: { | |
| getSession: vi.fn(), | |
| }, | |
| }, | |
| })); | |
| vi.mock("next/headers", () => ({ | |
| headers: vi.fn(), | |
| })); | |
| describe("Admin Authorization Utility", () => { | |
| const originalEnv = process.env; | |
| beforeEach(() => { | |
| vi.resetModules(); | |
| process.env = { ...originalEnv }; | |
| vi.clearAllMocks(); | |
| }); | |
| describe("isAdminEmail", () => { | |
| it("should return true if email is in ADMIN_EMAILS", () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io,ops@vault.io"; | |
| expect(isAdminEmail("admin@vault.io")).toBe(true); | |
| expect(isAdminEmail("ops@vault.io")).toBe(true); | |
| }); | |
| it("should be case-insensitive", () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io"; | |
| expect(isAdminEmail("ADMIN@VAULT.IO")).toBe(true); | |
| }); | |
| it("should return false if email is not in ADMIN_EMAILS", () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io"; | |
| expect(isAdminEmail("user@vault.io")).toBe(false); | |
| }); | |
| it("should return false for null or undefined", () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io"; | |
| expect(isAdminEmail(null)).toBe(false); | |
| expect(isAdminEmail(undefined)).toBe(false); | |
| }); | |
| }); | |
| describe("getAdminSession", () => { | |
| it("should return null if no session exists", async () => { | |
| vi.mocked(auth.api.getSession).mockResolvedValue(null); | |
| const session = await getAdminSession(); | |
| expect(session).toBeNull(); | |
| }); | |
| it("should return null if user is not an admin", async () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io"; | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| vi.mocked(auth.api.getSession).mockResolvedValue({ | |
| user: { email: "user@vault.io" } as any, | |
| session: {} as any, | |
| }); | |
| const session = await getAdminSession(); | |
| expect(session).toBeNull(); | |
| }); | |
| it("should return session if user is an admin", async () => { | |
| process.env.ADMIN_EMAILS = "admin@vault.io"; | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| const mockSession = { | |
| user: { email: "admin@vault.io" } as any, | |
| session: {} as any, | |
| }; | |
| vi.mocked(auth.api.getSession).mockResolvedValue(mockSession as any); | |
| const session = await getAdminSession(); | |
| expect(session).toEqual(mockSession); | |
| }); | |
| }); | |
| }); | |