Files
ipal-kit/test/totpTextsAndCookieButton.test.ts
T
Radosław SmolińskiandClaude Fable 5.2 0c61ac231b Security: staff checks only for staff collections; 2FA covers what the plugin adds
Staff check (userMeetsRole): the 'no roles field = every account passes'
rule applied to the account's own collection, so an account of a second
auth collection (customers) passed every staff check. It now applies only
to staff collections: the panel's login collection plus the new
access.staffCollections. The injected auth collection uses the new
adminOrSelfIn(slug), so an account of another collection with the same
numeric id is not 'self'.

2FA: @clocklimited/payload-2fa wraps the collections and globals present
when it runs; ipalKit's own globals and collections were added later and
stayed unwrapped, so a password without the TOTP code opened them over
REST and GraphQL. ipal-kit now has its own second-factor check
(secondFactorPassed / requireSecondFactor): applied to every collection
and global at the end of ipalKit, again in onInit for entities added by
plugins listed after it, and inside userMeetsRole and the endpoints that
take a project access function. It fails closed: an unrecognised user
shape is refused, and startup stops when the injected 2FA plugin lacks
the parts the check reads (twoFactor.guard: false opts out).

Tests through real HTTP requests (REST and GraphQL) against Payload's
route handlers, plus the reference smoke test. Docs, agent guide, skills
and the reference skills copy updated; changelog entry without a version.

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

82 lines
3.7 KiB
TypeScript

import type { Config } from 'payload'
import { describe, expect, test } from 'vitest'
import { bottomOverlap } from '../src/modules/consent/CookieButton.js'
import { TOTP_TEXTS } from '../src/modules/security/totp/totpTexts.js'
import { ipalKit } from '../src/plugin.js'
import { withTotpParts } from './helpers.js'
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] }
/** Stand-in for @clocklimited/payload-2fa: its own texts and the two views, as the package adds them. */
const fakeTotp = (o: { collection: string }) => (config: Config) => ({
...withTotpParts(config, o.collection),
admin: {
...config.admin,
components: {
...config.admin?.components,
views: {
...config.admin?.components?.views,
SetupTOTP: { Component: { path: 'pkg#TOTPSetup' }, path: 'setup-totp' },
VerifyTOTP: { Component: { path: 'pkg#TOTPVerify' }, path: 'verify-totp' },
},
},
},
i18n: {
...config.i18n,
translations: {
...config.i18n?.translations,
en: { ...config.i18n?.translations?.en, totpPlugin: { verify: { title: 'Verification' } } },
pl: { ...config.i18n?.translations?.pl, totpPlugin: { verify: { title: 'Weryfikacja' } } },
},
},
})
const base = (translations?: Record<string, unknown>): Config =>
({ collections: [{ slug: 'users', auth: true, fields: [] }], i18n: translations ? { translations } : undefined, secret: 'test' }) as unknown as Config
type Translations = Record<string, { totpPlugin: { setup: Record<string, string>; verify: { title: string } } }>
describe('2FA texts', () => {
test('ipal-kit texts go over the package, the project over both; screens get a tab title', async () => {
const config = await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp } } as never)(
base({ en: { totpPlugin: { verify: { title: 'Project check' } } } }),
)
const t = config.i18n!.translations as Translations
expect(t.pl.totpPlugin.verify.title).toBe('Weryfikacja dwuetapowa')
expect(t.pl.totpPlugin.setup.enterCode).toBe('Wpisz {digits}-cyfrowy kod z aplikacji:')
expect(t.en.totpPlugin.verify.title).toBe('Project check')
const views = config.admin!.components!.views as Record<string, { meta?: { title?: string } }>
expect(views.VerifyTOTP.meta?.title).toBe('Weryfikacja dwuetapowa')
expect(views.SetupTOTP.meta?.title).toBe('Weryfikacja dwuetapowa')
})
test("texts: false keeps the package's", async () => {
const config = await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp, texts: false } } as never)(base())
expect((config.i18n!.translations as Translations).pl.totpPlugin.verify.title).toBe('Weryfikacja')
})
test('same keys in English and Polish, short and formal', () => {
const keys = (o: object, p = ''): string[] =>
Object.entries(o).flatMap(([k, v]) => (typeof v === 'object' ? keys(v as object, `${p}${k}.`) : [`${p}${k}`]))
expect(keys(TOTP_TEXTS.pl)).toEqual(keys(TOTP_TEXTS.en))
const all = [...Object.values(TOTP_TEXTS.pl.totpPlugin.setup), TOTP_TEXTS.pl.totpPlugin.fieldDescription]
expect(all.some((s) => /\b(?:Tw[oó]j|Będziesz|Masz)\b/.test(s))).toBe(false)
expect(TOTP_TEXTS.en.totpPlugin.forceTotp.label.split(' ').length).toBeLessThanOrEqual(3)
})
})
describe('CookieButton lift above the footer', () => {
test('moves up by the visible part of the footer', () => {
expect(bottomOverlap([], 800)).toBe(0)
expect(bottomOverlap([1200], 800)).toBe(0) // footer below the fold
expect(bottomOverlap([650], 800)).toBe(150)
expect(bottomOverlap([700, 650], 800)).toBe(150) // the highest one counts
})
test('never pushes the button off the screen', () => {
expect(bottomOverlap([-500], 800)).toBe(704)
})
})