|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import setUpTotpConfig from "../../../../src/models/views/setUpTotp"; |
| 3 | +import { createMachine, interpret } from "xstate"; |
| 4 | +import { createTestMachine, createTestModel } from "@xstate/test"; |
| 5 | +import * as actions from "../../../../src/models/config/actions"; |
| 6 | +import * as guards from "../../../../src/models/config/guards"; |
| 7 | +import { useMockUserfront, addGlobalStates } from "../../../utils"; |
| 8 | +import { defaultAuthContext } from "../../../../src/models/forms/universal"; |
| 9 | + |
| 10 | +const machineOptions = { |
| 11 | + actions, |
| 12 | + guards, |
| 13 | +}; |
| 14 | + |
| 15 | +const setUpTotpCodeMachine = createMachine( |
| 16 | + addGlobalStates(setUpTotpConfig), |
| 17 | + <any>machineOptions |
| 18 | +).withContext({ |
| 19 | + config: defaultAuthContext.config, |
| 20 | + user: { |
| 21 | + email: "", |
| 22 | + }, |
| 23 | + action: "setup", |
| 24 | + isSecondFactor: true, |
| 25 | + activeFactor: { |
| 26 | + channel: "authenticator", |
| 27 | + strategy: "totp", |
| 28 | + isConfiguredByUser: true, |
| 29 | + }, |
| 30 | + allowBack: true, |
| 31 | +}); |
| 32 | + |
| 33 | +const testMachine = createTestMachine({ |
| 34 | + initial: "gettingQrCode", |
| 35 | + states: { |
| 36 | + gettingQrCode: { |
| 37 | + on: { |
| 38 | + succeedGettingQrCode: "showingQrCode", |
| 39 | + }, |
| 40 | + }, |
| 41 | + showingQrCode: { |
| 42 | + on: { |
| 43 | + submitQrCode: "confirmingQrCode", |
| 44 | + back: "returnedToFactorSelection", |
| 45 | + }, |
| 46 | + }, |
| 47 | + showingQrCodeWithError: { |
| 48 | + on: { |
| 49 | + submitQrCode: "confirmingQrCode", |
| 50 | + back: "returnedToFactorSelection", |
| 51 | + }, |
| 52 | + }, |
| 53 | + confirmingQrCode: { |
| 54 | + on: { |
| 55 | + failConfirmingCode: "showingQrCodeWithError", |
| 56 | + succeedConfirmingCode: "showingBackupCodes", |
| 57 | + }, |
| 58 | + }, |
| 59 | + showingBackupCodes: { |
| 60 | + on: { |
| 61 | + finish: "showingComplete", |
| 62 | + }, |
| 63 | + }, |
| 64 | + showingComplete: {}, |
| 65 | + returnedToFactorSelection: {}, |
| 66 | + }, |
| 67 | +}); |
| 68 | + |
| 69 | +const testModel = createTestModel(testMachine); |
| 70 | + |
| 71 | +/** |
| 72 | + * Tests for setting up TOTP as a second factor to resolve reported bug: |
| 73 | + * https://linear.app/userfront/issue/DEV-1046/bug-in-mfa-setup |
| 74 | + * |
| 75 | + * NOTE: Mock does not detect calls for store.user.getTotp(), so we are not |
| 76 | + * able to resolve/reject like we do with the other `Userfront` methods. |
| 77 | + * The userfront mock has been modified below to resolve the API's response. |
| 78 | + */ |
| 79 | +describe("model-based: models/mfa/setUpTotpCode", () => { |
| 80 | + testModel.getPaths().forEach((path) => { |
| 81 | + it(path.description, async () => { |
| 82 | + const qrCode = "data:image/png;base64,testqrcode=="; |
| 83 | + const backupCodes = [ |
| 84 | + "60bb6-9393a", |
| 85 | + "1b8ef-e3e4b", |
| 86 | + "1488f-7cd2e", |
| 87 | + "3169e-fa7e3", |
| 88 | + ]; |
| 89 | + // Mock must be initialized before starting machine |
| 90 | + const mockUserfront = useMockUserfront({ |
| 91 | + user: { |
| 92 | + getTotp: () => { |
| 93 | + return new Promise((resolve) => { |
| 94 | + resolve({ |
| 95 | + totpSecret: "testtotpsecret", |
| 96 | + qrCode, |
| 97 | + backupCodes, |
| 98 | + }); |
| 99 | + }); |
| 100 | + }, |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + const setUpTotpCodeService = interpret(setUpTotpCodeMachine); |
| 105 | + setUpTotpCodeService.start(); |
| 106 | + |
| 107 | + const expected = { |
| 108 | + totpCode: "123456", |
| 109 | + // Requests |
| 110 | + getQrCodeReq: { |
| 111 | + success: { |
| 112 | + qrCode, |
| 113 | + backupCodes, |
| 114 | + }, |
| 115 | + }, |
| 116 | + confirmQrCodeReq: { |
| 117 | + error: { |
| 118 | + message: "Confirm code error", |
| 119 | + error: { |
| 120 | + type: "ConfirmCodeError", |
| 121 | + }, |
| 122 | + }, |
| 123 | + success: { |
| 124 | + isMfaRequired: false, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }; |
| 128 | + |
| 129 | + await path.test({ |
| 130 | + states: { |
| 131 | + gettingQrCode: () => { |
| 132 | + const state = setUpTotpCodeService.getSnapshot(); |
| 133 | + expect(state.value).toEqual("getQrCode"); |
| 134 | + expect(state.context.error).toBeFalsy(); |
| 135 | + }, |
| 136 | + showingQrCode: () => { |
| 137 | + const state = setUpTotpCodeService.getSnapshot(); |
| 138 | + expect(state.value).toEqual("showQrCode"); |
| 139 | + expect(state.context.error).toBeFalsy(); |
| 140 | + expect(state.context.view).toEqual({ |
| 141 | + qrCode: expected.getQrCodeReq.success.qrCode, |
| 142 | + backupCodes: expected.getQrCodeReq.success.backupCodes, |
| 143 | + }); |
| 144 | + }, |
| 145 | + confirmingQrCode: () => { |
| 146 | + const state = setUpTotpCodeService.getSnapshot(); |
| 147 | + expect(state.value).toEqual("confirmTotpCode"); |
| 148 | + expect(state.context.error).toBeFalsy(); |
| 149 | + expect(mockUserfront.lastCall?.method).toEqual("login"); |
| 150 | + |
| 151 | + const arg = mockUserfront.lastCall?.args[0]; |
| 152 | + expect(arg).not.toHaveProperty("email"); |
| 153 | + expect(arg.method).toEqual("totp"); |
| 154 | + expect(arg.totpCode).toEqual(expected.totpCode); |
| 155 | + }, |
| 156 | + showingQrCodeWithError: () => { |
| 157 | + const state = setUpTotpCodeService.getSnapshot(); |
| 158 | + expect(state.value).toEqual("showQrCode"); |
| 159 | + expect(state.context.error).toEqual( |
| 160 | + expected.confirmQrCodeReq.error |
| 161 | + ); |
| 162 | + }, |
| 163 | + showingBackupCodes: () => { |
| 164 | + const state = setUpTotpCodeService.getSnapshot(); |
| 165 | + expect(state.value).toEqual("showBackupCodes"); |
| 166 | + expect(state.context.error).toBeFalsy(); |
| 167 | + }, |
| 168 | + showingComplete: () => { |
| 169 | + const state = setUpTotpCodeService.getSnapshot(); |
| 170 | + expect(state.value).toEqual("showBackupCodes"); |
| 171 | + expect(state.context.error).toBeFalsy(); |
| 172 | + }, |
| 173 | + returnedToFactorSelection: () => { |
| 174 | + const state = setUpTotpCodeService.getSnapshot(); |
| 175 | + expect(state.value).toEqual("backToFactors"); |
| 176 | + expect(state.context.error).toBeFalsy(); |
| 177 | + }, |
| 178 | + }, |
| 179 | + events: { |
| 180 | + succeedGettingQrCode: async () => { |
| 181 | + // Mock does not detect call for store.user.getTotp(); do not |
| 182 | + // resolve the getQrCode response here |
| 183 | + }, |
| 184 | + submitQrCode: () => { |
| 185 | + const { totpCode } = expected; |
| 186 | + setUpTotpCodeService.send("submit", { totpCode }); |
| 187 | + }, |
| 188 | + back: () => { |
| 189 | + setUpTotpCodeService.send("back"); |
| 190 | + }, |
| 191 | + failConfirmingCode: async () => { |
| 192 | + try { |
| 193 | + await mockUserfront.reject(expected.confirmQrCodeReq.error); |
| 194 | + } catch (error) { |
| 195 | + await Promise.resolve(); |
| 196 | + return; |
| 197 | + } |
| 198 | + }, |
| 199 | + succeedConfirmingCode: async () => { |
| 200 | + try { |
| 201 | + await mockUserfront.resolve(expected.confirmQrCodeReq.success); |
| 202 | + } catch (error) { |
| 203 | + await Promise.resolve(); |
| 204 | + return; |
| 205 | + } |
| 206 | + }, |
| 207 | + }, |
| 208 | + }); |
| 209 | + }); |
| 210 | + }); |
| 211 | +}); |
0 commit comments