Skip to content

Commit 3493336

Browse files
committed
chore: address test strategy feedback
1 parent 2fa41ae commit 3493336

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

packages/auth/__tests__/providers/cognito/signInErrorCases.test.ts

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import { AuthValidationErrorCode } from '../../../src/errors/types/validation';
88
import { getCurrentUser, signIn } from '../../../src/providers/cognito';
99
import { InitiateAuthException } from '../../../src/providers/cognito/types/errors';
1010
import { USER_ALREADY_AUTHENTICATED_EXCEPTION } from '../../../src/errors/constants';
11-
import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings';
12-
import * as signInHelpers from '../../../src/providers/cognito/utils/signInHelpers';
1311
import { createInitiateAuthClient } from '../../../src/foundation/factories/serviceClients/cognitoIdentityProvider';
12+
import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings';
1413

1514
import { authAPITestParams } from './testUtils/authApiTestParams';
1615
import { getMockError } from './testUtils/data';
@@ -28,11 +27,13 @@ jest.mock('../../../src/providers/cognito/apis/getCurrentUser');
2827
jest.mock(
2928
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
3029
);
30+
jest.mock('../../../src/providers/cognito/tokenProvider');
3131

3232
describe('signIn API error path cases:', () => {
3333
// assert mocks
3434
const mockCreateInitiateAuthClient = jest.mocked(createInitiateAuthClient);
3535
const mockInitiateAuth = jest.fn();
36+
3637
const mockedGetCurrentUser = getCurrentUser as jest.Mock;
3738

3839
beforeAll(() => {
@@ -45,7 +46,7 @@ describe('signIn API error path cases:', () => {
4546

4647
afterEach(() => {
4748
mockedGetCurrentUser.mockReset();
48-
mockInitiateAuth.mockClear();
49+
mockInitiateAuth.mockReset();
4950
});
5051

5152
it('should throw an error when a user is already signed-in', async () => {
@@ -90,42 +91,44 @@ describe('signIn API error path cases:', () => {
9091
});
9192

9293
it('should throw an error when service returns an error response', async () => {
93-
expect.assertions(2);
9494
mockInitiateAuth.mockImplementation(() => {
9595
throw getMockError(InitiateAuthException.InvalidParameterException);
9696
});
97-
try {
98-
await signIn({
99-
username: authAPITestParams.user1.username,
100-
password: authAPITestParams.user1.password,
101-
});
102-
} catch (error: any) {
103-
expect(error).toBeInstanceOf(AuthError);
104-
expect(error.name).toBe(InitiateAuthException.InvalidParameterException);
105-
}
97+
98+
const p = signIn({
99+
username: authAPITestParams.user1.username,
100+
password: authAPITestParams.user1.password,
101+
});
102+
103+
expect(p).rejects.toThrow(
104+
new AuthError({
105+
name: InitiateAuthException.InvalidParameterException,
106+
message: 'Error message',
107+
}),
108+
);
106109
});
107110
it('should throw an error when sign in step is MFA_SETUP and there are no valid setup options', async () => {
108-
expect.assertions(3);
109-
110-
jest
111-
.spyOn(signInHelpers, 'handleUserSRPAuthFlow')
112-
.mockImplementationOnce(async () => ({
113-
ChallengeName: 'MFA_SETUP',
114-
ChallengeParameters: {
115-
MFAS_CAN_SETUP: '["SMS_MFA"]',
116-
},
117-
$metadata: {},
118-
}));
111+
mockInitiateAuth.mockImplementation(() => ({
112+
ChallengeName: 'MFA_SETUP',
113+
ChallengeParameters: {
114+
MFAS_CAN_SETUP: '["SMS_MFA"]',
115+
},
116+
$metadata: {},
117+
}));
119118

120-
try {
121-
await signIn({
122-
username: authAPITestParams.user1.username,
123-
password: authAPITestParams.user1.password,
124-
});
125-
} catch (error: any) {
126-
expect(error).toBeInstanceOf(AuthError);
127-
expect(error.name).toBe(AuthErrorCodes.SignInException);
128-
expect(error.message).toContain('SMS');
129-
}
119+
const p = signIn({
120+
username: authAPITestParams.user1.username,
121+
password: authAPITestParams.user1.password,
122+
options: {
123+
authFlowType: 'USER_PASSWORD_AUTH',
124+
},
125+
});
126+
127+
expect(p).rejects.toThrow(
128+
new AuthError({
129+
name: AuthErrorCodes.SignInException,
130+
message: 'Cannot initiate MFA setup from available types: SMS',
131+
}),
132+
);
130133
});
131134
});

0 commit comments

Comments
 (0)