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 } 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) => `${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 = '
' const form = () => React.createElement('form', { id: 'code-form' }, React.createElement('input')) const render = (props: Record = {}, 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) }) })