File size: 2,812 Bytes
ceb943f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
 
 
 
 
 
 
eaf6f0e
ceb943f
 
 
 
eaf6f0e
ceb943f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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);
        });
    });
});