188 lines
6.1 KiB
TypeScript
188 lines
6.1 KiB
TypeScript
import { mkdtemp, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
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(() => {
|
|
uploadRoot = null;
|
|
});
|
|
|
|
describe("GET /api/uploads/[id]", () => {
|
|
it("returns 500 when UPLOAD_ROOT is unset", async () => {
|
|
getUploadRootFromEnvMock.mockReturnValueOnce(null);
|
|
const res = await GET(
|
|
new NextRequest("https://x.test/api/uploads/upload-1"),
|
|
{ params: Promise.resolve({ id: "upload-1" }) },
|
|
);
|
|
expect(res.status).toBe(500);
|
|
});
|
|
|
|
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",
|
|
),
|
|
{
|
|
params: Promise.resolve({
|
|
id: "550e8400-e29b-41d4-a716-446655440000",
|
|
}),
|
|
},
|
|
);
|
|
expect(res.status).toBe(404);
|
|
});
|
|
|
|
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/${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")).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({
|
|
idOrSlug: "rule-shared",
|
|
viaShareSlug: true,
|
|
lookup: "id",
|
|
}),
|
|
);
|
|
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");
|
|
});
|
|
});
|