Start every Rule private, with a rotating link and files that follow its readers.

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.
This commit is contained in:
adilallo committed 2026-11-09 19:54:04 -07:00
1 parent 642551064c
commit 5cbda35e32
50 files changed
+2429 -219

No files matched your search

+117 -71
View File
@@ -4,6 +4,9 @@ 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(),
@@ -21,17 +24,54 @@ 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]", () => {
@@ -86,80 +126,39 @@ describe("GET /api/rules/[id]", () => {
expect(res.status).toBe(404);
});
it("returns 200 with { rule, viewerIsOwner: false } when a published rule exists and the viewer is anonymous", async () => {
it("returns 404 for a private Rule when the viewer is anonymous", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
const row = {
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,
};
findUniqueMock.mockResolvedValueOnce(row);
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;
title: string;
summary: string | null;
isListed: boolean;
};
rule: { id: string; isListed: boolean };
viewerIsOwner: boolean;
};
expect(body.rule.id).toBe("rule-1");
expect(body.rule.title).toBe("Mutual Aid Mondays");
expect(body.rule.summary).toBe("A grassroots community in Denver.");
expect(body.rule.isListed).toBe(true);
expect(body.viewerIsOwner).toBe(false);
expect(findUniqueMock).toHaveBeenCalledTimes(1);
});
it("marks guest-claim rules as unlisted without exposing the claim token", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
findUniqueMock.mockResolvedValueOnce({
id: "guest-1",
title: "Unlisted",
summary: null,
document: {},
createdAt: new Date("2026-01-01T00:00:00Z"),
updatedAt: new Date("2026-01-02T00:00:00Z"),
userId: null,
claimTokenHash: "secret-hash",
});
const res = await GET(
new NextRequest("https://x.test/api/rules/guest-1"),
makeContext("guest-1"),
);
expect(res.status).toBe(200);
const body = (await res.json()) as {
rule: { isListed: boolean; claimTokenHash?: unknown; userId?: unknown };
};
expect(body.rule.isListed).toBe(false);
expect(body.rule).not.toHaveProperty("claimTokenHash");
expect(body.rule).not.toHaveProperty("userId");
});
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" });
const row = {
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"),
};
findUniqueMock
.mockResolvedValueOnce(row)
.mockResolvedValueOnce({ userId: "user-1" });
findUniqueMock.mockResolvedValueOnce(row());
const res = await GET(
new NextRequest("https://x.test/api/rules/rule-1"),
makeContext("rule-1"),
@@ -167,23 +166,70 @@ describe("GET /api/rules/[id]", () => {
expect(res.status).toBe(200);
const body = (await res.json()) as { viewerIsOwner: boolean };
expect(body.viewerIsOwner).toBe(true);
expect(findUniqueMock).toHaveBeenCalledTimes(2);
expect(findUniqueMock).toHaveBeenCalledTimes(1);
});
it("returns viewerIsOwner false when the signed-in user does not own the rule", async () => {
it("returns 404 for a shared-by-link Rule at the internal id", async () => {
isDatabaseConfiguredMock.mockReturnValue(true);
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
const row = {
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"),
};
findUniqueMock
.mockResolvedValueOnce(row)
.mockResolvedValueOnce({ userId: "other" });
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"),