Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
54 lines
2.2 KiB
TypeScript
54 lines
2.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn() }));
|
|
vi.mock("@/app/lib/clientApi", () => mocks);
|
|
|
|
import { getConfig, getPasswordRules, listAudit, listLocked, saveConfig, savePasswordRules, unlock } from "./loginSecurity";
|
|
|
|
beforeEach(() => {
|
|
mocks.apiGet.mockReset();
|
|
mocks.apiPost.mockReset();
|
|
mocks.apiPut.mockReset();
|
|
});
|
|
|
|
describe("loginSecurity service", () => {
|
|
it("listAudit sends only given filters with a 0-based page", async () => {
|
|
mocks.apiGet.mockResolvedValue({ content: [{ id: 1 }], totalElements: 9 });
|
|
const out = await listAudit({ username: "a", event: "LOGIN_FAIL", from: "2026-11-01", page: 2, pageSize: 10 });
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/login-security/audit?username=a&event=LOGIN_FAIL&from=2026-11-01&page=1&size=10");
|
|
expect(out).toEqual({ items: [{ id: 1 }], total: 9 });
|
|
});
|
|
|
|
it("listAudit tolerates an empty body", async () => {
|
|
mocks.apiGet.mockResolvedValue(null);
|
|
expect(await listAudit({ page: 1, pageSize: 10 })).toEqual({ items: [], total: 0 });
|
|
});
|
|
|
|
it("listLocked GETs the locked accounts", async () => {
|
|
mocks.apiGet.mockResolvedValue([]);
|
|
await listLocked();
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/login-security/locked");
|
|
});
|
|
|
|
it("unlock POSTs to the encoded username", async () => {
|
|
await unlock("a b/c");
|
|
expect(mocks.apiPost).toHaveBeenCalledWith("/api/login-security/locked/a%20b%2Fc/unlock");
|
|
});
|
|
|
|
it("getConfig / saveConfig use the config path", async () => {
|
|
await getConfig();
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/login-security/config");
|
|
const cfg = { enabled: true, maxAttempts: 5, period: 15, unit: "M" as const };
|
|
await saveConfig(cfg);
|
|
expect(mocks.apiPut).toHaveBeenCalledWith("/api/login-security/config", cfg);
|
|
});
|
|
|
|
it("getPasswordRules / savePasswordRules use the rules path", async () => {
|
|
await getPasswordRules();
|
|
expect(mocks.apiGet).toHaveBeenCalledWith("/api/login-security/password-rules");
|
|
const rules = [{ id: 1, status: "Y" as const, value: "8" }];
|
|
await savePasswordRules(rules);
|
|
expect(mocks.apiPut).toHaveBeenCalledWith("/api/login-security/password-rules", rules);
|
|
});
|
|
});
|