Files
ipal-kit/test/accountRulesNobodyManagesSuite.ts
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

97 lines
6.6 KiB
TypeScript

/**
* The start-up warning "no staff account holds admin" against a RUNNING Payload (review of branch A, K-2):
* `staffCollectionsNobodyManages` for each shape of `roles` field, in three states — the collection empty, with a
* lowest account, with its top account — and what it must say. It was silent or wrong where a query answers
* differently per database (a value outside a Postgres enum, json on SQL, a relationship). Now each collection is
* checked in the way that is right for its field. SQLite, Postgres, MongoDB.
*/
import type { CollectionConfig, Field, Payload } from 'payload'
import { payloadTotp } from '@clocklimited/payload-2fa'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { getPayload } from 'payload'
import { expect, test } from 'vitest'
import { staffCollectionsNobodyManages } from '../src/modules/access/selfRegistration.js'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
import { testDatabase } from './http/db.js'
const PASSWORD = 'a-long-test-password-1'
const select = (options: string[], hasMany = true): Field => ({ name: 'roles', type: 'select', hasMany, options }) as Field
/** `adminTop`: its top account IS a plugin admin; `ruled`: the collection runs on the admin rules. */
const SHAPES: { adminTop: boolean; fields: Field[]; low: unknown; ruled: boolean; slug: string; top: unknown }[] = [
{ slug: 'crew', adminTop: true, fields: [select(['user', 'editor', 'admin', 'boss'])], low: ['user'], ruled: true, top: ['admin'] },
{ slug: 'in-tabs', adminTop: true, fields: [{ type: 'tabs', tabs: [{ fields: [select(['user', 'editor', 'admin'])], label: 'Account' }] }], low: ['user'], ruled: true, top: ['admin'] },
{ slug: 'text-many', adminTop: true, fields: [{ name: 'roles', type: 'text', hasMany: true }], low: ['user'], ruled: true, top: ['admin'] },
// no `admin` among the values: nobody in it can be one — the editor is not
{ slug: 'viewers', adminTop: false, fields: [select(['viewer', 'editor'])], low: ['viewer'], ruled: true, top: ['editor'] },
{ slug: 'json-roles', adminTop: true, fields: [{ name: 'roles', type: 'json' }], low: ['user'], ruled: true, top: ['admin'] },
{ slug: 'rel-roles', adminTop: true, fields: [{ name: 'roles', type: 'relationship', hasMany: true, relationTo: 'role-names' }], low: ['user'], ruled: true, top: ['admin'] },
// single-valued: the value never counts, the collection is not on the admin rules, its 'admin' is not an admin
{ slug: 'single', adminTop: false, fields: [select(['user', 'editor', 'admin'], false)], low: 'user', ruled: false, top: 'admin' },
// roles of its own, and a collection with rules of its own: not on the admin rules
{ slug: 'team', adminTop: false, fields: [select(['lead', 'member'])], low: ['member'], ruled: false, top: ['lead'] },
]
/** One Payload instance per test file: run from a file of its own for 2FA off and on. */
export function nobodyManagesSuite(twoFactor: boolean): void {
test(`the start-up check says who has no admin — per collection, on this database (2FA on: ${twoFactor})`, async () => {
const config = buildTestConfig({
admin: { user: 'users' },
collections: [
...SHAPES.map(({ slug, fields }): CollectionConfig => ({ slug, auth: true, fields })),
{ slug: 'own-rules', access: { create: () => false, delete: () => false, update: () => false }, auth: true, fields: [select(['user', 'editor', 'admin', 'boss'])] },
{ slug: 'role-names', fields: [{ name: 'id', type: 'text' }] },
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
],
db: testDatabase(twoFactor ? 'nobody_2fa' : 'nobody'),
editor: lexicalEditor(),
plugins: [
iapl({
access: { authCollection: 'users', staffCollections: [...SHAPES.map((shape) => shape.slug), 'own-rules'] },
i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] },
twoFactor: twoFactor ? { issuer: 'Example', plugin: payloadTotp, verifyLimit: false } : false,
}),
],
secret: 'integration-test-secret-0123456789',
telemetry: false,
})
const payload: Payload = await getPayload({ config })
const ask = (staff: string[]) => staffCollectionsNobodyManages(payload as never, staff, 'users')
const make = (collection: string, email: string, roles?: unknown) => payload.create({ collection: collection as never, data: { email, password: PASSWORD, ...(roles === undefined ? {} : { roles }) } as never })
try {
for (const id of ['user', 'admin', 'editor']) {await payload.create({ collection: 'role-names' as never, data: { id } as never })}
const said: Record<string, unknown[]> = {}
const should: Record<string, unknown[]> = {}
for (const { slug, adminTop, low, ruled, top } of SHAPES) {
const nobody = { stuck: ruled ? [slug] : [], unchecked: [] }
const states: unknown[] = [await ask(['users', slug])] // empty
await make(slug, `low@${slug}.example.com`, low)
states.push(await ask(['users', slug])) // a lowest account
await make(slug, `top@${slug}.example.com`, top)
states.push(await ask(['users', slug])) // its top account
said[slug] = states
// with the top account: silent where that account is a plugin admin; still "nobody" where it is not
should[slug] = [nobody, nobody, adminTop ? { stuck: [], unchecked: [] } : nobody]
}
expect(said).toEqual(should)
// several collections in one start: each is checked on its own — one that the database cannot be asked about
// does not silence the others, and an admin anywhere answers for all
await payload.delete({ collection: 'crew' as never, where: { email: { equals: '[email protected]' } } })
for (const slug of ['in-tabs', 'text-many', 'json-roles', 'rel-roles']) {await payload.delete({ collection: slug as never, where: { email: { equals: `top@${slug}.example.com` } } })}
const all = ['users', ...SHAPES.map((shape) => shape.slug), 'own-rules']
expect(await ask(all)).toEqual({ stuck: SHAPES.filter((shape) => shape.ruled).map((shape) => shape.slug).sort(), unchecked: [] })
// a collection with rules of its own is not on the admin rules: it is never named
expect((await ask(['users', 'own-rules'])).stuck).toEqual([])
// an admin of the panel's collection: silent everywhere
await make('users', '[email protected]', ['admin'])
expect(await ask(all)).toEqual({ stuck: [], unchecked: [] })
} finally {
await payload.destroy?.()
}
}, 120_000)
}