85 lines
2.7 KiB
TypeScript
85 lines
2.7 KiB
TypeScript
// @ts-nocheck
|
|
import { revalidatePath } from "next/cache";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { requirePermissionRateLimited } from "@/lib/admin/guard";
|
|
import { rcon } from "@/lib/services/rcon";
|
|
import { createBan, liftBan } from "./admin-bans";
|
|
|
|
const { selectLimit, insertValues, deleteWhere } = vi.hoisted(() => {
|
|
const selectLimit = vi.fn();
|
|
const insertValues = vi.fn().mockResolvedValue([{ insertId: 1 }]);
|
|
const deleteWhere = vi.fn().mockResolvedValue([{ affectedRows: 1 }]);
|
|
return { selectLimit, insertValues, deleteWhere };
|
|
});
|
|
|
|
vi.mock("@/lib/admin/guard", () => ({ requirePermissionRateLimited: vi.fn() }));
|
|
vi.mock("@/lib/permissions", () => ({ PERMS: { USERS_BAN: "users.ban" } }));
|
|
vi.mock("@/lib/db", () => ({
|
|
db: {
|
|
select: vi.fn(() => ({
|
|
from: vi.fn(() => ({
|
|
where: vi.fn(() => ({
|
|
limit: selectLimit,
|
|
})),
|
|
})),
|
|
})),
|
|
insert: vi.fn(() => ({ values: insertValues })),
|
|
delete: vi.fn(() => ({ where: deleteWhere })),
|
|
},
|
|
Ban: { id: "id", userId: "userId" },
|
|
User: { id: "id", username: "username" },
|
|
}));
|
|
vi.mock("@/lib/services/rcon", () => ({ rcon: { disconnectUser: vi.fn() } }));
|
|
vi.mock("@/lib/services/staff-activity", () => ({ logStaffActivity: vi.fn() }));
|
|
vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
|
|
|
|
const staff = { id: 1, rank: 7, username: "admin" };
|
|
const fakeForm = (data: Record<string, string>) => ({
|
|
get: (key: string) => data[key] ?? null,
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
vi.mocked(requirePermissionRateLimited).mockResolvedValue(staff as never);
|
|
selectLimit.mockResolvedValue([{ username: "baduser" }]);
|
|
insertValues.mockResolvedValue([{ insertId: 1 }]);
|
|
deleteWhere.mockResolvedValue([{ affectedRows: 1 }]);
|
|
});
|
|
|
|
describe("createBan", () => {
|
|
it("creates a ban for valid inputs", async () => {
|
|
await createBan(
|
|
fakeForm({
|
|
userId: "42",
|
|
reason: "Spam",
|
|
hours: "24",
|
|
type: "account",
|
|
}) as unknown as FormData,
|
|
);
|
|
expect(insertValues).toHaveBeenCalledWith(
|
|
expect.objectContaining({ userId: 42, type: "account" }),
|
|
);
|
|
expect(rcon.disconnectUser).toHaveBeenCalledWith(42, "baduser");
|
|
expect(revalidatePath).toHaveBeenCalledWith("/admin/bans");
|
|
});
|
|
|
|
it("returns early when userId is invalid", async () => {
|
|
await createBan(
|
|
fakeForm({
|
|
userId: "0",
|
|
hours: "1",
|
|
type: "account",
|
|
}) as unknown as FormData,
|
|
);
|
|
expect(insertValues).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("liftBan", () => {
|
|
it("deletes ban and revalidates", async () => {
|
|
await liftBan(fakeForm({ id: "42" }) as unknown as FormData);
|
|
expect(deleteWhere).toHaveBeenCalled();
|
|
expect(revalidatePath).toHaveBeenCalledWith("/admin/bans");
|
|
});
|
|
});
|