Skip to content
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