/** * `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 { iapl } 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 = {}) => 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: [iapl({ 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: 'visitor@example.com', 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', 'visitor@example.com') 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: 'first@members.example.com', 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: 'admin@example.com', password: PASSWORD, roles: ['admin'] } as never }) await payload.create({ collection: 'users' as never, data: { email: 'plain@example.com', password: PASSWORD, roles: ['user'] } as never }) const plain = await login('users', 'plain@example.com') const made = await post('/users', { email: 'made-by-plain@example.com', password: PASSWORD, roles: ['admin'] }, plain) expect(made.status).toBe(201) const doc = (await payload.find({ collection: 'users' as never, where: { email: { equals: 'made-by-plain@example.com' } } })).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) })