/** * Journals access rules: authors own their entries (drafts private), every * logged-in user reads published entries, staff permissions bypass ownership. */ import { getPayload, Payload } from "payload"; import config from "@/payload.config"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import type { Journal, Role, User } from "@/payload-types"; import type { Permission } from "@/permissions"; let payload: Payload; const RUN = `jn-${Date.now().toString(36)}`; const TIMEOUT = 30_000; function journalBody(text: string): Journal["body"] { return { root: { type: "root", format: "", indent: 0, version: 1, direction: null, children: [ { type: "paragraph", format: "", indent: 0, version: 1, direction: null, textFormat: 0, textStyle: "", children: [ { type: "text", format: 0, version: 1, mode: "normal", style: "", text, detail: 0 }, ], }, ], }, } as Journal["body"]; } describe("Journals access control", () => { const roleIds: number[] = []; const userIds: number[] = []; const journalIds: number[] = []; let authorUser: User; let readerUser: User; let staffUser: User; const makeRole = async (label: string, permissions: Permission[]): Promise => { const role = (await payload.create({ collection: "roles", data: { name: `${RUN}-${label}`, slug: `${RUN}-${label}`, permissions }, overrideAccess: true, depth: 0, })) as unknown as Role; roleIds.push(role.id); return role; }; const makeUser = async (label: string, roleId: number): Promise => { const user = (await payload.create({ collection: "users", data: { username: `${RUN}-${label}`, discordUsername: `${RUN}-${label}`, displayName: label.toUpperCase(), steamId: `7656119${Math.floor(Math.random() * 1e9)}`, password: "Test123", roleDocs: [roleId], }, overrideAccess: true, depth: 0, })) as unknown as User; userIds.push(user.id); return user; }; beforeAll(async () => { payload = await getPayload({ config }); const plainRole = await makeRole("plain", []); const staffRole = await makeRole("staff", [ "journals:read", "journals:update", "journals:delete", ]); authorUser = await makeUser("author", plainRole.id); readerUser = await makeUser("reader", plainRole.id); staffUser = await makeUser("staff", staffRole.id); }, TIMEOUT); afterAll(async () => { for (const id of journalIds) { await payload.delete({ collection: "journals", id, overrideAccess: true }).catch(() => {}); } for (const id of userIds) { await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {}); } for (const id of roleIds) { await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {}); } }, TIMEOUT); const createDraft = async (author: User): Promise => { const doc = (await payload.create({ collection: "journals", data: { title: `${RUN} draft`, author: author.id, status: "draft" as const, body: journalBody("Field notes body."), }, overrideAccess: false, user: author, depth: 0, })) as unknown as Journal; journalIds.push(doc.id); return doc; }; it( "lets any logged-in member create an entry, owned by them, defaulting to draft", async () => { const doc = await createDraft(authorUser); expect(doc.status).toBe("draft"); expect(typeof doc.author === "number" ? doc.author : doc.author.id).toBe(authorUser.id); }, TIMEOUT, ); it( "keeps drafts invisible to other members but visible to the author and staff", async () => { const doc = await createDraft(authorUser); const asReader = await payload.find({ collection: "journals", where: { id: { equals: doc.id } }, user: readerUser, overrideAccess: false, }); expect(asReader.totalDocs).toBe(0); const asAuthor = await payload.find({ collection: "journals", where: { id: { equals: doc.id } }, user: authorUser, overrideAccess: false, }); expect(asAuthor.totalDocs).toBe(1); const asStaff = await payload.find({ collection: "journals", where: { id: { equals: doc.id } }, user: staffUser, overrideAccess: false, }); expect(asStaff.totalDocs).toBe(1); }, TIMEOUT, ); it( "opens published entries to every logged-in member", async () => { const doc = await createDraft(authorUser); await payload.update({ collection: "journals", id: doc.id, data: { status: "published" }, user: authorUser, overrideAccess: false, }); const asReader = await payload.findByID({ collection: "journals", id: doc.id, user: readerUser, overrideAccess: false, }); expect(asReader.title).toBe(`${RUN} draft`); }, TIMEOUT, ); it( "blocks non-authors from updating or deleting, allows the author", async () => { const doc = await createDraft(authorUser); await expect( payload.update({ collection: "journals", id: doc.id, data: { title: "hijacked" }, user: readerUser, overrideAccess: false, }), ).rejects.toThrow(); await expect( payload.delete({ collection: "journals", id: doc.id, user: readerUser, overrideAccess: false, }), ).rejects.toThrow(); const updated = (await payload.update({ collection: "journals", id: doc.id, data: { title: `${RUN} edited` }, user: authorUser, overrideAccess: false, depth: 0, })) as unknown as Journal; expect(updated.title).toBe(`${RUN} edited`); await payload.delete({ collection: "journals", id: doc.id, user: authorUser, overrideAccess: false, }); const gone = await payload.find({ collection: "journals", where: { id: { equals: doc.id } }, user: staffUser, overrideAccess: false, }); expect(gone.totalDocs).toBe(0); }, TIMEOUT, ); it( "lets journals:update staff edit another member's entry", async () => { const doc = await createDraft(authorUser); const updated = (await payload.update({ collection: "journals", id: doc.id, data: { title: `${RUN} staff edit` }, user: staffUser, overrideAccess: false, depth: 0, })) as unknown as Journal; expect(updated.title).toBe(`${RUN} staff edit`); }, TIMEOUT, ); });