This repository was archived by the owner on Mar 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Do not send user email when setting up TOTP as 2nd factor #104
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5c708b
Allow store to be modified in test Userfront mock
damionvega 521f8a5
Prevent email to be sent when setting up TOTP as 2nd factor
damionvega a692517
Format readme
damionvega fdf42cd
Remove password instructions on login form
damionvega 42226a4
Update to v1.1.0-alpha.3
github-actions[bot] 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
211 changes: 211 additions & 0 deletions
211
package/test/model-based/models/mfa/setUpTotpCode.test.ts
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,211 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import setUpTotpConfig from "../../../../src/models/views/setUpTotp"; | ||
| import { createMachine, interpret } from "xstate"; | ||
| import { createTestMachine, createTestModel } from "@xstate/test"; | ||
| import * as actions from "../../../../src/models/config/actions"; | ||
| import * as guards from "../../../../src/models/config/guards"; | ||
| import { useMockUserfront, addGlobalStates } from "../../../utils"; | ||
| import { defaultAuthContext } from "../../../../src/models/forms/universal"; | ||
|
|
||
| const machineOptions = { | ||
| actions, | ||
| guards, | ||
| }; | ||
|
|
||
| const setUpTotpCodeMachine = createMachine( | ||
| addGlobalStates(setUpTotpConfig), | ||
| <any>machineOptions | ||
| ).withContext({ | ||
| config: defaultAuthContext.config, | ||
| user: { | ||
| email: "", | ||
| }, | ||
| action: "setup", | ||
| isSecondFactor: true, | ||
| activeFactor: { | ||
| channel: "authenticator", | ||
| strategy: "totp", | ||
| isConfiguredByUser: true, | ||
| }, | ||
| allowBack: true, | ||
| }); | ||
|
|
||
| const testMachine = createTestMachine({ | ||
| initial: "gettingQrCode", | ||
| states: { | ||
| gettingQrCode: { | ||
| on: { | ||
| succeedGettingQrCode: "showingQrCode", | ||
| }, | ||
| }, | ||
| showingQrCode: { | ||
| on: { | ||
| submitQrCode: "confirmingQrCode", | ||
| back: "returnedToFactorSelection", | ||
| }, | ||
| }, | ||
| showingQrCodeWithError: { | ||
| on: { | ||
| submitQrCode: "confirmingQrCode", | ||
| back: "returnedToFactorSelection", | ||
| }, | ||
| }, | ||
| confirmingQrCode: { | ||
| on: { | ||
| failConfirmingCode: "showingQrCodeWithError", | ||
| succeedConfirmingCode: "showingBackupCodes", | ||
| }, | ||
| }, | ||
| showingBackupCodes: { | ||
| on: { | ||
| finish: "showingComplete", | ||
| }, | ||
| }, | ||
| showingComplete: {}, | ||
| returnedToFactorSelection: {}, | ||
| }, | ||
| }); | ||
|
|
||
| const testModel = createTestModel(testMachine); | ||
|
|
||
| /** | ||
| * Tests for setting up TOTP as a second factor to resolve reported bug: | ||
| * https://linear.app/userfront/issue/DEV-1046/bug-in-mfa-setup | ||
| * | ||
| * NOTE: Mock does not detect calls for store.user.getTotp(), so we are not | ||
| * able to resolve/reject like we do with the other `Userfront` methods. | ||
| * The userfront mock has been modified below to resolve the API's response. | ||
| */ | ||
| describe("model-based: models/mfa/setUpTotpCode", () => { | ||
| testModel.getPaths().forEach((path) => { | ||
| it(path.description, async () => { | ||
| const qrCode = "data:image/png;base64,testqrcode=="; | ||
| const backupCodes = [ | ||
| "60bb6-9393a", | ||
| "1b8ef-e3e4b", | ||
| "1488f-7cd2e", | ||
| "3169e-fa7e3", | ||
| ]; | ||
| // Mock must be initialized before starting machine | ||
| const mockUserfront = useMockUserfront({ | ||
| user: { | ||
| getTotp: () => { | ||
| return new Promise((resolve) => { | ||
| resolve({ | ||
| totpSecret: "testtotpsecret", | ||
| qrCode, | ||
| backupCodes, | ||
| }); | ||
| }); | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const setUpTotpCodeService = interpret(setUpTotpCodeMachine); | ||
| setUpTotpCodeService.start(); | ||
|
|
||
| const expected = { | ||
| totpCode: "123456", | ||
| // Requests | ||
| getQrCodeReq: { | ||
| success: { | ||
| qrCode, | ||
| backupCodes, | ||
| }, | ||
| }, | ||
| confirmQrCodeReq: { | ||
| error: { | ||
| message: "Confirm code error", | ||
| error: { | ||
| type: "ConfirmCodeError", | ||
| }, | ||
| }, | ||
| success: { | ||
| isMfaRequired: false, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await path.test({ | ||
| states: { | ||
| gettingQrCode: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("getQrCode"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| }, | ||
| showingQrCode: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("showQrCode"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| expect(state.context.view).toEqual({ | ||
| qrCode: expected.getQrCodeReq.success.qrCode, | ||
| backupCodes: expected.getQrCodeReq.success.backupCodes, | ||
| }); | ||
| }, | ||
| confirmingQrCode: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("confirmTotpCode"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| expect(mockUserfront.lastCall?.method).toEqual("login"); | ||
|
|
||
| const arg = mockUserfront.lastCall?.args[0]; | ||
| expect(arg).not.toHaveProperty("email"); | ||
| expect(arg.method).toEqual("totp"); | ||
| expect(arg.totpCode).toEqual(expected.totpCode); | ||
| }, | ||
| showingQrCodeWithError: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("showQrCode"); | ||
| expect(state.context.error).toEqual( | ||
| expected.confirmQrCodeReq.error | ||
| ); | ||
| }, | ||
| showingBackupCodes: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("showBackupCodes"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| }, | ||
| showingComplete: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("showBackupCodes"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| }, | ||
| returnedToFactorSelection: () => { | ||
| const state = setUpTotpCodeService.getSnapshot(); | ||
| expect(state.value).toEqual("backToFactors"); | ||
| expect(state.context.error).toBeFalsy(); | ||
| }, | ||
| }, | ||
| events: { | ||
| succeedGettingQrCode: async () => { | ||
| // Mock does not detect call for store.user.getTotp(); do not | ||
| // resolve the getQrCode response here | ||
| }, | ||
| submitQrCode: () => { | ||
| const { totpCode } = expected; | ||
| setUpTotpCodeService.send("submit", { totpCode }); | ||
| }, | ||
| back: () => { | ||
| setUpTotpCodeService.send("back"); | ||
| }, | ||
| failConfirmingCode: async () => { | ||
| try { | ||
| await mockUserfront.reject(expected.confirmQrCodeReq.error); | ||
| } catch (error) { | ||
| await Promise.resolve(); | ||
| return; | ||
| } | ||
| }, | ||
| succeedConfirmingCode: async () => { | ||
| try { | ||
| await mockUserfront.resolve(expected.confirmQrCodeReq.success); | ||
| } catch (error) { | ||
| await Promise.resolve(); | ||
| return; | ||
| } | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
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.
The
email/emailOrUsernamecheck has been added here.