Skip to content

Commit

Permalink
test(core): rebase backup code refactor
Browse files Browse the repository at this point in the history
rebase backup code refactor
  • Loading branch information
simeng-li committed Jul 26, 2024
1 parent 4390acc commit 7a4f020
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
24 changes: 9 additions & 15 deletions packages/integration-tests/src/client/experience/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ export class ExperienceClient extends MockClient {
.json<{ verificationId: string }>();
}

public async generateMfaBackupCodes() {
return api
.post(`${experienceRoutes.verification}/backup-code/generate`, {
headers: { cookie: this.interactionCookie },
})
.json<{ verificationId: string; codes: string[] }>();
}

public async verifyBackupCode(payload: { code: string }) {
return api
.post(`${experienceRoutes.verification}/backup-code/verify`, {
Expand Down Expand Up @@ -219,24 +227,10 @@ export class ExperienceClient extends MockClient {
});
}

public async bindMfa(type: MfaFactor.TOTP | MfaFactor.WebAuthn, verificationId: string) {
public async bindMfa(type: MfaFactor, verificationId: string) {
return api.post(`${experienceRoutes.mfa}`, {
headers: { cookie: this.interactionCookie },
json: { type, verificationId },
});
}

public async generateMfaBackupCodes() {
return api
.post(`${experienceRoutes.mfa}/backup-codes/generate`, {
headers: { cookie: this.interactionCookie },
})
.json<{ codes: string[] }>();
}

public async bindBackupCodes() {
return api.post(`${experienceRoutes.mfa}/backup-codes`, {
headers: { cookie: this.interactionCookie },
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,12 @@ devFeatureTest.describe('Bind MFA APIs happy path', () => {
status: 422,
});

const { codes } = await client.generateMfaBackupCodes();
const { codes, verificationId: backupCodeVerificationId } =
await client.generateMfaBackupCodes();

expect(codes.length).toBeGreaterThan(0);

await client.bindBackupCodes();
await client.bindMfa(MfaFactor.BackupCode, backupCodeVerificationId);

const { redirectTo } = await client.submitInteraction();
const userId = await processSession(client, redirectTo);
Expand Down Expand Up @@ -261,10 +262,10 @@ devFeatureTest.describe('Bind MFA APIs happy path', () => {
status: 422,
});

const { codes } = await client.generateMfaBackupCodes();
const { codes, verificationId } = await client.generateMfaBackupCodes();
expect(codes.length).toBeGreaterThan(0);

await client.bindBackupCodes();
await client.bindMfa(MfaFactor.BackupCode, verificationId);

const { redirectTo } = await client.submitInteraction();
await processSession(client, redirectTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ devFeatureTest.describe('Bind MFA APIs sad path', () => {
const client = await initExperienceClient();
await identifyUserWithUsernamePassword(client, username, password);

await expectRejects(client.generateMfaBackupCodes(), {
const { verificationId } = await client.generateMfaBackupCodes();

await expectRejects(client.bindMfa(MfaFactor.BackupCode, verificationId), {
code: 'session.mfa.mfa_factor_not_enabled',
status: 400,
});
Expand Down Expand Up @@ -131,15 +133,16 @@ devFeatureTest.describe('Bind MFA APIs sad path', () => {
});
});

it('should throw if the interaction is not verified, when generate new backup codes', async () => {
it('should throw if the interaction is not verified, when add new backup codes', async () => {
const { username, password } = generateNewUserProfile({ username: true, password: true });
const user = await userApi.create({ username, password });
await createUserMfaVerification(user.id, MfaFactor.TOTP);

const client = await initExperienceClient();
await identifyUserWithUsernamePassword(client, username, password);
const { verificationId } = await client.generateMfaBackupCodes();

await expectRejects(client.generateMfaBackupCodes(), {
await expectRejects(client.bindMfa(MfaFactor.BackupCode, verificationId), {
code: 'session.mfa.require_mfa_verification',
status: 403,
});
Expand All @@ -151,7 +154,10 @@ devFeatureTest.describe('Bind MFA APIs sad path', () => {

const client = await initExperienceClient();
await identifyUserWithUsernamePassword(client, username, password);
await expectRejects(client.generateMfaBackupCodes(), {

const { verificationId } = await client.generateMfaBackupCodes();

await expectRejects(client.bindMfa(MfaFactor.BackupCode, verificationId), {
code: 'session.mfa.backup_code_can_not_be_alone',
status: 422,
});
Expand All @@ -165,8 +171,9 @@ devFeatureTest.describe('Bind MFA APIs sad path', () => {
await identifyUserWithUsernamePassword(client, username, password);
const totpVerificationId = await successfullyCreateAndVerifyTotp(client);
await client.bindMfa(MfaFactor.TOTP, totpVerificationId);
await expectRejects(client.bindBackupCodes(), {
code: 'session.mfa.pending_info_not_found',

await expectRejects(client.bindMfa(MfaFactor.BackupCode, 'invalid_verification'), {
code: 'session.verification_session_not_found',
status: 404,
});
});
Expand Down

0 comments on commit 7a4f020

Please sign in to comment.