accounts.adminOnlyCreate (on): a staff collection without a create rule gets
"an admin, fully signed in" instead of Payload's "any logged-in user" — also the
project's own collection; a rule the project wrote stays.
accounts.firstUserRoute ('panel'): Payload's first-user route answers only for
the collection the panel logs in with. For any other empty auth collection it is
refused, unless that collection's own create rule lets an anonymous request in.
Checked and closed: an empty second staff collection, whose first visitor became
a staff admin (test/accountRulesOff.test.ts records the old behaviour, which
accounts: false / firstUserRoute: 'all' still give).
Doctor: open-signup (deep, info) names auth collections with open sign-up.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
78 lines
4.2 KiB
TypeScript
78 lines
4.2 KiB
TypeScript
/**
|
|
* `accounts: false` — Payload's own behaviour, which the M1 account rules replace by default. Kept as a test
|
|
* for two reasons: the switch must really give it back, and it records what the rules close, including the
|
|
* case the first check did not cover (an empty second staff collection).
|
|
*/
|
|
import type { Payload } from 'payload'
|
|
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, expect, test, vi } from 'vitest'
|
|
|
|
import { ipalKit } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
import { startPayloadServer, type TestServer } from './http/server.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
|
|
const PASSWORD = 'short1' // also: no password rule with accounts: false
|
|
let server: TestServer
|
|
let payload: Payload
|
|
const post = (pathname: string, body: unknown, headers: Record<string, string> = {}) =>
|
|
fetch(`${server.url}/api${pathname}`, { body: JSON.stringify(body), headers: { 'content-type': 'application/json', ...headers }, method: 'POST' })
|
|
const login = async (collection: string, email: string) => ({ authorization: `JWT ${((await (await post(`/${collection}/login`, { email, password: PASSWORD })).json()) as { token: string }).token}` })
|
|
|
|
beforeAll(async () => {
|
|
const config = buildTestConfig({
|
|
admin: { user: 'users' },
|
|
collections: [
|
|
{ slug: 'users', auth: true, fields: [] }, // the project's own staff collection, no access rules
|
|
{ slug: 'staff2', auth: true, fields: [] },
|
|
{ slug: 'members', auth: true, fields: [] },
|
|
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
|
|
],
|
|
db: testDatabase('accounts_off'),
|
|
editor: lexicalEditor(),
|
|
plugins: [ipalKit({ access: { authCollection: 'users', staffCollections: ['staff2'] }, accounts: false, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, twoFactor: false })],
|
|
secret: 'integration-test-secret-0123456789',
|
|
telemetry: false,
|
|
})
|
|
payload = await getPayload({ config })
|
|
server = await startPayloadServer(config)
|
|
}, 120_000)
|
|
|
|
afterAll(async () => {
|
|
await server?.close()
|
|
await payload?.destroy?.()
|
|
})
|
|
|
|
test('an empty second staff collection: its first-user route makes the first visitor a staff admin', async () => {
|
|
const res = await post('/staff2/first-register', { email: '[email protected]', password: PASSWORD })
|
|
expect(res.status).toBe(200)
|
|
// `staff2` has no roles field, so every account of it is an admin to the plugin: the secrets global opens
|
|
const session = await login('staff2', '[email protected]')
|
|
const integrations = await fetch(`${server.url}/api/globals/site-integrations`, { headers: session })
|
|
expect(integrations.status).toBe(200)
|
|
expect((await post('/globals/site-integrations', { smtpHost: 'changed-by-the-first-visitor.example.com' }, session)).status).toBe(200)
|
|
})
|
|
|
|
test("Payload's first-user route answers for every empty auth collection", async () => {
|
|
expect((await post('/members/first-register', { email: '[email protected]', password: PASSWORD })).status).toBe(200)
|
|
})
|
|
|
|
test("the project's own staff collection without a create rule: any signed-in account makes accounts", async () => {
|
|
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['admin'] } as never })
|
|
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['user'] } as never })
|
|
const plain = await login('users', '[email protected]')
|
|
const made = await post('/users', { email: '[email protected]', password: PASSWORD, roles: ['admin'] }, plain)
|
|
expect(made.status).toBe(201)
|
|
const doc = (await payload.find({ collection: 'users' as never, where: { email: { equals: '[email protected]' } } })).docs[0] as unknown as { roles: string[] }
|
|
expect(doc.roles).toEqual(['user']) // the roles field still refuses the requested admin
|
|
// and the plain user may open the panel
|
|
const access = (await (await fetch(`${server.url}/api/access`, { headers: plain })).json()) as { canAccessAdmin?: boolean }
|
|
expect(access.canAccessAdmin).toBe(true)
|
|
})
|