|
| 1 | +import { ConnectorType } from '@logto/connector-kit'; |
| 2 | +import { |
| 3 | + InteractionEvent, |
| 4 | + SignInIdentifier, |
| 5 | + type VerificationCodeSignInIdentifier, |
| 6 | +} from '@logto/schemas'; |
| 7 | + |
| 8 | +import { initExperienceClient, logoutClient, processSession } from '#src/helpers/client.js'; |
| 9 | +import { |
| 10 | + clearConnectorsByTypes, |
| 11 | + setEmailConnector, |
| 12 | + setSmsConnector, |
| 13 | +} from '#src/helpers/connector.js'; |
| 14 | +import { identifyUserWithUsernamePassword } from '#src/helpers/experience/index.js'; |
| 15 | +import { |
| 16 | + successfullySendVerificationCode, |
| 17 | + successfullyVerifyVerificationCode, |
| 18 | +} from '#src/helpers/experience/verification-code.js'; |
| 19 | +import { |
| 20 | + enableAllPasswordSignInMethods, |
| 21 | + enableMandatoryMfaWithEmail, |
| 22 | + enableMandatoryMfaWithPhone, |
| 23 | + resetMfaSettings, |
| 24 | +} from '#src/helpers/sign-in-experience.js'; |
| 25 | +import { generateNewUserProfile, UserApiTest } from '#src/helpers/user.js'; |
| 26 | +import { devFeatureTest } from '#src/utils.js'; |
| 27 | + |
| 28 | +const { describe, it } = devFeatureTest; |
| 29 | + |
| 30 | +const mfaTestCases = [ |
| 31 | + { |
| 32 | + type: 'Email', |
| 33 | + identifierType: SignInIdentifier.Email as VerificationCodeSignInIdentifier, |
| 34 | + setConnector: setEmailConnector, |
| 35 | + enableMfa: enableMandatoryMfaWithEmail, |
| 36 | + userProfileKey: 'primaryEmail' as const, |
| 37 | + }, |
| 38 | + { |
| 39 | + type: 'Phone', |
| 40 | + identifierType: SignInIdentifier.Phone as VerificationCodeSignInIdentifier, |
| 41 | + setConnector: setSmsConnector, |
| 42 | + enableMfa: enableMandatoryMfaWithPhone, |
| 43 | + userProfileKey: 'primaryPhone' as const, |
| 44 | + }, |
| 45 | +]; |
| 46 | + |
| 47 | +describe.each(mfaTestCases)( |
| 48 | + '$type MFA verification APIs', |
| 49 | + ({ type, identifierType, setConnector, enableMfa, userProfileKey }) => { |
| 50 | + const userApi = new UserApiTest(); |
| 51 | + |
| 52 | + beforeAll(async () => { |
| 53 | + await clearConnectorsByTypes([ConnectorType.Email, ConnectorType.Sms]); |
| 54 | + await setConnector(); |
| 55 | + await enableAllPasswordSignInMethods(); |
| 56 | + await enableMfa(); |
| 57 | + }); |
| 58 | + |
| 59 | + afterAll(async () => { |
| 60 | + await clearConnectorsByTypes([ConnectorType.Email, ConnectorType.Sms]); |
| 61 | + await resetMfaSettings(); |
| 62 | + }); |
| 63 | + |
| 64 | + afterEach(async () => { |
| 65 | + await userApi.cleanUp(); |
| 66 | + }); |
| 67 | + |
| 68 | + it(`should verify ${type} MFA during sign-in when user already has ${type.toLowerCase()} set`, async () => { |
| 69 | + await enableMfa(); |
| 70 | + |
| 71 | + const userProfile = generateNewUserProfile({ |
| 72 | + username: true, |
| 73 | + password: true, |
| 74 | + [userProfileKey]: true, |
| 75 | + }); |
| 76 | + await userApi.create(userProfile); |
| 77 | + |
| 78 | + const client = await initExperienceClient({ |
| 79 | + interactionEvent: InteractionEvent.SignIn, |
| 80 | + }); |
| 81 | + await identifyUserWithUsernamePassword(client, userProfile.username, userProfile.password); |
| 82 | + |
| 83 | + const identifierValue = userProfile[userProfileKey]!; |
| 84 | + const { verificationId, code } = await successfullySendVerificationCode(client, { |
| 85 | + identifier: { type: identifierType, value: identifierValue }, |
| 86 | + interactionEvent: InteractionEvent.SignIn, |
| 87 | + }); |
| 88 | + await successfullyVerifyVerificationCode(client, { |
| 89 | + identifier: { type: identifierType, value: identifierValue }, |
| 90 | + verificationId, |
| 91 | + code, |
| 92 | + }); |
| 93 | + |
| 94 | + const { redirectTo } = await client.submitInteraction(); |
| 95 | + await processSession(client, redirectTo); |
| 96 | + await logoutClient(client); |
| 97 | + }); |
| 98 | + } |
| 99 | +); |
0 commit comments