Files
EpicNext-Cms/src/actions/admin-bans.test.ts
T

70 lines
2.2 KiB
TypeScript

import { revalidatePath } from "next/cache";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { requirePermissionRateLimited } from "@/lib/admin/guard";
const { execute } = vi.hoisted(() => ({ execute: vi.fn() }));
vi.mock("@/features/housekeeping/domains/people/services/mutations", () => ({
createPeopleMutationInvocation: vi.fn((staff, correlationId) => ({
expectedActorId: staff.id,
correlationId,
legacy: true,
})),
peopleMutationService: { execute },
}));
vi.mock("@/lib/admin/guard", () => ({ requirePermissionRateLimited: vi.fn() }));
vi.mock("@/lib/permissions", () => ({ PERMS: { USERS_BAN: "users.ban" } }));
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
import { createBan, liftBan } from "./admin-bans";
const staff = { id: 1, rank: 7, username: "admin" };
const fakeForm = (data: Record<string, string>) =>
({ get: (key: string) => data[key] ?? null }) as unknown as FormData;
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(requirePermissionRateLimited).mockResolvedValue(staff as never);
execute.mockImplementation(async (invocation) => ({
ok: true,
data: { before: null, after: {} },
correlationId: invocation.correlationId,
}));
});
describe("legacy admin ban wrappers", () => {
it("preserves parsed create input, service delegation, and revalidation", async () => {
await createBan(
fakeForm({
userId: "42",
reason: "Spam",
hours: "24",
type: "account",
}),
);
expect(execute).toHaveBeenCalledWith(
expect.objectContaining({ expectedActorId: 1, legacy: true }),
"ban.create",
{ userId: 42, reason: "Spam", hours: 24, type: "account" },
);
expect(revalidatePath).toHaveBeenCalledWith("/admin/bans");
});
it("returns early when userId is invalid", async () => {
await createBan(
fakeForm({ userId: "0", hours: "1", type: "account" }),
);
expect(execute).not.toHaveBeenCalled();
});
it("delegates lift by exact ban id and preserves revalidation", async () => {
await liftBan(fakeForm({ id: "42" }));
expect(execute).toHaveBeenCalledWith(
expect.objectContaining({ expectedActorId: 1, legacy: true }),
"ban.lift",
{ id: 42 },
);
expect(revalidatePath).toHaveBeenCalledWith("/admin/bans");
});
});