Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
75 lines
4.2 KiB
TypeScript
75 lines
4.2 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 { GET, POST, PUT } from "./route";
|
|
|
|
const req = (url: string, body?: unknown) =>
|
|
new Request(url, body === undefined ? undefined : { method: "PUT", body: JSON.stringify(body), headers: { "Content-Type": "application/json" } }) 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/login-security/[[...path]]", () => {
|
|
it("returns 401 without a session", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
expect((await GET(req("http://localhost/api/login-security/locked"), ctx(["locked"]))).status).toBe(401);
|
|
expect((await PUT(req("http://localhost/api/login-security/config", {}), ctx(["config"]))).status).toBe(401);
|
|
expect((await POST(req("http://localhost/api/login-security/locked/a/unlock", {}), ctx(["locked", "a", "unlock"]))).status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("refuses dot segments", async () => {
|
|
expect((await GET(req("http://localhost/api/login-security/.."), ctx([".."]))).status).toBe(400);
|
|
expect((await POST(req("http://localhost/api/login-security/locked/./unlock"), ctx(["locked", ".", "unlock"]))).status).toBe(400);
|
|
expect((await PUT(req("http://localhost/api/login-security/.."), ctx([".."]))).status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("GET forwards the path and query", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { content: [] } });
|
|
const res = await GET(req("http://localhost/api/login-security/audit?username=a&page=0&size=20"), ctx(["audit"]));
|
|
expect(res.status).toBe(200);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/login-security/audit?username=a&page=0&size=20", { token: "jwt" });
|
|
});
|
|
|
|
it("encodes each segment", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: null });
|
|
await POST(req("http://localhost/api/login-security/locked/a%2Fb/unlock"), ctx(["locked", "a/b", "unlock"]));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/login-security/locked/a%2Fb/unlock", { token: "jwt", method: "POST", body: undefined });
|
|
});
|
|
|
|
it("PUT forwards the JSON body", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { enabled: true } });
|
|
const body = { enabled: true, maxAttempts: 5, period: 15, unit: "M" };
|
|
const res = await PUT(req("http://localhost/api/login-security/config", body), ctx(["config"]));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/login-security/config", { token: "jwt", method: "PUT", body });
|
|
expect(await res.json()).toEqual({ enabled: true });
|
|
});
|
|
|
|
it("POST forwards the JSON body", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { x: 1 } });
|
|
const r = new Request("http://localhost/api/login-security/x", { method: "POST", body: JSON.stringify({ a: 1 }) }) as unknown as Parameters<typeof GET>[0];
|
|
await POST(r, ctx(["x"]));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/login-security/x", { token: "jwt", method: "POST", body: { a: 1 } });
|
|
});
|
|
|
|
it("passes an empty upstream body through as 204", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: null });
|
|
const res = await POST(req("http://localhost/api/login-security/locked/a/unlock"), ctx(["locked", "a", "unlock"]));
|
|
expect(res.status).toBe(204);
|
|
});
|
|
|
|
it("passes a refusal through with its code", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 400, error: "Max attempts must be at least 1", code: "INVALID_SETTING" });
|
|
const res = await PUT(req("http://localhost/api/login-security/config", { maxAttempts: 0 }), ctx(["config"]));
|
|
expect(res.status).toBe(400);
|
|
expect(await res.json()).toMatchObject({ code: "INVALID_SETTING", error: "Max attempts must be at least 1" });
|
|
});
|
|
});
|