R2-1: the notice was rendered instead of the 2FA screen, and its flag is a text search of the importMap.js on disk — not the map in the build. A false alarm (map generated in the build with the repository's file in the running image, a re-exporting map) hid a working code form and locked everyone out. The notice is now rendered above the screen, the log line says "may be", and the flag goes to ipal-kit's own provider only. R2-2: views are compared against copies, so a factory that reuses its view objects is guarded too; copyViews, changedViewKeys, SAFE_TOTP_VIEW, totpViewImportKey, findImportMapFile and missingImportMapEntries are exported; ipal-doctor warns when the config registers the 2FA package's own view; docs wording and a misplaced comment fixed. Co-Authored-By: Claude Fable 5.2 <[email protected]>
68 lines
3.4 KiB
TypeScript
68 lines
3.4 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 } }),
|
|
}))
|
|
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('')
|
|
})
|
|
})
|