|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { useState } from 'react'; |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import type { UserProfileMfaSetupViewProps } from '../user-profile-mfa-setup.view'; |
| 7 | +import { MfaSetupDialog } from './mfa-test-utils'; |
| 8 | + |
| 9 | +type ViewProps = UserProfileMfaSetupViewProps['authenticator']; |
| 10 | + |
| 11 | +const setup = { |
| 12 | + secret: 'JBSWY3DPEHPK3PXP', |
| 13 | + uri: 'otpauth://totp/Swingset:demo@example.com?secret=JBSWY3DPEHPK3PXP&issuer=Swingset', |
| 14 | +}; |
| 15 | + |
| 16 | +function renderView(overrides: Partial<ViewProps> = {}, step: UserProfileMfaSetupViewProps['step'] = 'authenticator') { |
| 17 | + const props: ViewProps = { |
| 18 | + setup, |
| 19 | + onRetry: vi.fn(), |
| 20 | + code: '', |
| 21 | + onCodeChange: vi.fn(), |
| 22 | + onSubmit: vi.fn(), |
| 23 | + ...overrides, |
| 24 | + }; |
| 25 | + return { |
| 26 | + props, |
| 27 | + ...render( |
| 28 | + <MfaSetupDialog |
| 29 | + step={step} |
| 30 | + authenticator={props} |
| 31 | + />, |
| 32 | + ), |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function VerificationExample({ onSubmit }: Pick<ViewProps, 'onSubmit'>) { |
| 37 | + const [code, setCode] = useState(''); |
| 38 | + return ( |
| 39 | + <MfaSetupDialog |
| 40 | + step='authenticator' |
| 41 | + authenticator={{ setup, onRetry: () => undefined, code, onCodeChange: setCode, onSubmit }} |
| 42 | + /> |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +describe('UserProfileAddAuthenticatorView', () => { |
| 47 | + it('shows preparation, offers retry on failure, and waits for setup data before verification', async () => { |
| 48 | + const user = userEvent.setup(); |
| 49 | + const { props, rerender } = renderView({ setup: undefined }, 'select'); |
| 50 | + rerender( |
| 51 | + <MfaSetupDialog |
| 52 | + step='authenticator' |
| 53 | + authenticator={props} |
| 54 | + />, |
| 55 | + ); |
| 56 | + const dialog = screen.getByRole('dialog', { name: 'Add an authenticator app' }); |
| 57 | + expect(screen.getByRole('status', { name: 'Preparing authenticator…' })).toBeVisible(); |
| 58 | + expect(screen.queryByRole('img')).not.toBeInTheDocument(); |
| 59 | + expect(screen.queryByRole('textbox')).not.toBeInTheDocument(); |
| 60 | + expect(screen.queryByRole('button', { name: 'Verify', exact: true })).not.toBeInTheDocument(); |
| 61 | + expect(screen.getByRole('button', { name: 'Back' })).toBeEnabled(); |
| 62 | + expect(screen.getByRole('button', { name: /Preparing authenticator/ })).toHaveFocus(); |
| 63 | + |
| 64 | + rerender( |
| 65 | + <MfaSetupDialog |
| 66 | + step='authenticator' |
| 67 | + authenticator={{ ...props, setupErrorMessage: 'Unable to prepare your authenticator.' }} |
| 68 | + />, |
| 69 | + ); |
| 70 | + expect(screen.getByRole('alert')).toHaveTextContent('Unable to prepare your authenticator.'); |
| 71 | + expect(screen.queryByRole('status', { name: 'Preparing authenticator…' })).not.toBeInTheDocument(); |
| 72 | + expect(screen.getByRole('button', { name: 'Try again' })).toHaveFocus(); |
| 73 | + await user.click(screen.getByRole('button', { name: 'Try again' })); |
| 74 | + expect(props.onRetry).toHaveBeenCalledOnce(); |
| 75 | + expect(props.onSubmit).not.toHaveBeenCalled(); |
| 76 | + |
| 77 | + rerender( |
| 78 | + <MfaSetupDialog |
| 79 | + step='authenticator' |
| 80 | + authenticator={props} |
| 81 | + />, |
| 82 | + ); |
| 83 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 84 | + expect(screen.getByRole('status', { name: 'Preparing authenticator…' })).toBeVisible(); |
| 85 | + expect(screen.getByRole('button', { name: /Preparing authenticator/ })).toHaveFocus(); |
| 86 | + |
| 87 | + rerender( |
| 88 | + <MfaSetupDialog |
| 89 | + step='authenticator' |
| 90 | + authenticator={{ ...props, setup }} |
| 91 | + />, |
| 92 | + ); |
| 93 | + expect(screen.getByRole('dialog')).toBe(dialog); |
| 94 | + expect(screen.queryByRole('status', { name: 'Preparing authenticator…' })).not.toBeInTheDocument(); |
| 95 | + expect(screen.getByRole('img', { name: 'Authenticator setup QR code' })).toBeVisible(); |
| 96 | + expect(screen.getByRole('textbox', { name: 'Verification code' })).toHaveFocus(); |
| 97 | + expect(screen.getByRole('button', { name: 'Verify', exact: true })).toHaveAttribute('aria-disabled', 'true'); |
| 98 | + await user.keyboard('{Enter}'); |
| 99 | + expect(props.onSubmit).not.toHaveBeenCalled(); |
| 100 | + rerender( |
| 101 | + <MfaSetupDialog |
| 102 | + step='authenticator' |
| 103 | + authenticator={{ ...props, setup, code: '123456' }} |
| 104 | + />, |
| 105 | + ); |
| 106 | + expect(props.onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); |
| 107 | + }); |
| 108 | + |
| 109 | + it('blocks incomplete and pending submissions, including native form submission', async () => { |
| 110 | + const user = userEvent.setup(); |
| 111 | + const { props, rerender } = renderView({ code: '123' }); |
| 112 | + const verify = screen.getByRole('button', { name: 'Verify', exact: true }); |
| 113 | + expect(verify).toHaveAttribute('aria-disabled', 'true'); |
| 114 | + await user.click(screen.getByRole('textbox', { name: 'Verification code' })); |
| 115 | + await user.keyboard('{Enter}'); |
| 116 | + expect(props.onSubmit).not.toHaveBeenCalled(); |
| 117 | + |
| 118 | + rerender( |
| 119 | + <MfaSetupDialog |
| 120 | + step='authenticator' |
| 121 | + authenticator={{ ...props, code: '123456', isPending: true }} |
| 122 | + />, |
| 123 | + ); |
| 124 | + expect(verify).toHaveAttribute('aria-busy', 'true'); |
| 125 | + expect(screen.getByRole('progressbar', { name: 'Verifying code' })).toBeInTheDocument(); |
| 126 | + for (const slot of screen.getAllByRole('textbox')) { |
| 127 | + expect(slot).toBeDisabled(); |
| 128 | + } |
| 129 | + expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled(); |
| 130 | + await user.click(verify); |
| 131 | + const form = screen.getByRole('textbox', { name: 'Verification code' }).closest('form'); |
| 132 | + if (!form) { |
| 133 | + throw new Error('Verification form missing'); |
| 134 | + } |
| 135 | + form.requestSubmit(); |
| 136 | + expect(props.onSubmit).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('keeps the entered code when switching between QR and manual setup', async () => { |
| 140 | + const user = userEvent.setup(); |
| 141 | + const onSubmit = vi.fn(); |
| 142 | + render(<VerificationExample onSubmit={onSubmit} />); |
| 143 | + await user.click(screen.getByRole('textbox', { name: 'Verification code' })); |
| 144 | + await user.keyboard('123'); |
| 145 | + await user.click(screen.getByRole('button', { name: 'Can’t scan? View setup key' })); |
| 146 | + await user.click(screen.getByRole('button', { name: 'Scan QR code instead' })); |
| 147 | + await user.click(screen.getByRole('textbox', { name: 'Character 4 of 6' })); |
| 148 | + await user.keyboard('456'); |
| 149 | + expect(onSubmit).toHaveBeenCalledExactlyOnceWith('123456'); |
| 150 | + }); |
| 151 | +}); |
0 commit comments