-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathconfirmSignInErrorCases.test.ts
More file actions
114 lines (102 loc) · 3.62 KB
/
Copy pathconfirmSignInErrorCases.test.ts
File metadata and controls
114 lines (102 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { AuthError } from '../../../src/errors/AuthError';
import { AuthValidationErrorCode } from '../../../src/errors/types/validation';
import { confirmSignIn } from '../../../src/providers/cognito/apis/confirmSignIn';
import { RespondToAuthChallengeException } from '../../../src/providers/cognito/types/errors';
import { signInStore } from '../../../src/client/utils/store';
import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings';
import { createRespondToAuthChallengeClient } from '../../../src/foundation/factories/serviceClients/cognitoIdentityProvider';
import { createMockAmplifyContext } from '../../testUtils/mockAmplifyContext';
import { getMockError } from './testUtils/data';
import { authAPITestParams } from './testUtils/authApiTestParams';
jest.mock('../../../src/client/utils/store');
jest.mock(
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider',
);
jest.mock('../../../src/providers/cognito/factories');
const mockCtx = createMockAmplifyContext({
Auth: {
Cognito: {
userPoolClientId: '111111-aaaaa-42d8-891d-ee81a1549398',
userPoolId: 'us-west-2_zzzzz',
identityPoolId: 'us-west-2:xxxxxx',
},
},
});
describe('confirmSignIn API error path cases:', () => {
const challengeName = 'SELECT_MFA_TYPE';
const signInSession = '1234234232';
const { username } = authAPITestParams.user1;
// assert mocks
const mockStoreGetState = jest.mocked(signInStore.getState);
const mockRespondToAuthChallenge = jest.fn();
const mockCreateRespondToAuthChallengeClient = jest.mocked(
createRespondToAuthChallengeClient,
);
beforeAll(() => {
mockStoreGetState.mockReturnValue({
username,
challengeName,
signInSession,
});
});
beforeEach(() => {
mockCreateRespondToAuthChallengeClient.mockReturnValueOnce(
mockRespondToAuthChallenge,
);
});
afterEach(() => {
mockRespondToAuthChallenge.mockReset();
mockCreateRespondToAuthChallengeClient.mockClear();
});
it('confirmSignIn API should throw an error when challengeResponse is empty', async () => {
expect.assertions(2);
try {
await confirmSignIn(mockCtx, { challengeResponse: '' });
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(AuthValidationErrorCode.EmptyChallengeResponse);
}
});
it('should throw an error when sign-in step is CONTINUE_SIGN_IN_WITH_MFA_SELECTION and challengeResponse is not "SMS", "TOTP", or "EMAIL"', async () => {
expect.assertions(2);
try {
await confirmSignIn(mockCtx, { challengeResponse: 'NO_SMS' });
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(AuthValidationErrorCode.IncorrectMFAMethod);
}
});
it('should throw an error when service returns an error response', async () => {
expect.assertions(2);
mockRespondToAuthChallenge.mockImplementation(() => {
throw getMockError(
RespondToAuthChallengeException.InvalidParameterException,
);
});
try {
await confirmSignIn(mockCtx, { challengeResponse: 'TOTP' });
} catch (error: any) {
expect(error).toBeInstanceOf(AuthError);
expect(error.name).toBe(
RespondToAuthChallengeException.InvalidParameterException,
);
}
});
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(mockCtx, {
challengeResponse: 'SMS',
});
} catch (err: any) {
expect(err).toBeInstanceOf(AuthError);
expect(err.name).toBe(AuthErrorCodes.SignInException);
expect(err.message).toContain('SMS');
}
});
});