|
| 1 | +import { ConnectorType } from '@logto/connector-kit'; |
| 2 | +import { InteractionEvent, SignInIdentifier } from '@logto/schemas'; |
| 3 | + |
| 4 | +import { initExperienceClient, logoutClient, processSession } from '#src/helpers/client.js'; |
| 5 | +import { clearConnectorsByTypes, setEmailConnector } from '#src/helpers/connector.js'; |
| 6 | +import { identifyUserWithUsernamePassword } from '#src/helpers/experience/index.js'; |
| 7 | +import { |
| 8 | + successfullySendVerificationCode, |
| 9 | + successfullyVerifyVerificationCode, |
| 10 | +} from '#src/helpers/experience/verification-code.js'; |
| 11 | +import { |
| 12 | + enableAllPasswordSignInMethods, |
| 13 | + enableMandatoryMfaWithEmail, |
| 14 | + resetMfaSettings, |
| 15 | +} from '#src/helpers/sign-in-experience.js'; |
| 16 | +import { generateNewUserProfile, UserApiTest } from '#src/helpers/user.js'; |
| 17 | +import { devFeatureTest } from '#src/utils.js'; |
| 18 | + |
| 19 | +const { describe, it } = devFeatureTest; |
| 20 | + |
| 21 | +describe('Email MFA verification APIs', () => { |
| 22 | + const userApi = new UserApiTest(); |
| 23 | + |
| 24 | + beforeAll(async () => { |
| 25 | + await clearConnectorsByTypes([ConnectorType.Email]); |
| 26 | + await setEmailConnector(); |
| 27 | + await enableAllPasswordSignInMethods(); |
| 28 | + await enableMandatoryMfaWithEmail(); |
| 29 | + }); |
| 30 | + |
| 31 | + afterAll(async () => { |
| 32 | + await clearConnectorsByTypes([ConnectorType.Email]); |
| 33 | + await resetMfaSettings(); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + await userApi.cleanUp(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should verify Email MFA during sign-in when user already has email set', async () => { |
| 41 | + await enableMandatoryMfaWithEmail(); |
| 42 | + |
| 43 | + const { username, password, primaryEmail } = generateNewUserProfile({ |
| 44 | + username: true, |
| 45 | + password: true, |
| 46 | + primaryEmail: true, |
| 47 | + }); |
| 48 | + await userApi.create({ username, password, primaryEmail }); |
| 49 | + |
| 50 | + const client = await initExperienceClient({ |
| 51 | + interactionEvent: InteractionEvent.SignIn, |
| 52 | + }); |
| 53 | + await identifyUserWithUsernamePassword(client, username, password); |
| 54 | + |
| 55 | + const { verificationId, code } = await successfullySendVerificationCode(client, { |
| 56 | + identifier: { type: SignInIdentifier.Email, value: primaryEmail }, |
| 57 | + interactionEvent: InteractionEvent.SignIn, |
| 58 | + }); |
| 59 | + await successfullyVerifyVerificationCode(client, { |
| 60 | + identifier: { type: SignInIdentifier.Email, value: primaryEmail }, |
| 61 | + verificationId, |
| 62 | + code, |
| 63 | + }); |
| 64 | + |
| 65 | + const { redirectTo } = await client.submitInteraction(); |
| 66 | + await processSession(client, redirectTo); |
| 67 | + await logoutClient(client); |
| 68 | + }); |
| 69 | +}); |
0 commit comments