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:
1 parent
642551064c
commit
5cbda35e32
50 files changed
+2429
-219
No files matched your search
@@ -5,18 +5,61 @@ import { NextRequest } from "next/server";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const getUploadRootFromEnvMock = vi.fn();
|
||||
const isDatabaseConfiguredMock = vi.fn();
|
||||
const findUniqueMock = vi.fn();
|
||||
const getSessionUserMock = vi.fn();
|
||||
const getReadablePublishedRuleMock = vi.fn();
|
||||
const browserHoldsUploadGrantMock = vi.fn();
|
||||
let uploadRoot: string | null = null;
|
||||
|
||||
vi.mock("../../lib/server/uploads/uploadRoot", () => ({
|
||||
getUploadRootFromEnv: () => getUploadRootFromEnvMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/env", () => ({
|
||||
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/db", () => ({
|
||||
prisma: {
|
||||
upload: {
|
||||
findUnique: (...args: unknown[]) => findUniqueMock(...args),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/session", () => ({
|
||||
getSessionUser: () => getSessionUserMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/publishedRules", () => ({
|
||||
getReadablePublishedRule: (...args: unknown[]) =>
|
||||
getReadablePublishedRuleMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/uploads/uploadGrantCookie", () => ({
|
||||
browserHoldsUploadGrant: (...args: unknown[]) =>
|
||||
browserHoldsUploadGrantMock(...args),
|
||||
}));
|
||||
|
||||
import { GET } from "../../app/api/uploads/[id]/route";
|
||||
import { RULE_VISIBILITY } from "../../lib/ruleVisibility";
|
||||
|
||||
const FEATURED_ID = "550e8400-e29b-41d4-a716-446655440000";
|
||||
const PRIVATE_ID = "550e8400-e29b-41d4-a716-446655440001";
|
||||
|
||||
beforeEach(async () => {
|
||||
uploadRoot = await mkdtemp(path.join(tmpdir(), "cr-upload-test-"));
|
||||
getUploadRootFromEnvMock.mockReset();
|
||||
getUploadRootFromEnvMock.mockImplementation(() => uploadRoot);
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
findUniqueMock.mockReset();
|
||||
getSessionUserMock.mockReset();
|
||||
getSessionUserMock.mockResolvedValue(null);
|
||||
getReadablePublishedRuleMock.mockReset();
|
||||
browserHoldsUploadGrantMock.mockReset();
|
||||
browserHoldsUploadGrantMock.mockResolvedValue(false);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -34,6 +77,7 @@ describe("GET /api/uploads/[id]", () => {
|
||||
});
|
||||
|
||||
it("returns 404 when the upload is not found", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce(null);
|
||||
const res = await GET(
|
||||
new NextRequest(
|
||||
"https://x.test/api/uploads/550e8400-e29b-41d4-a716-446655440000",
|
||||
@@ -47,16 +91,93 @@ describe("GET /api/uploads/[id]", () => {
|
||||
expect(res.status).toBe(404);
|
||||
});
|
||||
|
||||
it("returns the file bytes with content type", async () => {
|
||||
const id = "550e8400-e29b-41d4-a716-446655440000";
|
||||
await writeFile(path.join(uploadRoot!, `${id}.png`), "png-bytes");
|
||||
it("returns featured file bytes with a short public cache", async () => {
|
||||
await writeFile(path.join(uploadRoot!, `${FEATURED_ID}.png`), "png-bytes");
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: FEATURED_ID,
|
||||
userId: null,
|
||||
draftId: null,
|
||||
ruleId: "rule-1",
|
||||
rule: { visibility: RULE_VISIBILITY.public_and_featured },
|
||||
draft: null,
|
||||
});
|
||||
getReadablePublishedRuleMock.mockResolvedValueOnce({
|
||||
visibility: RULE_VISIBILITY.public_and_featured,
|
||||
rule: { isListed: true },
|
||||
});
|
||||
const res = await GET(
|
||||
new NextRequest(`https://x.test/api/uploads/${id}`),
|
||||
{ params: Promise.resolve({ id }) },
|
||||
new NextRequest(`https://x.test/api/uploads/${FEATURED_ID}`),
|
||||
{ params: Promise.resolve({ id: FEATURED_ID }) },
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("Content-Type")).toBe("image/png");
|
||||
expect(res.headers.get("Cache-Control")).toContain("immutable");
|
||||
expect(res.headers.get("Cache-Control")).toBe("public, max-age=3600");
|
||||
expect(await res.text()).toBe("png-bytes");
|
||||
});
|
||||
|
||||
it("returns 404 for a private file the viewer cannot read", async () => {
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: PRIVATE_ID,
|
||||
userId: null,
|
||||
draftId: null,
|
||||
ruleId: "rule-private",
|
||||
rule: { visibility: RULE_VISIBILITY.private },
|
||||
draft: null,
|
||||
});
|
||||
getReadablePublishedRuleMock.mockResolvedValueOnce(null);
|
||||
const res = await GET(
|
||||
new NextRequest(`https://x.test/api/uploads/${PRIVATE_ID}`),
|
||||
{ params: Promise.resolve({ id: PRIVATE_ID }) },
|
||||
);
|
||||
expect(res.status).toBe(404);
|
||||
expect(res.headers.get("Cache-Control")).toBeNull();
|
||||
});
|
||||
|
||||
it("serves a shared-by-link file to an anonymous viewer without a public cache", async () => {
|
||||
await writeFile(path.join(uploadRoot!, `${PRIVATE_ID}.png`), "shared");
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: PRIVATE_ID,
|
||||
userId: null,
|
||||
draftId: null,
|
||||
ruleId: "rule-shared",
|
||||
rule: { visibility: RULE_VISIBILITY.shared_by_link },
|
||||
draft: null,
|
||||
});
|
||||
getReadablePublishedRuleMock.mockResolvedValueOnce({
|
||||
visibility: RULE_VISIBILITY.shared_by_link,
|
||||
rule: { isListed: false },
|
||||
});
|
||||
const res = await GET(
|
||||
new NextRequest(`https://x.test/api/uploads/${PRIVATE_ID}`),
|
||||
{ params: Promise.resolve({ id: PRIVATE_ID }) },
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(getReadablePublishedRuleMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ viaShareSlug: true }),
|
||||
);
|
||||
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
|
||||
});
|
||||
|
||||
it("serves a private file to a permitted reader without a public cache", async () => {
|
||||
await writeFile(path.join(uploadRoot!, `${PRIVATE_ID}.png`), "private");
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: PRIVATE_ID,
|
||||
userId: "user-1",
|
||||
draftId: null,
|
||||
ruleId: "rule-private",
|
||||
rule: { visibility: RULE_VISIBILITY.private },
|
||||
draft: null,
|
||||
});
|
||||
getReadablePublishedRuleMock.mockResolvedValueOnce({
|
||||
visibility: RULE_VISIBILITY.private,
|
||||
rule: { isListed: false },
|
||||
});
|
||||
const res = await GET(
|
||||
new NextRequest(`https://x.test/api/uploads/${PRIVATE_ID}`),
|
||||
{ params: Promise.resolve({ id: PRIVATE_ID }) },
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(res.headers.get("Cache-Control")).toBe("private, no-store");
|
||||
expect(await res.text()).toBe("private");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user