From 16b351a82f6aad3ec413d9ad089f29eb6fe14935 Mon Sep 17 00:00:00 2001 From: James Jarvis Date: Wed, 28 Aug 2024 16:45:34 -0400 Subject: [PATCH] Add Error Case Unit Tests --- .../cognito/confirmSignInErrorCases.test.ts | 24 +++++++++++++++-- .../cognito/confirmSignInHappyCases.test.ts | 2 +- .../cognito/signInErrorCases.test.ts | 26 +++++++++++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/packages/auth/__tests__/providers/cognito/confirmSignInErrorCases.test.ts b/packages/auth/__tests__/providers/cognito/confirmSignInErrorCases.test.ts index 355f89c2ac8..3516c146e6a 100644 --- a/packages/auth/__tests__/providers/cognito/confirmSignInErrorCases.test.ts +++ b/packages/auth/__tests__/providers/cognito/confirmSignInErrorCases.test.ts @@ -6,6 +6,7 @@ import { confirmSignIn } from '../../../src/providers/cognito/apis/confirmSignIn import { RespondToAuthChallengeException } from '../../../src/providers/cognito/types/errors'; import { respondToAuthChallenge } from '../../../src/providers/cognito/utils/clients/CognitoIdentityProvider'; import { signInStore } from '../../../src/providers/cognito/utils/signInStore'; +import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings'; import { getMockError } from './testUtils/data'; import { setUpGetConfig } from './testUtils/setUpGetConfig'; @@ -25,8 +26,8 @@ describe('confirmSignIn API error path cases:', () => { const signInSession = '1234234232'; const { username } = authAPITestParams.user1; // assert mocks - const mockStoreGetState = signInStore.getState as jest.Mock; - const mockRespondToAuthChallenge = respondToAuthChallenge as jest.Mock; + const mockStoreGetState = jest.mocked(signInStore.getState); + const mockRespondToAuthChallenge = jest.mocked(respondToAuthChallenge); beforeAll(() => { setUpGetConfig(Amplify); @@ -77,4 +78,23 @@ describe('confirmSignIn API error path cases:', () => { ); } }); + it('should throw an error when sign-in step is MFA_SETUP and challengeResponse is not valid', async () => { + expect.assertions(3); + + mockStoreGetState.mockReturnValue({ + username, + challengeName: 'MFA_SETUP', + signInSession, + }); + + try { + await confirmSignIn({ + challengeResponse: 'SMS', + }); + } catch (err: any) { + expect(err).toBeInstanceOf(AuthError); + expect(err.name).toBe(AuthErrorCodes.SignInException); + expect(err.message).toContain('SMS'); + } + }); }); diff --git a/packages/auth/__tests__/providers/cognito/confirmSignInHappyCases.test.ts b/packages/auth/__tests__/providers/cognito/confirmSignInHappyCases.test.ts index d8ad83f1d42..11bd3a85b08 100644 --- a/packages/auth/__tests__/providers/cognito/confirmSignInHappyCases.test.ts +++ b/packages/auth/__tests__/providers/cognito/confirmSignInHappyCases.test.ts @@ -825,7 +825,7 @@ describe('confirmSignIn MFA_SETUP challenge happy path cases', () => { ); jest - .spyOn(signInHelpers, 'handleChallengeName') + .spyOn(clients, 'respondToAuthChallenge') .mockImplementationOnce( async (): Promise => authAPITestParams.RespondToAuthChallengeCommandOutput, diff --git a/packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts b/packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts index b4e8453b17d..fee490b8bfa 100644 --- a/packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts +++ b/packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts @@ -9,6 +9,8 @@ import { getCurrentUser, signIn } from '../../../src/providers/cognito'; import { initiateAuth } from '../../../src/providers/cognito/utils/clients/CognitoIdentityProvider'; import { InitiateAuthException } from '../../../src/providers/cognito/types/errors'; import { USER_ALREADY_AUTHENTICATED_EXCEPTION } from '../../../src/errors/constants'; +import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings'; +import * as signInHelpers from '../../../src/providers/cognito/utils/signInHelpers'; import { authAPITestParams } from './testUtils/authApiTestParams'; import { getMockError } from './testUtils/data'; @@ -97,4 +99,28 @@ describe('signIn API error path cases:', () => { expect(error.name).toBe(InitiateAuthException.InvalidParameterException); } }); + it('should throw an error when sign in step is MFA_SETUP and there are no valid setup options', async () => { + expect.assertions(3); + + jest + .spyOn(signInHelpers, 'handleUserSRPAuthFlow') + .mockImplementationOnce(async () => ({ + ChallengeName: 'MFA_SETUP', + ChallengeParameters: { + MFAS_CAN_SETUP: '["SMS_MFA"]', + }, + $metadata: {}, + })); + + try { + await signIn({ + username: authAPITestParams.user1.username, + password: authAPITestParams.user1.password, + }); + } catch (error: any) { + expect(error).toBeInstanceOf(AuthError); + expect(error.name).toBe(AuthErrorCodes.SignInException); + expect(error.message).toContain('SMS'); + } + }); });