@@ -8,9 +8,8 @@ import { AuthValidationErrorCode } from '../../../src/errors/types/validation';
8
8
import { getCurrentUser , signIn } from '../../../src/providers/cognito' ;
9
9
import { InitiateAuthException } from '../../../src/providers/cognito/types/errors' ;
10
10
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' ;
13
11
import { createInitiateAuthClient } from '../../../src/foundation/factories/serviceClients/cognitoIdentityProvider' ;
12
+ import { AuthErrorCodes } from '../../../src/common/AuthErrorStrings' ;
14
13
15
14
import { authAPITestParams } from './testUtils/authApiTestParams' ;
16
15
import { getMockError } from './testUtils/data' ;
@@ -28,11 +27,13 @@ jest.mock('../../../src/providers/cognito/apis/getCurrentUser');
28
27
jest . mock (
29
28
'../../../src/foundation/factories/serviceClients/cognitoIdentityProvider' ,
30
29
) ;
30
+ jest . mock ( '../../../src/providers/cognito/tokenProvider' ) ;
31
31
32
32
describe ( 'signIn API error path cases:' , ( ) => {
33
33
// assert mocks
34
34
const mockCreateInitiateAuthClient = jest . mocked ( createInitiateAuthClient ) ;
35
35
const mockInitiateAuth = jest . fn ( ) ;
36
+
36
37
const mockedGetCurrentUser = getCurrentUser as jest . Mock ;
37
38
38
39
beforeAll ( ( ) => {
@@ -45,7 +46,7 @@ describe('signIn API error path cases:', () => {
45
46
46
47
afterEach ( ( ) => {
47
48
mockedGetCurrentUser . mockReset ( ) ;
48
- mockInitiateAuth . mockClear ( ) ;
49
+ mockInitiateAuth . mockReset ( ) ;
49
50
} ) ;
50
51
51
52
it ( 'should throw an error when a user is already signed-in' , async ( ) => {
@@ -90,42 +91,44 @@ describe('signIn API error path cases:', () => {
90
91
} ) ;
91
92
92
93
it ( 'should throw an error when service returns an error response' , async ( ) => {
93
- expect . assertions ( 2 ) ;
94
94
mockInitiateAuth . mockImplementation ( ( ) => {
95
95
throw getMockError ( InitiateAuthException . InvalidParameterException ) ;
96
96
} ) ;
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
+ ) ;
106
109
} ) ;
107
110
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
+ } ) ) ;
119
118
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
+ ) ;
130
133
} ) ;
131
134
} ) ;
0 commit comments