Responses had no CSP, the 48-hour download token never changed, and short share slugs could be guessed without a miss limit.
137 lines
4.0 KiB
TypeScript
137 lines
4.0 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { ruleExportToken } from "../../lib/server/ruleExportToken";
|
|
|
|
const isDatabaseConfiguredMock = vi.fn();
|
|
const getSessionPepperMock = vi.fn();
|
|
const findUniqueMock = vi.fn();
|
|
const getSessionUserMock = vi.fn();
|
|
const isRuleAdminEmailMock = vi.fn();
|
|
|
|
vi.mock("../../lib/server/env", () => ({
|
|
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
|
getSessionPepper: () => getSessionPepperMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/db", () => ({
|
|
prisma: {
|
|
publishedRule: {
|
|
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../lib/server/session", () => ({
|
|
getSessionUser: () => getSessionUserMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/ruleAdmins", () => ({
|
|
isRuleAdminEmail: (...args: unknown[]) => isRuleAdminEmailMock(...args),
|
|
}));
|
|
|
|
import { GET } from "../../app/api/rules/[id]/export/[format]/route";
|
|
|
|
const PEPPER = "test-pepper-16chars";
|
|
|
|
function makeContext(id: string, format: string) {
|
|
return { params: Promise.resolve({ id, format }) };
|
|
}
|
|
|
|
function row() {
|
|
return {
|
|
id: "rule-1",
|
|
title: "Garden Norms",
|
|
summary: "We share tools.",
|
|
document: {
|
|
sections: [
|
|
{
|
|
categoryName: "Values",
|
|
entries: [{ title: "Solidarity", body: "Be kind." }],
|
|
},
|
|
],
|
|
},
|
|
createdAt: new Date("2026-11-08T00:00:00.000Z"),
|
|
userId: "user-1",
|
|
exportTokenVersion: 0,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
isDatabaseConfiguredMock.mockReset();
|
|
getSessionPepperMock.mockReset();
|
|
findUniqueMock.mockReset();
|
|
getSessionUserMock.mockReset();
|
|
isRuleAdminEmailMock.mockReset();
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionPepperMock.mockReturnValue(PEPPER);
|
|
getSessionUserMock.mockResolvedValue(null);
|
|
isRuleAdminEmailMock.mockReturnValue(false);
|
|
findUniqueMock.mockResolvedValue(row());
|
|
});
|
|
|
|
describe("GET /api/rules/[id]/export/[format]", () => {
|
|
it("returns 404 for an unknown format", async () => {
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1/export/csv"),
|
|
makeContext("rule-1", "csv"),
|
|
);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 401 without a token or session", async () => {
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1/export/pdf"),
|
|
makeContext("rule-1", "pdf"),
|
|
);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
|
|
it("returns a PDF attachment when the export token matches", async () => {
|
|
const token = ruleExportToken("rule-1", PEPPER, 0);
|
|
const res = await GET(
|
|
new NextRequest(
|
|
`https://x.test/api/rules/rule-1/export/pdf?t=${token}`,
|
|
),
|
|
makeContext("rule-1", "pdf"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get("content-type")).toBe("application/pdf");
|
|
expect(res.headers.get("content-disposition")).toContain(
|
|
"Garden Norms Rule.pdf",
|
|
);
|
|
const buf = Buffer.from(await res.arrayBuffer());
|
|
expect(buf.subarray(0, 5).toString()).toBe("%PDF-");
|
|
});
|
|
|
|
it("returns RULE.md for the owner without a token", async () => {
|
|
getSessionUserMock.mockResolvedValue({
|
|
id: "user-1",
|
|
email: "owner@example.com",
|
|
});
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1/export/md"),
|
|
makeContext("rule-1", "md"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
expect(res.headers.get("content-type")).toContain("text/markdown");
|
|
const text = await res.text();
|
|
expect(text).toContain("# Garden Norms");
|
|
expect(text).toContain("## Shared terms");
|
|
});
|
|
|
|
it("rejects a mailed token after the share link is turned off", async () => {
|
|
const token = ruleExportToken("rule-1", PEPPER, 0);
|
|
findUniqueMock.mockResolvedValueOnce({
|
|
...row(),
|
|
exportTokenVersion: 1,
|
|
});
|
|
const res = await GET(
|
|
new NextRequest(
|
|
`https://x.test/api/rules/rule-1/export/pdf?t=${token}`,
|
|
),
|
|
makeContext("rule-1", "pdf"),
|
|
);
|
|
expect(res.status).toBe(401);
|
|
});
|
|
});
|