65 lines
4.0 KiB
TypeScript
65 lines
4.0 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 { applyLockedDocumentsAccess } from '../src/modules/security/queryGuard.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('Payload\'s document locks are not listed once iapl\'s staff-only rule is on (lockedDocuments, 1.18.0)', async () => {
|
|
const config = await build([{ slug: 'customers', access: { create: () => false }, auth: true, fields: [] }])
|
|
expect(await visitorWritableEntities(live(config))).toContain('payload-locked-documents:create')
|
|
applyLockedDocumentsAccess(config)
|
|
expect((await visitorWritableEntities(live(config))).filter((e) => e.startsWith('payload-locked-documents'))).toEqual([])
|
|
}, 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)
|
|
})
|