import { describe, it, expect, vi, beforeEach } from "vitest"; const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPut: vi.fn() })); vi.mock("@/app/lib/clientApi", () => mocks); import { getRoleGrantTree, updateRoleGrantTree } from "./adminAcl"; beforeEach(() => { mocks.apiGet.mockReset(); mocks.apiPut.mockReset(); }); describe("getRoleGrantTree", () => { it("GETs /api/roles/{id}/modules", async () => { mocks.apiGet.mockResolvedValue([{ id: 40600000, name: "BO_USERS", parentId: 40000000, granted: true }]); const result = await getRoleGrantTree(4); expect(mocks.apiGet).toHaveBeenCalledWith("/api/roles/4/modules"); expect(result).toHaveLength(1); }); }); describe("updateRoleGrantTree", () => { it("PUTs the desired moduleIds and sends no X-Caller-Role header", async () => { mocks.apiPut.mockResolvedValue([]); await updateRoleGrantTree(4, [40600000, 40700000]); // The lockout guard resolves the caller's roles from the authenticated JWT on mobile-api, // so the client must NOT forward a caller-role header the guard could be spoofed with. expect(mocks.apiPut).toHaveBeenCalledWith("/api/roles/4/modules", { moduleIds: [40600000, 40700000] }); }); it("never forwards a caller-role value even when one is supplied", async () => { mocks.apiPut.mockResolvedValue([]); // @ts-expect-error -- the callerRole param no longer exists; a stale caller must not be sent. await updateRoleGrantTree(4, [40600000, 40700000], "ROLE_HQ"); expect(mocks.apiPut).toHaveBeenCalledWith("/api/roles/4/modules", { moduleIds: [40600000, 40700000] }); }); });