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]>
This commit is contained in:
Radosław Smoliński
2026-11-24 18:56:07 +01:00
co-authored by Claude Fable 5.2
parent 5d44ac3135
commit 9d2ab43438
7 changed files with 191 additions and 6 deletions
+51 -1
View File
@@ -12,7 +12,7 @@ const state = { language: 'en', pathname: '/admin/verify-totp', user: { _strateg
vi.mock('@payloadcms/ui', () => ({
useAuth: () => ({ user: state.user }),
useConfig: () => ({ config: { admin: { routes: {} }, routes: { admin: '/admin' } } }),
useTranslation: () => ({ i18n: { language: state.language } }),
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 }) }))
@@ -65,3 +65,53 @@ describe('TotpRedirectProvider with staleImportMap', () => {
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)
})
})