Files
ipal-kit/test/visitorWrites.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 4e68b7e44a 1.17.2 (owner's Q1-Q3): form submissions over REST for staff only; Payload's job queue for an admin or Bearer CRON_SECRET; doctor visitor-writes; explicit write rules on the reference site
- form-submissions create: a staff account with its second factor (formSubmissionOverrides.access.create keeps the way back)
- jobsRunAccess (default on when the project writes no jobs.access.run): admin with the second factor or Bearer CRON_SECRET (16+); start-up note when the secret is missing; jobsRunAccess: false keeps Payload's default
- iapl-doctor deep rule visitor-writes: entities a visitor login account may write, asked by behaviour
- reference: Pages, Posts, CaseStudies, Media, Header, Footer, Company get adminOrEditor write rules
- closed doors: submissions, pages, media, header, company closed for customers; the fixture's job queue refused without the secret, run with it
- route snapshot reasons, docs, AGENT.md, skills, changelog draft 1.17.2

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-01 10:44:18 +01:00

57 lines
3.4 KiB
TypeScript

/**
* The doctor's `visitor-writes` probe (1.17.2, the owner's Q3 answer (b)): on a site with a visitor login collection,
* every collection and global whose write rule lets such an account in — Payload's default "any signed-in account"
* included — is listed. Asked by behaviour on the built config, never by a rule's name.
*/
import type { Config, SanitizedConfig } from 'payload'
import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import { describe, expect, test, vi } from 'vitest'
import { adminOrEditor } from '../src/modules/access/access.js'
import { visitorWritableEntities } from '../src/modules/doctor/visitorWrites.js'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
const i18n = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
const build = (collections: Config['collections'], globals: Config['globals'] = []) =>
buildTestConfig({
admin: { user: 'users' },
collections: [{ slug: 'media', fields: [], upload: true }, ...(collections ?? [])],
db: sqliteAdapter({ client: { url: 'file::memory:' } }),
editor: lexicalEditor(),
globals,
plugins: [iapl({ access: { authCollection: 'users' }, forms: {}, i18n, twoFactor: false } as never)],
secret: 'visitor-writes-test',
telemetry: false,
} as unknown as Config)
/** The live shape the probe reads (`payload.collections`, `payload.config`), from a built config. */
const live = (config: SanitizedConfig) => ({ collections: Object.fromEntries(config.collections.map((c) => [c.slug, { config: c }])), config }) as never
describe('visitorWritableEntities', () => {
test('with a visitor login collection: Payload\'s defaults are listed, explicit staff rules and the plugin\'s own are not', async () => {
const config = await build(
[
{ slug: 'customers', access: { create: () => true, delete: () => false, update: ({ req }) => ({ id: { equals: req.user?.id } }) }, auth: true, fields: [] },
{ slug: 'pages', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] },
{ slug: 'posts', access: { create: adminOrEditor, delete: adminOrEditor, read: () => true, update: adminOrEditor }, fields: [{ name: 'title', type: 'text' }] },
],
[{ slug: 'header', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] }],
)
const open = await visitorWritableEntities(live(config))
expect(open).toEqual(expect.arrayContaining(['customers:create', 'globals/header:update', 'media:create', 'media:delete', 'media:update', 'pages:create', 'pages:delete', 'pages:update', 'payload-locked-documents:create']))
// explicit staff rules, a rule that answers a query, "nobody", the plugin's own entities
for (const closed of ['posts:create', 'posts:update', 'posts:delete', 'customers:update', 'customers:delete', 'form-submissions:create', 'forms:create', 'globals/site-settings:update', 'globals/site-integrations:update', 'payload-kv:create']) {
expect(open).not.toContain(closed)
}
}, 30_000)
test('a site with only the staff login collection: nothing to report', async () => {
const config = await build([{ slug: 'pages', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] }])
expect(await visitorWritableEntities(live(config))).toEqual([])
}, 30_000)
})