Skip to content

Commit

Permalink
Add Error Case Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jjarvisp committed Aug 28, 2024
1 parent db166ef commit 16b351a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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');
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ describe('confirmSignIn MFA_SETUP challenge happy path cases', () => {
);

jest
.spyOn(signInHelpers, 'handleChallengeName')
.spyOn(clients, 'respondToAuthChallenge')
.mockImplementationOnce(
async (): Promise<RespondToAuthChallengeCommandOutput> =>
authAPITestParams.RespondToAuthChallengeCommandOutput,
Expand Down
26 changes: 26 additions & 0 deletions packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');
}
});
});

0 comments on commit 16b351a

Please sign in to comment.