audittrail.ListFiltered (Zeitraum/Akteur/Aktion) + auditapi (Liste, gestreamter CSV-Export, Allowlist-Zugriffspruefung) als Backend- Voraussetzung. Audit-Log-Seite im dms-browser mit Filterformular und Export derselben gefilterten Menge. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
40 lines
1.6 KiB
TypeScript
40 lines
1.6 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { auditExportUrl, fetchAuditLog } from "./auditApi";
|
|
|
|
const ORIGINAL_ENV = process.env;
|
|
|
|
beforeEach(() => {
|
|
process.env = { ...ORIGINAL_ENV, NEXT_PUBLIC_AUDIT_API_URL: "http://backend.test", NEXT_PUBLIC_USER_ID: "auditor-1" };
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = ORIGINAL_ENV;
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe("auditApi", () => {
|
|
it("fetchAuditLog sendet caller und alle gesetzten Filter", async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue(new Response("[]", { status: 200 }));
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
await fetchAuditLog({ actor: "user-a", action: "login" });
|
|
const url = new URL(fetchMock.mock.calls[0][0] as string);
|
|
expect(url.pathname).toBe("/audit-log");
|
|
expect(url.searchParams.get("caller")).toBe("auditor-1");
|
|
expect(url.searchParams.get("actor")).toBe("user-a");
|
|
expect(url.searchParams.get("action")).toBe("login");
|
|
});
|
|
|
|
it("fetchAuditLog wirft bei 403 eine verstaendliche Fehlermeldung", async () => {
|
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response(null, { status: 403 })));
|
|
await expect(fetchAuditLog({})).rejects.toThrow(/Berechtigung/);
|
|
});
|
|
|
|
it("auditExportUrl baut dieselben Filter in die Export-URL ein", () => {
|
|
const url = new URL(auditExportUrl({ from: "2026-01-01T00:00:00Z", actor: "user-a" }));
|
|
expect(url.pathname).toBe("/audit-log/export");
|
|
expect(url.searchParams.get("from")).toBe("2026-01-01T00:00:00Z");
|
|
expect(url.searchParams.get("actor")).toBe("user-a");
|
|
expect(url.searchParams.get("caller")).toBe("auditor-1");
|
|
});
|
|
});
|