Clearing the hash made the expired page report success without sending, and made the invite look accepted.
88 lines
2.6 KiB
TypeScript
88 lines
2.6 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const isDatabaseConfiguredMock = vi.fn();
|
|
const getOpsPruneTokenMock = vi.fn();
|
|
const pruneExpiredRecordsMock = vi.fn();
|
|
const retryPendingWorkshopInquiryMailMock = vi.fn();
|
|
|
|
vi.mock("../../lib/server/env", () => ({
|
|
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
|
getOpsPruneToken: () => getOpsPruneTokenMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/db", () => ({
|
|
prisma: { tag: "prisma" },
|
|
}));
|
|
|
|
vi.mock("../../lib/server/pruneExpiredRecords", () => ({
|
|
pruneExpiredRecords: (...args: unknown[]) =>
|
|
pruneExpiredRecordsMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/workshopSignup", () => ({
|
|
retryPendingWorkshopInquiryMail: () =>
|
|
retryPendingWorkshopInquiryMailMock(),
|
|
}));
|
|
|
|
import { POST } from "../../app/api/ops/prune/route";
|
|
|
|
function post(auth?: string) {
|
|
return new NextRequest("https://x.test/api/ops/prune", {
|
|
method: "POST",
|
|
headers: auth ? { authorization: auth } : undefined,
|
|
});
|
|
}
|
|
|
|
beforeEach(() => {
|
|
isDatabaseConfiguredMock.mockReset();
|
|
getOpsPruneTokenMock.mockReset();
|
|
pruneExpiredRecordsMock.mockReset();
|
|
retryPendingWorkshopInquiryMailMock.mockReset();
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
pruneExpiredRecordsMock.mockResolvedValue({
|
|
sessions: 1,
|
|
magicLinkTokens: 2,
|
|
emailChangeTokens: 0,
|
|
workshopInquiries: 4,
|
|
});
|
|
retryPendingWorkshopInquiryMailMock.mockResolvedValue(1);
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs();
|
|
});
|
|
|
|
describe("POST /api/ops/prune", () => {
|
|
it("returns 404 when the token is unset", async () => {
|
|
getOpsPruneTokenMock.mockReturnValue(null);
|
|
const res = await POST(post("Bearer secret"), undefined);
|
|
expect(res.status).toBe(404);
|
|
expect(pruneExpiredRecordsMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns 401 when the bearer token does not match", async () => {
|
|
getOpsPruneTokenMock.mockReturnValue("secret");
|
|
const res = await POST(post("Bearer other"), undefined);
|
|
expect(res.status).toBe(401);
|
|
expect(pruneExpiredRecordsMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("prunes expired rows when authorized", async () => {
|
|
getOpsPruneTokenMock.mockReturnValue("secret");
|
|
const res = await POST(post("Bearer secret"), undefined);
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({
|
|
ok: true,
|
|
retried: 1,
|
|
sessions: 1,
|
|
magicLinkTokens: 2,
|
|
emailChangeTokens: 0,
|
|
workshopInquiries: 4,
|
|
});
|
|
expect(pruneExpiredRecordsMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ tag: "prisma" }),
|
|
);
|
|
});
|
|
});
|