Delete unused login and invite tokens on a daily job, and keep their URLs out of logs.
Expired sign-in, save-progress, email-change, and invitation tokens (and any draft they carry) are cleared by a Cloudron scheduler task. Page timing logs record the path without the query string, and mail failures log a request id instead of an address.
This commit is contained in:
1 parent
73d0856004
commit
46c34a6323
28 files changed
+636
-45
No files matched your search
@@ -0,0 +1,89 @@
|
||||
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,
|
||||
unusedInviteTokens: 3,
|
||||
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,
|
||||
unusedInviteTokens: 3,
|
||||
workshopInquiries: 4,
|
||||
});
|
||||
expect(pruneExpiredRecordsMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ tag: "prisma" }),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user