Skip to content

Commit 655d6b3

Browse files
committed
Unit test cases for sign-in flow (#6640)
* adding test cases for signing in * fixed test cases to handle async signin
1 parent 35d59b0 commit 655d6b3

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

packages/auth/src/mfa/assertions/totp.test.ts

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
2424
import { Endpoint } from '../../api';
2525
import { MultiFactorSessionImpl } from '../../mfa/mfa_session';
2626
import { StartTotpMfaEnrollmentResponse } from '../../api/account_management/mfa';
27-
import { FinalizeMfaResponse } from '../../api/authentication/mfa';
27+
import {
28+
FinalizeMfaResponse,
29+
StartTotpMfaSignInResponse
30+
} from '../../api/authentication/mfa';
2831
import {
2932
TotpMultiFactorAssertionImpl,
3033
TotpMultiFactorGenerator,
@@ -34,6 +37,7 @@ import { FactorId } from '../../model/public_types';
3437
import { AuthErrorCode } from '../../core/errors';
3538
import { AppName } from '../../model/auth';
3639
import { _castAuth } from '../../core/auth/auth_impl';
40+
import { MultiFactorAssertionImpl } from '../mfa_assertion';
3741

3842
use(chaiAsPromised);
3943

@@ -208,6 +212,84 @@ describe('core/mfa/totp/assertions/TotpMultiFactorAssertionImpl', () => {
208212
});
209213
});
210214

215+
describe('Testing signin Flow', () => {
216+
let auth: TestAuth;
217+
let assertion: MultiFactorAssertionImpl;
218+
let totpSignInResponse: StartTotpMfaSignInResponse;
219+
let session: MultiFactorSessionImpl;
220+
beforeEach(async () => {
221+
mockFetch.setUp();
222+
auth = await testAuth();
223+
session = MultiFactorSessionImpl._fromMfaPendingCredential(
224+
'mfa-pending-credential'
225+
);
226+
});
227+
afterEach(mockFetch.tearDown);
228+
229+
it('should finalize mfa signin for totp', async () => {
230+
totpSignInResponse = {
231+
verificationCode: '123456',
232+
idToken: 'final-id-token',
233+
refreshToken: 'refresh-token'
234+
} as any;
235+
assertion = TotpMultiFactorGenerator.assertionForSignIn(
236+
'enrollment-id',
237+
'123456'
238+
) as any;
239+
240+
const mock = mockEndpoint(
241+
Endpoint.FINALIZE_MFA_SIGN_IN,
242+
totpSignInResponse
243+
);
244+
245+
const response = await assertion._process(auth, session);
246+
247+
expect(response).to.eql(totpSignInResponse);
248+
249+
expect(mock.calls[0].request).to.eql({
250+
mfaPendingCredential: 'mfa-pending-credential',
251+
mfaEnrollmentId: 'enrollment-id',
252+
totpVerificationInfo: {
253+
verificationCode: '123456'
254+
}
255+
});
256+
});
257+
258+
it('should throw Firebase Error if enrollment-id is undefined', async () => {
259+
let _response: FinalizeMfaResponse;
260+
totpSignInResponse = {
261+
verificationCode: '123456',
262+
idToken: 'final-id-token',
263+
refreshToken: 'refresh-token'
264+
} as any;
265+
assertion = TotpMultiFactorGenerator.assertionForSignIn(
266+
undefined as any,
267+
'123456'
268+
) as any;
269+
270+
await expect(assertion._process(auth, session)).to.be.rejectedWith(
271+
'auth/argument-error'
272+
);
273+
});
274+
275+
it('should throw Firebase Error if otp is undefined', async () => {
276+
let _response: FinalizeMfaResponse;
277+
totpSignInResponse = {
278+
verificationCode: '123456',
279+
idToken: 'final-id-token',
280+
refreshToken: 'refresh-token'
281+
} as any;
282+
assertion = TotpMultiFactorGenerator.assertionForSignIn(
283+
'enrollment-id',
284+
undefined as any
285+
) as any;
286+
287+
await expect(assertion._process(auth, session)).to.be.rejectedWith(
288+
'auth/argument-error'
289+
);
290+
});
291+
});
292+
211293
describe('core/mfa/assertions/totp/TotpSecret', async () => {
212294
const serverResponse: StartTotpMfaEnrollmentResponse = {
213295
totpSessionInfo: {

0 commit comments

Comments
 (0)