-
Notifications
You must be signed in to change notification settings - Fork 238
fix(settings): serve the account recovery key as a plain text file #21003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vpomerleau
wants to merge
1
commit into
main
Choose a base branch
from
FXA-14334-switch-to-text-file-recovery-key
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/fxa-settings/src/components/ButtonDownloadRecoveryKey/en.ftl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| ## ButtonDownloadRecoveryKey | ||
| ## Clicking on this button downloads a plain text file that contains the user's account recovery key | ||
| ## The account recovery key can be used to recover data when users forget their account password | ||
|
|
||
| # Button to download the account recovery key as a plain text file and navigate to the next step | ||
| # The next (and final) step is an optional prompt to save a storage hint | ||
| recovery-key-download-button-v3 = Download and continue | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this still need a .title attribute? If so it seems it was removed. If not, then we'll need to update the string ID since adding/removing an attribute requires a new ID. |
||
|
|
||
| # Error message shown in a banner if the account recovery key download failed. | ||
| # The id keeps "pdf" from when this was a PDF, to preserve existing translations. | ||
| recovery-key-pdf-download-error = Sorry, there was a problem downloading your account recovery key. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
packages/fxa-settings/src/components/ButtonDownloadRecoveryKey/index.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { renderWithLocalizationProvider } from 'fxa-react/lib/test-utils/localizationProvider'; | ||
| import { ButtonDownloadRecoveryKey, getFilename } from '.'; | ||
| import { downloadTextFile } from '../../lib/download'; | ||
| import * as Sentry from '@sentry/browser'; | ||
| import { MOCK_EMAIL } from '../../pages/mocks'; | ||
|
|
||
| jest.mock('../../lib/download', () => ({ | ||
| downloadTextFile: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('@sentry/browser', () => ({ | ||
| captureException: jest.fn(), | ||
| })); | ||
|
|
||
| const MOCK_RECOVERY_KEY = 'WXYZ WXYZ WXYZ WXYZ WXYZ WXYZ WXYZ WXYZ'; | ||
|
|
||
| const renderButton = (navigateForward?: () => void) => | ||
| renderWithLocalizationProvider( | ||
| <ButtonDownloadRecoveryKey | ||
| recoveryKeyValue={MOCK_RECOVERY_KEY} | ||
| email={MOCK_EMAIL} | ||
| {...{ navigateForward }} | ||
| /> | ||
| ); | ||
|
|
||
| const getDownloadButton = () => | ||
| screen.getByRole('button', { name: 'Download and continue' }); | ||
|
|
||
| const renderAndClickDownload = async (navigateForward?: () => void) => { | ||
| const user = userEvent.setup(); | ||
| renderButton(navigateForward); | ||
| await user.click(getDownloadButton()); | ||
| }; | ||
|
|
||
| const mockDownloadFailure = () => | ||
| jest.mocked(downloadTextFile).mockImplementationOnce(() => { | ||
| throw new Error('download failed'); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| // clearAllMocks leaves implementations queued; reset so an unconsumed | ||
| // failure cannot leak into the next test. | ||
| jest.mocked(downloadTextFile).mockReset(); | ||
| }); | ||
|
|
||
| describe('ButtonDownloadRecoveryKey', () => { | ||
| it('renders the download button', () => { | ||
| renderButton(); | ||
|
|
||
| expect(getDownloadButton()).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('tags the button for Glean click metrics', () => { | ||
| renderButton(); | ||
|
|
||
| expect(getDownloadButton()).toHaveAttribute( | ||
| 'data-glean-id', | ||
| 'account_pref_recovery_key_download' | ||
| ); | ||
| }); | ||
|
|
||
| it('downloads the recovery key as a text file when clicked', async () => { | ||
| await renderAndClickDownload(); | ||
|
|
||
| const today = new Date().toISOString().split('T')[0]; | ||
| expect(downloadTextFile).toHaveBeenCalledWith( | ||
| MOCK_RECOVERY_KEY, | ||
| `Mozilla-Recovery-Key_${today}_${MOCK_EMAIL}.txt` | ||
| ); | ||
| }); | ||
|
|
||
| it('shows an inline error banner when the download throws', async () => { | ||
| mockDownloadFailure(); | ||
|
|
||
| await renderAndClickDownload(); | ||
|
|
||
| expect(screen.getByRole('alert')).toHaveTextContent( | ||
| 'Sorry, there was a problem downloading your account recovery key.' | ||
| ); | ||
| }); | ||
|
|
||
| it('reports the failure to Sentry when the download throws', async () => { | ||
| mockDownloadFailure(); | ||
|
|
||
| await renderAndClickDownload(); | ||
|
|
||
| expect(Sentry.captureException).toHaveBeenCalledWith(expect.any(Error)); | ||
| }); | ||
|
|
||
| it('does not navigate forward when the download throws', async () => { | ||
| mockDownloadFailure(); | ||
| const navigateForward = jest.fn(); | ||
|
|
||
| await renderAndClickDownload(navigateForward); | ||
|
|
||
| expect(navigateForward).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('navigates forward when the download succeeds', async () => { | ||
| const navigateForward = jest.fn(); | ||
|
|
||
| await renderAndClickDownload(navigateForward); | ||
|
|
||
| expect(navigateForward).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('clears a previous error once a retry succeeds', async () => { | ||
| const user = userEvent.setup(); | ||
| mockDownloadFailure(); | ||
| renderButton(); | ||
|
|
||
| await user.click(getDownloadButton()); | ||
| expect(screen.getByRole('alert')).toBeInTheDocument(); | ||
|
|
||
| await user.click(getDownloadButton()); | ||
|
|
||
| expect(screen.queryByRole('alert')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('renders no error banner before any download is attempted', () => { | ||
| renderButton(); | ||
|
|
||
| expect(screen.queryByRole('alert')).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('getFilename', () => { | ||
| const MOCK_DATE = '2026-08-10'; | ||
|
|
||
| beforeEach(() => { | ||
| jest.useFakeTimers().setSystemTime(new Date(`${MOCK_DATE}T12:00:00.000Z`)); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| it('builds the filename from the prefix, current date and email', () => { | ||
| expect(getFilename(MOCK_EMAIL)).toBe( | ||
| `Mozilla-Recovery-Key_${MOCK_DATE}_${MOCK_EMAIL}.txt` | ||
| ); | ||
| }); | ||
|
|
||
| it('replaces characters that are unsafe in a filename', () => { | ||
| expect(getFilename('a/b@example.com')).toBe( | ||
| `Mozilla-Recovery-Key_${MOCK_DATE}_a_b@example.com.txt` | ||
| ); | ||
| }); | ||
|
|
||
| it('truncates a very long email but keeps the prefix, date and extension', () => { | ||
| const longEmail = | ||
| 'supercalifragilisticexpialidocious@marypoppins.superfan.conference.com'; | ||
| const filename = getFilename(longEmail); | ||
|
|
||
| expect(filename).toBe( | ||
| `Mozilla-Recovery-Key_${MOCK_DATE}_supercalifragilisticexpialidocious@mary.txt` | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏽