Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
395 lines
14 KiB
TypeScript
395 lines
14 KiB
TypeScript
import { act, render, screen, waitFor, fireEvent, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import AuditLogPage from "./page";
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
const auth = vi.hoisted(() => ({ hasModule: vi.fn(() => true), loading: false }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ hasModule: auth.hasModule, loading: auth.loading }),
|
|
}));
|
|
|
|
const svc = vi.hoisted(() => ({
|
|
listAuditLog: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/auditLog", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("@/app/lib/services/auditLog")>();
|
|
return {
|
|
...actual,
|
|
listAuditLog: svc.listAuditLog,
|
|
};
|
|
});
|
|
|
|
import { listAuditLog } from "@/app/lib/services/auditLog";
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
memoryRouter.reset("/audit-log");
|
|
auth.hasModule.mockReturnValue(true);
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
});
|
|
|
|
function makeEvent(overrides: Record<string, unknown> = {}): Record<string, unknown> {
|
|
return {
|
|
id: 1,
|
|
occurredAt: "2026-09-01T10:30:00",
|
|
actorType: "ADMIN",
|
|
actor: "admin1",
|
|
platform: "WEB",
|
|
module: "BO_MEMBERS",
|
|
action: "CREATE",
|
|
httpMethod: "POST",
|
|
pathTemplate: "/api/admin/members",
|
|
targetType: "MEMBER",
|
|
targetId: "M-100",
|
|
outcome: "SUCCESS",
|
|
httpStatus: 200,
|
|
clientIp: "203.0.113.10",
|
|
changes: null,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("AuditLogPage", () => {
|
|
it("renders the not-authorised state without the BO_AUDIT_LOG grant", async () => {
|
|
auth.hasModule.mockReturnValue(false);
|
|
|
|
render(<AuditLogPage />);
|
|
|
|
expect(await screen.findByTestId("not-authorized")).toBeInTheDocument();
|
|
expect(svc.listAuditLog).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("fetches once and settles instead of refetching in a loop", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [makeEvent()], totalElements: 1, totalPages: 1, number: 0, size: 20 });
|
|
|
|
render(<AuditLogPage />);
|
|
|
|
expect(await screen.findByText("admin1")).toBeInTheDocument();
|
|
await new Promise((r) => setTimeout(r, 300));
|
|
expect(svc.listAuditLog).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("renders rows from a mocked list response, newest first", async () => {
|
|
const evt1 = makeEvent({ id: 1, actor: "admin1", occurredAt: "2026-09-01T10:30:00" });
|
|
const evt2 = makeEvent({ id: 2, actor: "admin2", occurredAt: "2026-09-02T11:00:00" });
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt2, evt1],
|
|
totalElements: 2,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
expect(await screen.findByText("admin1")).toBeInTheDocument();
|
|
expect(screen.getByText("admin2")).toBeInTheDocument();
|
|
// Translated headers
|
|
expect(screen.getByRole("columnheader", { name: /^time$/i })).toBeInTheDocument();
|
|
expect(screen.getByRole("columnheader", { name: /^actor$/i })).toBeInTheDocument();
|
|
// Translated outcome badge on first row
|
|
const row1 = await screen.findByText("admin1");
|
|
const firstRow = row1.closest("tr");
|
|
expect(within(firstRow!).getByText("Success")).toHaveAttribute("data-variant", "default");
|
|
});
|
|
|
|
it("shows a legacy XController.method action as its readable code", async () => {
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [makeEvent({ action: "AdminPromotionController.changeStatus" })],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 0,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
|
|
const row = (await screen.findByText("admin1")).closest("tr")!;
|
|
expect(within(row).getByText("PROMOTION_CHANGE_STATUS")).toBeInTheDocument();
|
|
expect(within(row).queryByText("AdminPromotionController.changeStatus")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the readable action code in the detail view", async () => {
|
|
const user = userEvent.setup();
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [makeEvent({ action: "AdminPromotionController.generateVouchers" })],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 0,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
await user.click(await screen.findByText("admin1"));
|
|
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(within(dialog).getByText("PROMOTION_GENERATE_VOUCHERS")).toBeInTheDocument();
|
|
});
|
|
|
|
it("sends the actor filter as a query param", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
const user = userEvent.setup();
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("searchbox", { name: /actor/i });
|
|
|
|
await user.type(await screen.findByRole("searchbox", { name: /actor/i }), "admin1");
|
|
await user.keyboard("{Escape}");
|
|
// The filter is debounced (300ms), so wait for the call
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ actor: "admin1" })
|
|
));
|
|
});
|
|
|
|
it("sends the actor type filter (ADMIN/MEMBER)", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
const user = userEvent.setup();
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("searchbox", { name: /actor/i });
|
|
|
|
const selectTrigger = screen.getByRole("combobox", { name: /actor type/i });
|
|
await user.click(selectTrigger);
|
|
await user.click(await screen.findByRole("option", { name: "Admin" }));
|
|
|
|
await waitFor(() =>
|
|
expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ actorType: "ADMIN" })
|
|
)
|
|
);
|
|
});
|
|
|
|
it("sends the module filter", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
const user = userEvent.setup();
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("searchbox", { name: /actor/i });
|
|
|
|
const moduleInput = screen.getByRole("searchbox", { name: /module/i });
|
|
await user.type(moduleInput, "BO_MEMBERS");
|
|
await user.keyboard("{Escape}");
|
|
await waitFor(() =>
|
|
expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ module: "BO_MEMBERS" })
|
|
)
|
|
);
|
|
});
|
|
|
|
it("sends the action filter", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
const user = userEvent.setup();
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("searchbox", { name: /actor/i });
|
|
|
|
const actionInput = screen.getByRole("searchbox", { name: /action/i });
|
|
await user.type(actionInput, "CREATE");
|
|
await user.keyboard("{Escape}");
|
|
await waitFor(() =>
|
|
expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ action: "CREATE" })
|
|
)
|
|
);
|
|
});
|
|
|
|
it("sends the ip filter", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
const user = userEvent.setup();
|
|
render(<AuditLogPage />);
|
|
await screen.findByRole("searchbox", { name: /actor/i });
|
|
|
|
const ipInput = screen.getByRole("searchbox", { name: /ip/i });
|
|
await user.type(ipInput, "203.0.113.10");
|
|
await user.keyboard("{Escape}");
|
|
await waitFor(() =>
|
|
expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ ip: "203.0.113.10" })
|
|
)
|
|
);
|
|
});
|
|
|
|
it("renders the empty state with translated text", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
render(<AuditLogPage />);
|
|
const emptyTitle = await screen.findByText(/no audit events/i);
|
|
expect(emptyTitle).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders the error state with translated text when load fails", async () => {
|
|
svc.listAuditLog.mockRejectedValue(new Error("upstream error"));
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(screen.getByRole("alert")).toBeInTheDocument());
|
|
expect(screen.getByText(/couldn't load the audit log/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("opens the detail view when clicking a row", async () => {
|
|
const evt = makeEvent({
|
|
id: 42,
|
|
changes: null,
|
|
});
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
await screen.findByText("admin1");
|
|
|
|
await userEvent.click(screen.getByText("admin1"));
|
|
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(dialog).toBeInTheDocument();
|
|
expect(dialog).toHaveTextContent("Audit event details");
|
|
expect(dialog).toHaveTextContent("admin1");
|
|
expect(dialog).toHaveTextContent("Success");
|
|
});
|
|
|
|
it("shows the no-changes text when changes is null", async () => {
|
|
const evt = makeEvent({ id: 42, changes: null });
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
await screen.findByText("admin1");
|
|
|
|
await userEvent.click(screen.getByText("admin1"));
|
|
|
|
const noChanges = await screen.findByText(/no changes recorded/i);
|
|
expect(noChanges).toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the before/after table when changes are present", async () => {
|
|
const evt = makeEvent({
|
|
id: 42,
|
|
changes: JSON.stringify({ name: { before: "old", after: "new" } }),
|
|
});
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
await screen.findByText("admin1");
|
|
|
|
await userEvent.click(screen.getByText("admin1"));
|
|
|
|
const dialog = await screen.findByRole("dialog");
|
|
const dialogue = within(dialog);
|
|
expect(dialogue.getByText("name")).toBeInTheDocument();
|
|
expect(dialogue.getByText("old")).toBeInTheDocument();
|
|
expect(dialogue.getByText("new")).toBeInTheDocument();
|
|
});
|
|
|
|
it("sends date filters in yyyy-MM-dd format (not datetime)", async () => {
|
|
svc.listAuditLog.mockResolvedValue({ content: [], totalElements: 0, totalPages: 0, number: 1, size: 20 });
|
|
memoryRouter.reset("/audit-log", "auditlog.from=2026-09-01&auditlog.to=2026-09-30");
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalledWith(
|
|
expect.objectContaining({ from: "2026-09-01", to: "2026-09-30" })
|
|
));
|
|
});
|
|
|
|
it("clears error and shows rows on retry after failure", async () => {
|
|
// The first fetch fails; the user clicks Retry; the retry succeeds. (The old version forced a refetch with an
|
|
// unrelated URL param, which only worked because the page refetched on every render — the loop fixed here.)
|
|
const evt = makeEvent({ id: 1, actor: "admin1" });
|
|
svc.listAuditLog.mockRejectedValueOnce(new Error("upstream error"));
|
|
svc.listAuditLog.mockResolvedValueOnce({ content: [evt], totalElements: 1, totalPages: 1, number: 0, size: 20 });
|
|
|
|
render(<AuditLogPage />);
|
|
|
|
expect(await screen.findByRole("alert")).toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole("button", { name: /retry/i }));
|
|
|
|
expect(await screen.findByText("admin1")).toBeInTheDocument();
|
|
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
expect(svc.listAuditLog).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("clears error and shows rows on retry after failure", async () => {
|
|
const evt = makeEvent({ id: 1, actor: "admin1", actorType: "ADMIN" });
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
// Translated label should appear in table cell
|
|
expect(await screen.findByText("Admin")).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders translated actor type in detail view badge", async () => {
|
|
const evt = makeEvent({ id: 42, actor: "admin1", actorType: "MEMBER" });
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
await screen.findByText("admin1");
|
|
await userEvent.click(screen.getByText("admin1"));
|
|
const dialog = await screen.findByRole("dialog");
|
|
expect(dialog).toHaveTextContent("Member");
|
|
});
|
|
|
|
it("renders numeric/boolean/object values via JSON.stringify and null shows translated none text", async () => {
|
|
const evt = makeEvent({
|
|
id: 42,
|
|
changes: JSON.stringify({
|
|
count: { before: 0, after: 5 },
|
|
active: { before: false, after: true },
|
|
tags: { before: [], after: ["a","b"] },
|
|
empty: { before: null, after: null },
|
|
}),
|
|
});
|
|
svc.listAuditLog.mockResolvedValue({
|
|
content: [evt],
|
|
totalElements: 1,
|
|
totalPages: 1,
|
|
number: 1,
|
|
size: 20,
|
|
});
|
|
render(<AuditLogPage />);
|
|
await waitFor(() => expect(listAuditLog).toHaveBeenCalled());
|
|
await screen.findByText("admin1");
|
|
await userEvent.click(screen.getByText("admin1"));
|
|
const dialog = await screen.findByRole("dialog");
|
|
const dialogue = within(dialog);
|
|
// Numeric values rendered as strings
|
|
expect(dialogue.getByText("5")).toBeInTheDocument();
|
|
expect(dialogue.getByText("0")).toBeInTheDocument();
|
|
// Boolean values rendered as strings
|
|
expect(dialogue.getByText("true")).toBeInTheDocument();
|
|
expect(dialogue.getByText("false")).toBeInTheDocument();
|
|
// Array values rendered via JSON.stringify
|
|
const rows = dialogue.getAllByRole('row');
|
|
const rowTexts = rows.flatMap(r => Array.from(r.childNodes).map(n => n.textContent ?? '').filter(Boolean));
|
|
expect(rowTexts.some(t => t.includes('"a"') && t.includes('"b"'))).toBe(true);
|
|
// null value shows translated none text (em dash)
|
|
const emDashes = dialogue.getAllByText('—');
|
|
// One em dash from the null before value, one from the null after value in the "empty" field row
|
|
expect(emDashes.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
});
|