Files

48 lines
2.0 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn(), getToken: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
vi.mock("@/app/lib/server/session", () => ({ getToken: mocks.getToken }));
import * as route from "./route";
const { GET } = route;
const req = (url: string) => new Request(url) as unknown as Parameters<typeof GET>[0];
const ctx = (path?: string[]) => ({ params: Promise.resolve({ path }) });
beforeEach(() => {
mocks.callMobileApi.mockReset();
mocks.getToken.mockReset().mockResolvedValue("jwt");
});
describe("/api/audit-log/[[...path]]", () => {
it("is GET only", () => {
expect(Object.keys(route).filter((k) => /^(GET|POST|PUT|PATCH|DELETE)$/.test(k))).toEqual(["GET"]);
});
it("returns 401 without a session", async () => {
mocks.getToken.mockResolvedValue(null);
expect((await GET(req("http://localhost/api/audit-log"), ctx())).status).toBe(401);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("refuses dot segments", async () => {
expect((await GET(req("http://localhost/api/audit-log/.."), ctx([".."]))).status).toBe(400);
expect((await GET(req("http://localhost/api/audit-log/."), ctx(["."]))).status).toBe(400);
expect(mocks.callMobileApi).not.toHaveBeenCalled();
});
it("forwards the query to the list", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { content: [] } });
const res = await GET(req("http://localhost/api/audit-log?actor=a&page=0&size=20"), ctx());
expect(res.status).toBe(200);
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/audit-log?actor=a&page=0&size=20", { token: "jwt" });
});
it("forwards one event by encoded id", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { id: "a/b" } });
await GET(req("http://localhost/api/audit-log/a%2Fb"), ctx(["a/b"]));
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/audit-log/a%2Fb", { token: "jwt" });
});
});