Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
85 lines
4.5 KiB
TypeScript
85 lines
4.5 KiB
TypeScript
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ searchAuditLog: vi.fn(), useAdminAuth: vi.fn() }));
|
|
vi.mock("@/app/lib/services/auditLog", async (importActual) => ({
|
|
...(await importActual<typeof import("@/app/lib/services/auditLog")>()),
|
|
searchAuditLog: mocks.searchAuditLog,
|
|
}));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import AuditLogPage from "./page";
|
|
|
|
const EVENTS = [
|
|
{ id: 2, companyId: 1, occurredAt: "2026-11-30T15:52:00.5", actor: "sales1", module: "BO_USERS", action: "USER_UPDATE", httpMethod: "PUT", pathTemplate: "/api/admin/users/{id}", targetId: "42", outcome: "FAILURE", httpStatus: 403, clientIp: "10.0.0.5", changes: null },
|
|
{ id: 1, companyId: 1, occurredAt: "2026-11-29T09:05:00", actor: "admin", module: null, action: "LEAD_EXPORT", httpMethod: "GET", pathTemplate: "/api/admin/leads/export", targetId: null, outcome: "SUCCESS", httpStatus: 200, clientIp: "10.0.0.6", changes: null },
|
|
];
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/audit-log");
|
|
mocks.searchAuditLog.mockReset().mockResolvedValue({ items: EVENTS, total: 2 });
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true });
|
|
});
|
|
|
|
describe("AuditLogPage", () => {
|
|
it("renders NotAuthorized without BO_AUDIT_LOG", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<AuditLogPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(mocks.searchAuditLog).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("lists rows with module label, outcome badge with status, and a link to the detail", async () => {
|
|
render(<AuditLogPage />);
|
|
const table = await screen.findByRole("table");
|
|
const rows = within(table).getAllByRole("row").slice(1);
|
|
expect(rows[0]).toHaveTextContent("30/11/2026 15:52");
|
|
expect(rows[0]).toHaveTextContent("sales1");
|
|
expect(rows[0]).toHaveTextContent("Users");
|
|
expect(rows[0]).toHaveTextContent("USER_UPDATE");
|
|
expect(rows[0]).toHaveTextContent("FAILURE 403");
|
|
expect(rows[0]).toHaveTextContent("10.0.0.5");
|
|
expect(within(rows[0]).getByRole("link", { name: "USER_UPDATE" })).toHaveAttribute("href", "/audit-log/2");
|
|
expect(rows[1]).toHaveTextContent("SUCCESS 200");
|
|
});
|
|
|
|
it("labels lead modules and offers them in the module filter, without BO_AUDIT_LOG", async () => {
|
|
mocks.searchAuditLog.mockResolvedValue({ items: [{ ...EVENTS[1], module: "BO_LEADS_EXPORT" }], total: 1 });
|
|
render(<AuditLogPage />);
|
|
const table = await screen.findByRole("table");
|
|
expect(within(table).getAllByRole("row")[1]).toHaveTextContent("Lead export");
|
|
await userEvent.click(screen.getByRole("combobox", { name: "Module" }));
|
|
expect(await screen.findByRole("option", { name: "Lead editing" })).toBeInTheDocument();
|
|
expect(screen.getByRole("option", { name: "Lead export" })).toBeInTheDocument();
|
|
expect(screen.queryByRole("option", { name: "Audit log" })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows a dash for a null module", async () => {
|
|
render(<AuditLogPage />);
|
|
const table = await screen.findByRole("table");
|
|
expect(within(table).getAllByRole("row")[2]).toHaveTextContent("admin—LEAD_EXPORT");
|
|
});
|
|
|
|
it("puts the filters in the query", async () => {
|
|
memoryRouter.reset("/audit-log", "outcome=FAILURE&from=2026-11-01&module=BO_USERS");
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("table");
|
|
expect(mocks.searchAuditLog).toHaveBeenCalledWith(expect.objectContaining({ outcome: "FAILURE", from: "2026-11-01", module: "BO_USERS", page: 1, pageSize: 20 }));
|
|
await userEvent.type(screen.getByRole("searchbox", { name: "Who" }), "sales");
|
|
await waitFor(() => expect(mocks.searchAuditLog).toHaveBeenLastCalledWith(expect.objectContaining({ actor: "sales", outcome: "FAILURE" })));
|
|
});
|
|
|
|
it("shows an empty state and an error state", async () => {
|
|
mocks.searchAuditLog.mockResolvedValue({ items: [], total: 0 });
|
|
const { unmount } = render(<AuditLogPage />);
|
|
expect(await screen.findByText("No audit events found")).toBeInTheDocument();
|
|
unmount();
|
|
mocks.searchAuditLog.mockRejectedValue(new Error("x"));
|
|
render(<AuditLogPage />);
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("Failed to load the audit log");
|
|
});
|
|
});
|