Skip to content

Unit test cases for sign-in flow #6640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 83 additions & 1 deletion packages/auth/src/mfa/assertions/totp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
import { Endpoint } from '../../api';
import { MultiFactorSessionImpl } from '../../mfa/mfa_session';
import { StartTotpMfaEnrollmentResponse } from '../../api/account_management/mfa';
import { FinalizeMfaResponse } from '../../api/authentication/mfa';
import {
FinalizeMfaResponse,
StartTotpMfaSignInResponse
} from '../../api/authentication/mfa';
import {
TotpMultiFactorAssertionImpl,
TotpMultiFactorGenerator,
Expand All @@ -34,6 +37,7 @@ import { FactorId } from '../../model/public_types';
import { AuthErrorCode } from '../../core/errors';
import { AppName } from '../../model/auth';
import { _castAuth } from '../../core/auth/auth_impl';
import { MultiFactorAssertionImpl } from '../mfa_assertion';

use(chaiAsPromised);

Expand Down Expand Up @@ -208,6 +212,84 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
});
});

describe('Testing signin Flow', () => {
let auth: TestAuth;
let assertion: MultiFactorAssertionImpl;
let totpSignInResponse: StartTotpMfaSignInResponse;
let session: MultiFactorSessionImpl;
beforeEach(async () => {
mockFetch.setUp();
auth = await testAuth();
session = MultiFactorSessionImpl._fromMfaPendingCredential(
'mfa-pending-credential'
);
});
afterEach(mockFetch.tearDown);

it('should finalize mfa signin for totp', async () => {
totpSignInResponse = {
verificationCode: '123456',
idToken: 'final-id-token',
refreshToken: 'refresh-token'
} as any;
assertion = TotpMultiFactorGenerator.assertionForSignIn(
'enrollment-id',
'123456'
) as any;

const mock = mockEndpoint(
Endpoint.FINALIZE_MFA_SIGN_IN,
totpSignInResponse
);

const response = await assertion._process(auth, session);

expect(response).to.eql(totpSignInResponse);

expect(mock.calls[0].request).to.eql({
mfaPendingCredential: 'mfa-pending-credential',
mfaEnrollmentId: 'enrollment-id',
totpVerificationInfo: {
verificationCode: '123456'
}
});
});

it('should throw Firebase Error if enrollment-id is undefined', async () => {
let _response: FinalizeMfaResponse;
totpSignInResponse = {
verificationCode: '123456',
idToken: 'final-id-token',
refreshToken: 'refresh-token'
} as any;
assertion = TotpMultiFactorGenerator.assertionForSignIn(
undefined as any,
'123456'
) as any;

await expect(assertion._process(auth, session)).to.be.rejectedWith(
'auth/argument-error'
);
});

it('should throw Firebase Error if otp is undefined', async () => {
let _response: FinalizeMfaResponse;
totpSignInResponse = {
verificationCode: '123456',
idToken: 'final-id-token',
refreshToken: 'refresh-token'
} as any;
assertion = TotpMultiFactorGenerator.assertionForSignIn(
'enrollment-id',
undefined as any
) as any;

await expect(assertion._process(auth, session)).to.be.rejectedWith(
'auth/argument-error'
);
});
});

describe('core/mfa/assertions/totp/TotpSecret', async () => {
const serverResponse: StartTotpMfaEnrollmentResponse = {
totpSessionInfo: {
Expand Down