import { getTestPayload } from "../helpers/get-test-payload"; import { Payload } from "payload"; import type { AccessArgs, AccessResult } 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/`) 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) as ( args: AccessArgs, ) => Promise; /** * 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): AccessArgs => ({ isReadingStaticFile, req: { user: null, payload, pathname }, }) as unknown as AccessArgs; 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, ); });