From 814a39ef954c7733b0d9d67c9549bbd9c1b46360 Mon Sep 17 00:00:00 2001 From: Parijat Bhatt Date: Wed, 28 Sep 2022 22:21:47 -0700 Subject: [PATCH 1/2] adding test cases for signing in --- packages/auth/src/mfa/assertions/totp.test.ts | 91 ++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/packages/auth/src/mfa/assertions/totp.test.ts b/packages/auth/src/mfa/assertions/totp.test.ts index 17c8f323f08..1d8aaef16d0 100644 --- a/packages/auth/src/mfa/assertions/totp.test.ts +++ b/packages/auth/src/mfa/assertions/totp.test.ts @@ -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, @@ -34,6 +37,8 @@ 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'; +import { FirebaseError } from '@firebase/util'; use(chaiAsPromised); @@ -208,6 +213,90 @@ 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; + + try { + _response = await assertion._process(auth, session); + } catch (e) { + expect(e).to.be.an.instanceOf(FirebaseError); + expect(e.message).to.eql('Firebase: Error (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; + + try { + _response = await assertion._process(auth, session); + } catch (e) { + expect(e).to.be.an.instanceOf(FirebaseError); + expect(e.message).to.eql('Firebase: Error (auth/argument-error).'); + } + }); +}); + describe('core/mfa/assertions/totp/TotpSecret', async () => { const serverResponse: StartTotpMfaEnrollmentResponse = { totpSessionInfo: { From 4ebae0a3b39aa442b5547069d6fc3a8d2f22603e Mon Sep 17 00:00:00 2001 From: Parijat Bhatt Date: Wed, 12 Oct 2022 14:30:39 -0700 Subject: [PATCH 2/2] fixed test cases to handle async signin --- packages/auth/src/mfa/assertions/totp.test.ts | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/packages/auth/src/mfa/assertions/totp.test.ts b/packages/auth/src/mfa/assertions/totp.test.ts index 1d8aaef16d0..e30e85ad5e2 100644 --- a/packages/auth/src/mfa/assertions/totp.test.ts +++ b/packages/auth/src/mfa/assertions/totp.test.ts @@ -38,7 +38,6 @@ import { AuthErrorCode } from '../../core/errors'; import { AppName } from '../../model/auth'; import { _castAuth } from '../../core/auth/auth_impl'; import { MultiFactorAssertionImpl } from '../mfa_assertion'; -import { FirebaseError } from '@firebase/util'; use(chaiAsPromised); @@ -268,12 +267,9 @@ describe('Testing signin Flow', () => { '123456' ) as any; - try { - _response = await assertion._process(auth, session); - } catch (e) { - expect(e).to.be.an.instanceOf(FirebaseError); - expect(e.message).to.eql('Firebase: Error (auth/argument-error).'); - } + await expect(assertion._process(auth, session)).to.be.rejectedWith( + 'auth/argument-error' + ); }); it('should throw Firebase Error if otp is undefined', async () => { @@ -288,12 +284,9 @@ describe('Testing signin Flow', () => { undefined as any ) as any; - try { - _response = await assertion._process(auth, session); - } catch (e) { - expect(e).to.be.an.instanceOf(FirebaseError); - expect(e.message).to.eql('Firebase: Error (auth/argument-error).'); - } + await expect(assertion._process(auth, session)).to.be.rejectedWith( + 'auth/argument-error' + ); }); });