Skip to content

Commit c8e1b2b

Browse files
fix(mosaic): keep MFA setup on the method list until it is ready (#10432)
1 parent 0a9a1e5 commit c8e1b2b

15 files changed

Lines changed: 444 additions & 273 deletions

‎.changeset/mfa-loading-fixes.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/src/features/user-profile/user-profile-mfa-section/__tests__/user-profile-mfa-interactions.feature.test.tsx‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ describe('User profile MFA interaction boundaries', () => {
4545
await user.click(screen.getByRole('button', { name: 'Add verification method' }));
4646
await user.click(screen.getByRole('button', { name: /Authenticator app/ }));
4747
await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Please try again.'));
48-
const retry = screen.getByRole('button', { name: 'Try again' });
49-
await waitFor(() => expect(retry).toHaveFocus());
48+
const retry = screen.getByRole('button', { name: /Authenticator app/ });
49+
expect(retry).toBeEnabled();
5050
await user.click(retry);
5151
expect(await screen.findByRole('img', { name: /QR code/i })).toBeVisible();
5252
const code = screen.getByRole('textbox', { name: 'Verification code' });
@@ -74,7 +74,7 @@ describe('User profile MFA interaction boundaries', () => {
7474
expect(fapi.mfa.totpCreations).toBe(1);
7575
});
7676

77-
it('disables Back while authenticator preparation is pending', async () => {
77+
it('disables method choices and prevents duplicate authenticator preparation', async () => {
7878
await renderMfa();
7979
const held = holdRequests('post', '/v1/me/totp');
8080
const user = userEvent.setup();
@@ -83,9 +83,12 @@ describe('User profile MFA interaction boundaries', () => {
8383
await waitFor(() => expect(held.requests).toHaveLength(1));
8484
try {
8585
await waitFor(() => {
86-
expect(screen.getByRole('status', { name: 'Preparing authenticator…' })).toBeVisible();
87-
expect(screen.getByRole('button', { name: 'Back' })).toBeDisabled();
86+
expect(screen.getByRole('button', { name: /Authenticator app/ })).toHaveAttribute('aria-busy', 'true');
87+
expect(screen.getByRole('button', { name: /Authenticator app/ })).toBeDisabled();
88+
expect(screen.getByRole('button', { name: /SMS verification/ })).toBeDisabled();
8889
});
90+
await user.click(screen.getByRole('button', { name: /Authenticator app/ }));
91+
expect(held.requests).toHaveLength(1);
8992
} finally {
9093
held.release();
9194
}
@@ -202,9 +205,39 @@ describe('User profile MFA interaction boundaries', () => {
202205
await waitFor(() => expect(screen.getByRole('alert')).toHaveTextContent('Please try again.'));
203206
expect(screen.queryByRole('button', { name: 'Copy and close' })).toBeNull();
204207
expect(screen.queryByRole('button', { name: 'Download' })).toBeNull();
205-
const retry = screen.getByRole('button', { name: 'Try again' });
206-
await waitFor(() => expect(retry).toHaveFocus());
208+
const retry = screen.getByRole('button', { name: /Backup codes/ });
209+
expect(retry).toBeEnabled();
210+
const held = holdRequests('post', '/v1/me/backup_codes/');
207211
await user.click(retry);
212+
await waitFor(() => expect(held.requests).toHaveLength(1));
213+
try {
214+
expect(retry).toBeDisabled();
215+
expect(retry).toHaveAttribute('aria-busy', 'true');
216+
expect(screen.queryByRole('list', { name: 'Backup codes' })).toBeNull();
217+
await user.click(retry);
218+
expect(held.requests).toHaveLength(1);
219+
} finally {
220+
held.release();
221+
}
222+
expect(await screen.findByText('CODE0100')).toBeVisible();
223+
});
224+
225+
it('keeps the selected backup choice while the account refresh completes', async () => {
226+
await renderMfa(fapiUser({ id: 'user_1', totp_enabled: true, two_factor_enabled: true }));
227+
const user = userEvent.setup();
228+
await user.click(screen.getByRole('button', { name: 'Add verification method' }));
229+
const held = holdRequests('get', '/v1/me');
230+
await user.click(screen.getByRole('button', { name: /Backup codes/ }));
231+
await waitFor(() => expect(held.requests).toHaveLength(1));
232+
try {
233+
expect(within(screen.getByRole('dialog')).getByRole('button', { name: /Backup codes/ })).toHaveAttribute(
234+
'aria-busy',
235+
'true',
236+
);
237+
expect(screen.queryByRole('list', { name: 'Backup codes' })).toBeNull();
238+
} finally {
239+
held.release();
240+
}
208241
expect(await screen.findByText('CODE0100')).toBeVisible();
209242
});
210243

‎packages/mosaic/src/features/user-profile/user-profile-mfa-section/__tests__/user-profile-mfa-management.feature.test.tsx‎

Lines changed: 131 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { screen, waitFor, within } from '@testing-library/react';
1+
import { act, screen, waitFor, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { http, HttpResponse } from 'msw';
44
import { describe, expect, it, vi } from 'vitest';
@@ -355,7 +355,7 @@ describe('User profile MFA management', () => {
355355
expect(clerk.user?.id).toBe('user_2');
356356
});
357357

358-
it('opens the backup-code surface while regeneration is pending', async () => {
358+
it('keeps the profile visible until regenerated backup codes are ready', async () => {
359359
await renderMfa(
360360
fapiUser({ id: 'user_1', totp_enabled: true, backup_code_enabled: true, two_factor_enabled: true }),
361361
);
@@ -364,22 +364,46 @@ describe('User profile MFA management', () => {
364364
await openMenu(user, screen.getByRole('button', { name: 'Manage Backup codes' }));
365365
await user.click(screen.getByRole('menuitem', { name: 'Regenerate' }));
366366
await waitFor(() => expect(held.requests).toHaveLength(1));
367-
await waitFor(() => expect(screen.getByRole('status', { name: 'Generating backup codes' })).toBeVisible());
368-
for (const name of ['Print', 'Download']) {
369-
expect(screen.getByRole('button', { name })).toBeVisible();
370-
expect(screen.getByRole('button', { name })).toBeDisabled();
367+
try {
368+
expect(screen.queryByRole('dialog')).toBeNull();
369+
expect(screen.getByRole('button', { name: 'Add verification method' })).toBeDisabled();
370+
expect(screen.getByRole('button', { name: 'Manage Backup codes' })).toBeVisible();
371+
} finally {
372+
held.release();
371373
}
372-
expect(screen.getByRole('button', { name: 'Copy and close' })).toBeVisible();
373-
expect(screen.getByRole('button', { name: 'Copy and close' })).toHaveAttribute('aria-disabled', 'true');
374-
expect(screen.queryByRole('button', { name: 'Close' })).toBeNull();
375-
held.release();
376374
await waitFor(() => expect(screen.getByText('CODE0100')).toBeVisible());
377375
expect(screen.getByRole('button', { name: 'Close' })).toBeVisible();
378376
for (const name of ['Print', 'Download', 'Copy and close']) {
379377
expect(screen.getByRole('button', { name })).toBeEnabled();
380378
}
381379
});
382380

381+
it('returns failed regeneration to the section and retries from the menu', async () => {
382+
const fapi = await renderMfa(
383+
fapiUser({ id: 'user_1', totp_enabled: true, backup_code_enabled: true, two_factor_enabled: true }),
384+
);
385+
const handlers = worker.listHandlers();
386+
const held = holdRequests('post', '/v1/me/backup_codes/');
387+
const user = userEvent.setup();
388+
const trigger = screen.getByRole('button', { name: 'Manage Backup codes' });
389+
await openMenu(user, trigger);
390+
await user.click(screen.getByRole('menuitem', { name: 'Regenerate' }));
391+
await waitFor(() => expect(held.requests).toHaveLength(1));
392+
held.fail('action_blocked', 'Unable to create backup codes.');
393+
await waitFor(() =>
394+
expect(screen.getByRole('alert')).toHaveTextContent(
395+
"This action couldn't be completed. Please try again later or contact support if this persists.",
396+
),
397+
);
398+
expect(screen.queryByRole('dialog')).toBeNull();
399+
await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
400+
worker.resetHandlers(...handlers);
401+
await openMenu(user, trigger);
402+
await user.click(screen.getByRole('menuitem', { name: 'Regenerate' }));
403+
expect(await screen.findByText('CODE0100')).toBeVisible();
404+
expect(fapi.mfa.backupCodeCreations).toBe(1);
405+
});
406+
383407
it('uses fresh server codes for explicit regeneration', async () => {
384408
const fapi = await renderMfa(
385409
fapiUser({ id: 'user_1', totp_enabled: true, backup_code_enabled: true, two_factor_enabled: true }),
@@ -414,7 +438,21 @@ describe('User profile MFA management', () => {
414438

415439
await regenerate(user);
416440
expect(await screen.findByText('Enter your current password to continue')).toBeVisible();
417-
await user.type(screen.getByLabelText('Password'), 'hunter2{Enter}');
441+
await waitFor(() => expect(screen.getByLabelText('Password')).toHaveFocus());
442+
const refresh = holdRequests('get', '/v1/me');
443+
try {
444+
await user.keyboard('hunter2{Enter}');
445+
await act(async () => {
446+
await waitFor(() => expect(refresh.requests).toHaveLength(1));
447+
});
448+
await user.keyboard('{Escape}');
449+
expect(screen.getByRole('dialog')).toHaveAttribute('data-open');
450+
expect(screen.getByText('Enter your current password to continue')).toBeVisible();
451+
expect(screen.queryByRole('button', { name: 'Close' })).toBeNull();
452+
expect(screen.getByRole('button', { name: 'Continue' })).toHaveAttribute('aria-busy', 'true');
453+
} finally {
454+
refresh.release();
455+
}
418456

419457
const list = await screen.findByRole('list', { name: 'Backup codes' });
420458
expect(within(list).getAllByRole('listitem')).toHaveLength(10);
@@ -445,6 +483,7 @@ describe('User profile MFA management', () => {
445483
await regenerate(user);
446484
await screen.findByText('Enter your current password to continue');
447485
const dialog = screen.getByRole('dialog');
486+
const handlers = worker.listHandlers();
448487
const replay = holdRequests('post', '/v1/me/backup_codes/');
449488
await user.type(screen.getByLabelText('Password'), 'hunter2{Enter}');
450489
await waitFor(() => expect(replay.requests).toHaveLength(1));
@@ -454,47 +493,53 @@ describe('User profile MFA management', () => {
454493
expect(dialog).toBeVisible();
455494

456495
replay.fail('action_blocked', 'Unable to create backup codes.');
457-
await waitFor(() => expect(within(screen.getByRole('dialog')).getByRole('alert')).toBeVisible());
496+
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
497+
expect(screen.getByRole('alert')).toHaveTextContent(
498+
"This action couldn't be completed. Please try again later or contact support if this persists.",
499+
);
458500
expect(screen.queryByRole('list', { name: 'Backup codes' })).toBeNull();
459501
expect(fapi.mfa.backupCodeCreations).toBe(0);
502+
worker.resetHandlers(...handlers);
503+
await regenerate(user);
504+
await screen.findByRole('list', { name: 'Backup codes' });
505+
expect(fapi.mfa.backupCodeCreations).toBe(1);
460506
});
461507

462-
it('cancels a loading verification when the backup-code dialog closes so regeneration can start again', async () => {
508+
it('keeps the profile visible until verification preparation is ready', async () => {
463509
const fapi = await renderBackupCodesNeedingReverification();
464510
const start = holdRequests('post', '/v1/client/sessions/:id/verify');
465511
const user = userEvent.setup();
466512

467513
await regenerate(user);
468514
await waitFor(() => expect(start.requests).toHaveLength(1));
469-
await user.click(within(screen.getByRole('dialog')).getByRole('button', { name: 'Close' }));
470-
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
471-
start.release();
472-
473-
await regenerate(user);
515+
try {
516+
expect(screen.queryByRole('dialog')).toBeNull();
517+
expect(screen.getByRole('button', { name: 'Manage Backup codes' })).toBeVisible();
518+
} finally {
519+
start.release();
520+
}
521+
await screen.findByText('Enter your current password to continue');
522+
await waitFor(() => expect(screen.getByRole('dialog')).toContainElement(document.activeElement));
523+
await user.type(screen.getByLabelText('Password'), 'hunter2{Enter}');
474524
await screen.findByRole('list', { name: 'Backup codes' });
475525
expect(fapi.mfa.backupCodeCreations).toBe(1);
476526
});
477527

478-
it('keeps the backup-code dialog open until the first response so a verification can appear', async () => {
479-
const fapi = await renderBackupCodesNeedingReverification();
528+
it('keeps the profile visible until the first response can request verification', async () => {
529+
await renderBackupCodesNeedingReverification();
480530
const first = holdRequests('post', '/v1/me/backup_codes/');
481531
const user = userEvent.setup();
482532

483533
await regenerate(user);
484534
await waitFor(() => expect(first.requests).toHaveLength(1));
485-
const dialog = screen.getByRole('dialog');
486535
try {
487-
expect(within(dialog).queryByRole('button', { name: 'Close' })).toBeNull();
488-
await user.keyboard('{Escape}');
489-
expect(dialog).toBeVisible();
536+
expect(screen.queryByRole('dialog')).toBeNull();
537+
expect(screen.getByRole('button', { name: 'Manage Backup codes' })).toBeVisible();
490538
} finally {
491539
first.release();
492540
}
493541

494-
expect(await within(dialog).findByText('Enter your current password to continue')).toBeVisible();
495-
await user.type(screen.getByLabelText('Password'), 'hunter2{Enter}');
496-
await screen.findByRole('list', { name: 'Backup codes' });
497-
expect(fapi.mfa.backupCodeCreations).toBe(1);
542+
expect(await screen.findByText('Enter your current password to continue')).toBeVisible();
498543
});
499544

500545
const startSms = async (user: ReturnType<typeof userEvent.setup>) => {
@@ -517,6 +562,34 @@ describe('User profile MFA management', () => {
517562
expect(fapi.mfa.phoneUpdates).toContainEqual({ id: 'phone_1', reserved: true, default: undefined });
518563
});
519564

565+
it('keeps session verification on screen until the SMS reservation refresh completes', async () => {
566+
await renderMfa(undefined, mfaEnvironment(), { verification: { secrets: { password: 'hunter2' } } });
567+
demandReverificationOnce('/v1/me/phone_numbers/phone_1');
568+
const user = userEvent.setup();
569+
570+
await startSms(user);
571+
await user.click(screen.getByRole('button', { name: 'Continue' }));
572+
expect(await screen.findByText('Enter your current password to continue')).toBeVisible();
573+
await waitFor(() => expect(screen.getByLabelText('Password')).toHaveFocus());
574+
const refresh = holdRequests('get', '/v1/me');
575+
try {
576+
await user.keyboard('hunter2{Enter}');
577+
await act(async () => {
578+
await waitFor(() => expect(refresh.requests).toHaveLength(1));
579+
});
580+
await user.keyboard('{Escape}');
581+
expect(screen.getByRole('dialog')).toHaveAttribute('data-open');
582+
expect(screen.getByText('Enter your current password to continue')).toBeVisible();
583+
expect(screen.queryByRole('button', { name: 'Close' })).toBeNull();
584+
expect(screen.getByRole('button', { name: 'Continue' })).toHaveAttribute('aria-busy', 'true');
585+
expect(screen.queryByText('CODE0001')).toBeNull();
586+
} finally {
587+
refresh.release();
588+
}
589+
590+
expect(await screen.findByText('CODE0001')).toBeVisible();
591+
});
592+
520593
it('keeps SMS code verification open until its reservation challenge can be cancelled', async () => {
521594
const unverified = fapiPhoneNumber({ ...phone, verification: { ...phone.verification, status: 'unverified' } });
522595
const fapi = await renderMfa(fapiUser({ id: 'user_1', phone_numbers: [unverified] }), mfaEnvironment(), {
@@ -567,6 +640,36 @@ describe('User profile MFA management', () => {
567640
expect(await screen.findByRole('button', { name: 'Verify' })).toBeVisible();
568641
});
569642

643+
it('keeps session verification on screen until the new phone code is sent', async () => {
644+
const fapi = await renderMfa(fapiUser({ id: 'user_1' }), mfaEnvironment(), {
645+
verification: { secrets: { password: 'hunter2' } },
646+
});
647+
demandReverificationOnce('/v1/me/phone_numbers');
648+
const user = userEvent.setup();
649+
650+
await startSms(user);
651+
await user.type(screen.getByRole('textbox', { name: 'Phone' }), '5555550303');
652+
await user.click(screen.getByRole('button', { name: 'Send code' }));
653+
expect(await screen.findByText('Enter your current password to continue')).toBeVisible();
654+
await waitFor(() => expect(screen.getByLabelText('Password')).toHaveFocus());
655+
const prepare = holdRequests('post', '/v1/me/phone_numbers/:id/prepare_verification');
656+
try {
657+
await user.keyboard('hunter2{Enter}');
658+
await act(async () => {
659+
await waitFor(() => expect(prepare.requests).toHaveLength(1));
660+
});
661+
expect(screen.getByText('Enter your current password to continue')).toBeVisible();
662+
expect(screen.getByRole('button', { name: 'Continue' })).toHaveAttribute('aria-busy', 'true');
663+
expect(screen.queryByRole('textbox', { name: 'Phone' })).toBeNull();
664+
} finally {
665+
prepare.release();
666+
}
667+
668+
expect(await screen.findByRole('textbox', { name: 'Verification code' })).toBeVisible();
669+
expect(screen.queryByRole('textbox', { name: 'Phone' })).toBeNull();
670+
expect(fapi.mfa.phonePreparations).toHaveLength(1);
671+
});
672+
570673
it('lets the user cancel SMS MFA verification quietly and add the method again', async () => {
571674
const fapi = await renderMfa(undefined, mfaEnvironment(), { verification: { secrets: { password: 'hunter2' } } });
572675
demandReverificationOnce('/v1/me/phone_numbers/phone_1');

‎packages/mosaic/src/features/user-profile/user-profile-mfa-section/__tests__/user-profile-mfa.feature.test.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,16 @@ describe('User profile MFA', () => {
221221
const user = userEvent.setup();
222222
await user.click(screen.getByRole('button', { name: 'Add verification method' }));
223223
await user.click(screen.getByRole('button', { name: /SMS verification/ }));
224+
const held = holdRequests('post', '/v1/me/phone_numbers/phone_1/prepare_verification');
224225
await user.click(screen.getByRole('button', { name: 'Continue' }));
226+
await waitFor(() => expect(held.requests).toHaveLength(1));
227+
try {
228+
expect(screen.getByRole('combobox', { name: /Phone number/ })).toBeVisible();
229+
expect(screen.getByRole('button', { name: 'Continue' })).toHaveAttribute('aria-busy', 'true');
230+
expect(screen.queryByRole('textbox', { name: 'Verification code' })).toBeNull();
231+
} finally {
232+
held.release();
233+
}
225234
expect(await screen.findByRole('textbox', { name: 'Verification code' })).toBeVisible();
226235
expect(fapi.mfa.phonePreparations).toEqual(['phone_1']);
227236
expect(fapi.mfa.phoneUpdates).toEqual([]);

‎packages/mosaic/src/features/user-profile/user-profile-mfa-section/user-profile-add-authenticator.messages.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ export const userProfileAddAuthenticatorMessages = {
33
back: 'Back',
44
verify: 'Verify',
55
pending: 'Verifying code',
6-
preparing: 'Preparing authenticator…',
7-
retry: 'Try again',
86
} as const;

0 commit comments

Comments
 (0)