Files
ipal-kit/test/totpRedirectProvider.test.ts
T
Radosław SmolińskiandClaude Fable 5.2 9d2ab43438 2FA setup required: the window of existing accounts starts once, at the first start after the upgrade
Owner's decision: for accounts that exist before the upgrade the 72 hours
count from the first start after it. The code already stamped them then; this
adds the tests that the moment is written once per account (a restart, two
instances starting at once, edits and logins do not move it; 71 h after it setup
is open, 73 h after it the account is blocked) and says so in the docs.

After the rebase on 1.16.5: tests that the setup-required notice and the stale
import-map notice both stand above the screen and never replace the code form,
and that requireSetup, safeBack and guard switch independently.

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

118 lines
6.1 KiB
TypeScript

import React from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { beforeEach, describe, expect, test, vi } from 'vitest'
/**
* The stale-import-map notice of TotpRedirectProvider. The flag behind it is a guess (a text search of the
* importMap.js on disk, not the map in the bundle), so the notice is shown NEXT TO the screen: a wrong guess
* must never take the code form away.
*/
const state = { language: 'en', pathname: '/admin/verify-totp', user: { _strategy: 'local-jwt', hasTotp: true } as null | Record<string, unknown> }
vi.mock('@payloadcms/ui', () => ({
useAuth: () => ({ user: state.user }),
useConfig: () => ({ config: { admin: { routes: {} }, routes: { admin: '/admin' } } }),
useTranslation: () => ({ i18n: { language: state.language }, t: (key: string, vars?: Record<string, unknown>) => `${key}${vars ? JSON.stringify(vars) : ''}` }),
}))
vi.mock('next/navigation.js', () => ({ usePathname: () => state.pathname, useRouter: () => ({ replace: () => undefined }) }))
const { TotpRedirectProvider } = await import('../src/modules/security/totp/TotpRedirectProvider.js')
const FORM = '<form id="code-form"><input/></form>'
const form = () => React.createElement('form', { id: 'code-form' }, React.createElement('input'))
const render = (props: Record<string, unknown> = {}, children: React.ReactNode = form()) =>
renderToStaticMarkup(React.createElement(TotpRedirectProvider, props, children))
beforeEach(() => {
Object.assign(state, { language: 'en', pathname: '/admin/verify-totp', user: { _strategy: 'local-jwt', hasTotp: true } })
})
describe('TotpRedirectProvider with staleImportMap', () => {
// Reviewer repro 1: the build is current, but the importMap.js on disk is an older one → the flag is wrong.
// Reviewer repro 2: the map file re-exports the generated map → the text search finds nothing → the flag is wrong.
// In both the screen works, and must stay on the page.
test('a false alarm: the working code form is still rendered, with the notice above it', () => {
const html = render({ staleImportMap: true })
expect(html).toContain(FORM)
expect(html).toContain('role="alert"')
expect(html).toContain('may not be available')
expect(html.indexOf('role="alert"')).toBeLessThan(html.indexOf('code-form'))
})
test('a really stale map: Payload renders nothing for the screen, the notice is what the user sees', () => {
const html = render({ staleImportMap: true }, null)
expect(html).toContain('role="alert"')
expect(html).toContain('pnpm payload generate:importmap')
})
test('the setup screen too; Polish for a Polish panel', () => {
Object.assign(state, { language: 'pl', pathname: '/admin/setup-totp', user: { _strategy: 'local-jwt', hasTotp: false } })
const html = render({ staleImportMap: true })
expect(html).toContain('Weryfikacja dwuetapowa może być niedostępna')
expect(html).toContain(FORM)
})
test('no notice without the flag, on other pages, or for a visitor', () => {
expect(render({})).toBe(FORM)
Object.assign(state, { pathname: '/admin/collections/pages', user: { _strategy: 'totp', hasTotp: true } })
expect(render({ staleImportMap: true })).toBe(FORM)
Object.assign(state, { pathname: '/admin/verify-totp', user: null })
expect(render({ staleImportMap: true })).toBe(FORM)
})
test('a pending redirect still renders nothing', () => {
Object.assign(state, { pathname: '/admin/collections/pages', user: { _strategy: 'local-jwt', hasTotp: true } })
expect(render({ staleImportMap: true })).toBe('')
})
})
// With "2FA setup required" the setup screen carries its own notice (until when, or that the time has passed).
// Neither notice, alone or together, may take the code form off the page.
describe('TotpRedirectProvider: the setup-required notice and the stale-map notice together', () => {
const HOUR = 3_600_000
const onSetup = (totpSetupFrom: string) => Object.assign(state, { language: 'en', pathname: '/admin/setup-totp', user: { _strategy: 'local-jwt', hasTotp: false, totpSetupFrom } })
test('inside the window: the deadline notice, and the form', () => {
onSetup(new Date(Date.now() - HOUR).toISOString())
const html = render({ requireSetup: true, setupWindowHours: 72 })
expect(html).toContain('id="ipal-two-factor-notice"')
expect(html).toContain('role="status"')
expect(html).toContain('twoFactorSetupDeadline')
expect(html).toContain(FORM)
})
test('after the window: the "time has passed" notice as an alert, and the screen still rendered', () => {
onSetup(new Date(Date.now() - 73 * HOUR).toISOString())
const html = render({ requireSetup: true, setupWindowHours: 72 })
expect(html).toContain('twoFactorSetupClosed')
expect(html).toContain('role="alert"')
expect(html).toContain(FORM)
})
test('both notices at once, inside and after the window: both shown, the form below them', () => {
for (const hours of [1, 73]) {
onSetup(new Date(Date.now() - hours * HOUR).toISOString())
const html = render({ requireSetup: true, setupWindowHours: 72, staleImportMap: true })
expect(html).toContain('id="ipal-two-factor-notice"')
expect(html).toContain('may not be available')
expect(html).toContain(FORM)
expect(html.indexOf('ipal-two-factor-notice')).toBeLessThan(html.indexOf('code-form'))
expect(html.indexOf('may not be available')).toBeLessThan(html.indexOf('code-form'))
}
})
test('no window (windowHours: false): the plain "setup required" notice', () => {
onSetup(new Date(Date.now() - 500 * HOUR).toISOString())
const html = render({ requireSetup: true, setupWindowHours: 0 })
expect(html).toContain('twoFactorSetupRequired')
expect(html).toContain(FORM)
})
test('requireSetup off: no setup notice; the verify screen never shows it', () => {
onSetup(new Date(Date.now() - 73 * HOUR).toISOString())
expect(render({ requireSetup: false, setupWindowHours: 72 })).toBe(FORM)
Object.assign(state, { pathname: '/admin/verify-totp', user: { _strategy: 'local-jwt', hasTotp: true } })
expect(render({ requireSetup: true, setupWindowHours: 72 })).toBe(FORM)
})
})