New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
242 lines
7.8 KiB
TypeScript
242 lines
7.8 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const isDatabaseConfiguredMock = vi.fn();
|
|
const findUniqueMock = vi.fn();
|
|
const getSessionUserMock = vi.fn();
|
|
const browserHoldsClaimForHashMock = vi.fn();
|
|
const isRuleAdminEmailMock = vi.fn();
|
|
const logAdminRuleReadMock = vi.fn();
|
|
|
|
vi.mock("../../lib/server/env", () => ({
|
|
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/db", () => ({
|
|
prisma: {
|
|
publishedRule: {
|
|
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
|
},
|
|
},
|
|
}));
|
|
|
|
vi.mock("../../lib/server/session", () => ({
|
|
getSessionUser: () => getSessionUserMock(),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/guestRuleClaim", () => ({
|
|
browserHoldsClaimForHash: (...args: unknown[]) =>
|
|
browserHoldsClaimForHashMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/ruleAdmins", () => ({
|
|
isRuleAdminEmail: (...args: unknown[]) => isRuleAdminEmailMock(...args),
|
|
}));
|
|
|
|
vi.mock("../../lib/server/adminRuleReadLog", () => ({
|
|
logAdminRuleRead: (...args: unknown[]) => logAdminRuleReadMock(...args),
|
|
}));
|
|
|
|
import { GET } from "../../app/api/rules/[id]/route";
|
|
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
|
|
|
|
function makeContext(id: string) {
|
|
return { params: Promise.resolve({ id }) };
|
|
}
|
|
|
|
function row(overrides: Record<string, unknown> = {}) {
|
|
return {
|
|
id: "rule-1",
|
|
title: "Mutual Aid Mondays",
|
|
summary: "A grassroots community in Denver.",
|
|
document: { sections: [] },
|
|
createdAt: new Date("2026-01-01T00:00:00Z"),
|
|
updatedAt: new Date("2026-01-02T00:00:00Z"),
|
|
userId: "user-1",
|
|
claimTokenHash: null,
|
|
visibility: RULE_VISIBILITY.private,
|
|
shareSlug: null,
|
|
stakeholders: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
isDatabaseConfiguredMock.mockReset();
|
|
findUniqueMock.mockReset();
|
|
getSessionUserMock.mockReset();
|
|
browserHoldsClaimForHashMock.mockReset();
|
|
isRuleAdminEmailMock.mockReset();
|
|
logAdminRuleReadMock.mockReset();
|
|
getSessionUserMock.mockResolvedValue(null);
|
|
browserHoldsClaimForHashMock.mockResolvedValue(false);
|
|
isRuleAdminEmailMock.mockReturnValue(false);
|
|
logAdminRuleReadMock.mockResolvedValue(undefined);
|
|
});
|
|
|
|
describe("GET /api/rules/[id]", () => {
|
|
it("returns 503 when the database is not configured", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(false);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/abc"),
|
|
makeContext("abc"),
|
|
);
|
|
expect(res.status).toBe(503);
|
|
expect(findUniqueMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("returns 404 with the canonical error shape when no published rule matches the id", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(null);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/missing"),
|
|
makeContext("missing"),
|
|
);
|
|
expect(res.status).toBe(404);
|
|
expect(res.headers.get("x-request-id")).toBeTruthy();
|
|
const body = (await res.json()) as {
|
|
error: { code: string; message: string };
|
|
};
|
|
expect(body.error.code).toBe("not_found");
|
|
expect(typeof body.error.message).toBe("string");
|
|
expect(findUniqueMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({ where: { id: "missing" } }),
|
|
);
|
|
});
|
|
|
|
it("forwards an incoming x-request-id on the response", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(null);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/missing", {
|
|
headers: { "x-request-id": "req_test-1" },
|
|
}),
|
|
makeContext("missing"),
|
|
);
|
|
expect(res.headers.get("x-request-id")).toBe("req_test-1");
|
|
});
|
|
|
|
it("returns 404 when the query throws (swallowed by helper)", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockRejectedValueOnce(new Error("db down"));
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/broken"),
|
|
makeContext("broken"),
|
|
);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 404 for a private Rule when the viewer is anonymous", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(row());
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 200 for a public and featured Rule when the viewer is anonymous", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(
|
|
row({ visibility: RULE_VISIBILITY.public_and_featured }),
|
|
);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as {
|
|
rule: { id: string; isListed: boolean };
|
|
viewerIsOwner: boolean;
|
|
};
|
|
expect(body.rule.id).toBe("rule-1");
|
|
expect(body.rule.isListed).toBe(true);
|
|
expect(body.viewerIsOwner).toBe(false);
|
|
});
|
|
|
|
it("returns viewerIsOwner true when the signed-in user owns the rule", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
|
|
findUniqueMock.mockResolvedValueOnce(row());
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as { viewerIsOwner: boolean };
|
|
expect(body.viewerIsOwner).toBe(true);
|
|
expect(findUniqueMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("returns 404 for a shared-by-link Rule at the internal id", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(
|
|
row({
|
|
visibility: RULE_VISIBILITY.shared_by_link,
|
|
shareSlug: "k7f2-9qxm",
|
|
}),
|
|
);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
it("returns 200 when the browser holds the guest claim", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
browserHoldsClaimForHashMock.mockResolvedValue(true);
|
|
findUniqueMock.mockResolvedValueOnce(
|
|
row({ userId: null, claimTokenHash: "claim-hash" }),
|
|
);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
});
|
|
|
|
it("logs named-admin reads of a private Rule", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
isRuleAdminEmailMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue({
|
|
id: "admin-1",
|
|
email: "ops@example.org",
|
|
});
|
|
findUniqueMock.mockResolvedValueOnce(row());
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
expect(logAdminRuleReadMock).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
ruleId: "rule-1",
|
|
adminEmail: "ops@example.org",
|
|
path: "/api/rules/rule-1",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("returns 200 for an invited reader of a private Rule", async () => {
|
|
isDatabaseConfiguredMock.mockReturnValue(true);
|
|
getSessionUserMock.mockResolvedValue({
|
|
id: "reader-1",
|
|
email: "rosa@example.org",
|
|
});
|
|
findUniqueMock.mockResolvedValueOnce(
|
|
row({
|
|
stakeholders: [{ email: "rosa@example.org", userId: "reader-1" }],
|
|
}),
|
|
);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/rules/rule-1"),
|
|
makeContext("rule-1"),
|
|
);
|
|
expect(res.status).toBe(200);
|
|
const body = (await res.json()) as { viewerIsOwner: boolean };
|
|
expect(body.viewerIsOwner).toBe(false);
|
|
});
|
|
});
|