1
0
Fork 0
polaris-task-force/tests/int/modlist-file-access.int.spec.ts
Z8MB1E a6709e4cf3 fix(modlists): allow anonymous link download of preset files
The file-serving route (/api/mod-lists/file/<name>) runs the collection
read access with isReadingStaticFile: true, but mod-lists required the
mod-lists:read permission, so anyone clicking a shared download link got
403 until they signed in. Gate the read access on isReadingStaticFile: the
file-serving path serves the preset to anyone with the link, while REST and
admin-panel reads keep the mod-lists:read gate. Documented in
src/collections/AGENTS.md and covered by an integration test.
2026-10-31 15:01:29 -04:00

65 lines
2.2 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import { Payload } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { ModLists } from "@/collections/server/ModLists";
import { scopedAdminPageAccess } from "@/utils/access-control/divisionAccess";
let payload: Payload;
const TIMEOUT = 30_000;
/**
* Replicate the production wrapping exactly: payload.config.ts maps every
* collection through `scopedAdminPageAccess(slug, read)`, and the file-serving
* route (`/api/mod-lists/file/<name>`) invokes `read` with
* `isReadingStaticFile: true`. The vitest environment has no Next.js HTTP
* server, so invoking the access function directly is the standard way to
* unit-test Payload access control (see admin-page-access.int.spec.ts).
*/
const readAccess = scopedAdminPageAccess(ModLists.slug, ModLists.access.read);
/**
* A synthetic args object: non-admin pathname (REST/file route), no auth cookie.
* `isReadingStaticFile` is a sibling of `req` (Payload builds
* `{ data, isReadingStaticFile, req }` for the file route), not nested in `req`.
*/
const anonReq = (pathname: string, isReadingStaticFile?: boolean) => ({
isReadingStaticFile,
req: { user: null, payload, pathname },
});
describe("ModLists file access", () => {
beforeAll(async () => {
payload = await getTestPayload();
}, TIMEOUT);
afterAll(async () => {
// Nothing persists.
});
it(
"lets anyone with the link download the preset without signing in",
async () => {
// The file-serving route runs read with isReadingStaticFile: true.
const result = await readAccess(anonReq("/api/mod-lists/file/x.html", true));
expect(result).toBe(true);
},
TIMEOUT,
);
it(
"still requires mod-lists:read for anonymous REST and admin-panel reads",
async () => {
// A logged-out REST read of the document metadata is denied.
const api = await readAccess(anonReq("/api/mod-lists/1"));
expect(api).toBe(false);
// The admin-panel path keeps its permission gate (no user → denied).
const admin = await readAccess(anonReq("/admin/collections/mod-lists", true));
expect(admin).toBe(false);
},
TIMEOUT,
);
});