import type { Access, CollectionConfig, Config, Field, GlobalConfig } from 'payload' import { describe, expect, test, vi } from 'vitest' import type { I18nConfig } from '../src/modules/i18n/types.js' import { ipalKit } from '../src/plugin.js' const i18n: I18nConfig = { defaultLocale: 'pl', locales: [ { code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }, ], } const empty = (extra: Partial = {}): Config => ({ collections: [], secret: 'x', ...extra }) as unknown as Config const admin = { id: 1, roles: ['admin'] } const editor = { id: 2, roles: ['editor'] } const user = { id: 3, roles: ['user'] } /** Runs a collection/global access function the way Payload does. */ const run = (fn: Access | undefined, u: unknown) => fn ? fn({ req: { user: u } } as never) : 'payload-default' describe('injected auth collection', () => { test('with roles: a plain user can only see and edit itself, never create or delete', async () => { const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty()) const access = config.collections?.find((c) => c.slug === 'users')?.access ?? {} expect(run(access.update, user)).toEqual({ id: { equals: 3 } }) expect(run(access.read, user)).toEqual({ id: { equals: 3 } }) expect(run(access.delete, user)).toBe(false) expect(run(access.create, user)).toBe(false) expect(run(access.delete, editor)).toBe(false) expect(run(access.update, null)).toBe(false) expect(run(access.update, admin)).toBe(true) expect(run(access.delete, admin)).toBe(true) expect(run(access.create, admin)).toBe(true) }) test("the project's own auth collection keeps its own access", async () => { const own = { slug: 'users', access: { read: () => true }, auth: true, fields: [] } const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })( empty({ collections: [own] as CollectionConfig[] }), ) expect(config.collections?.find((c) => c.slug === 'users')?.access).toBe(own.access) }) }) describe('2FA wraps the auth collection people log in with', () => { const totp = () => vi.fn((_: unknown) => (c: Config) => c) test('defaults to access.authCollection, not a literal "users"', async () => { const plugin = totp() await ipalKit({ access: { authCollection: 'admins' }, i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })( empty(), ) expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' })) }) test('defaults to admin.user when access is not configured', async () => { const plugin = totp() await ipalKit({ i18n, twoFactor: { issuer: 'A', plugin: plugin as never } })( empty({ admin: { user: 'staff' } } as Partial), ) expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'staff' })) }) test('an explicit collectionSlug still wins', async () => { const plugin = totp() await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: { collectionSlug: 'admins', issuer: 'A', plugin: plugin as never }, })(empty()) expect(plugin).toHaveBeenCalledWith(expect.objectContaining({ collection: 'admins' })) }) }) describe('globals: public read, editor+ write', () => { test.each(['site-settings', 'cookie-settings', 'notifications'])('%s', async (slug) => { const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty()) const access = (config.globals?.find((g) => g.slug === slug) as GlobalConfig).access ?? {} expect(run(access.read, null)).toBe(true) expect(run(access.update, user)).toBe(false) expect(run(access.update, null)).toBe(false) expect(run(access.update, editor)).toBe(true) expect(run(access.update, admin)).toBe(true) }) }) describe('forms collection: public read, editor+ write, Emails hidden from plain users', () => { const build = async () => { const config = await ipalKit({ access: { authCollection: 'users' }, forms: {}, i18n, twoFactor: false })(empty()) return config.collections?.find((c) => c.slug === 'forms') as CollectionConfig } test('create / update / delete need editor', async () => { const { access = {} } = await build() expect(run(access.read, null)).toBe(true) for (const op of ['create', 'update', 'delete'] as const) { expect(run(access[op], user)).toBe(false) expect(run(access[op], editor)).toBe(true) } }) test('the Emails field (recipients, BCC) is unreadable for a plain user', async () => { const forms = await build() const emails = forms.fields.find((f: Field) => 'name' in f && f.name === 'emails') as { access?: { read?: (a: unknown) => boolean } } expect(emails.access?.read?.({ req: { user } })).toBe(false) expect(emails.access?.read?.({ req: { user: editor } })).toBe(true) }) }) describe('roles field: the first-register account becomes admin', () => { const hook = async (totalDocs: number, value: string[], pathname = '/api/users/first-register') => { const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(empty()) const users = config.collections?.find((c) => c.slug === 'users') as CollectionConfig const roles = users.fields.find((f) => 'name' in f && f.name === 'roles') as { hooks?: { beforeChange?: ((a: unknown) => unknown)[] } } const count = vi.fn(async () => ({ totalDocs })) return roles.hooks?.beforeChange?.[0]?.({ collection: { slug: 'users' }, operation: 'create', req: { pathname, payload: { count } }, value, }) } test('empty collection → admin, whatever was sent', async () => { expect(await hook(0, ['user'])).toEqual(['admin']) }) test('later accounts keep what access allowed', async () => { expect(await hook(1, ['user'])).toEqual(['user']) }) test('an open sign-up on an empty collection is not promoted', async () => { expect(await hook(0, ['user'], '/api/users')).toEqual(['user']) }) })