@@ -11,18 +11,17 @@ import {
11
11
import {
12
12
CognitoAWSCredentialsAndIdentityIdProvider ,
13
13
DefaultIdentityIdStore ,
14
- } from '../../../src/providers/cognito' ;
15
- import { AuthError } from '../../../src/errors/AuthError' ;
16
-
17
- import { authAPITestParams } from './testUtils/authApiTestParams' ;
14
+ } from '../../../../src/providers/cognito' ;
15
+ import { AuthError } from '../../../../src/errors/AuthError' ;
16
+ import { authAPITestParams } from '../testUtils/authApiTestParams' ;
18
17
19
18
jest . mock ( '@aws-amplify/core' , ( ) => ( {
20
19
...jest . requireActual ( '@aws-amplify/core' ) ,
21
20
getCredentialsForIdentity : jest . fn ( ) ,
22
21
} ) ) ;
23
22
24
23
jest . mock (
25
- './../../../src/providers/cognito/credentialsProvider/IdentityIdProvider' ,
24
+ './../../../../ src/providers/cognito/credentialsProvider/IdentityIdProvider' ,
26
25
( ) => ( {
27
26
cognitoIdentityIdProvider : jest
28
27
. fn ( )
@@ -70,18 +69,20 @@ describe('Guest Credentials', () => {
70
69
71
70
describe ( 'Happy Path Cases:' , ( ) => {
72
71
beforeEach ( ( ) => {
72
+ const identityIdStore = new DefaultIdentityIdStore ( sharedInMemoryStorage ) ;
73
+ identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
73
74
cognitoCredentialsProvider =
74
- new CognitoAWSCredentialsAndIdentityIdProvider (
75
- new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
76
- ) ;
75
+ new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
77
76
credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
78
77
return authAPITestParams . CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
79
78
} ) ;
80
79
} ) ;
80
+
81
81
afterEach ( ( ) => {
82
82
cognitoCredentialsProvider . clearCredentials ( ) ;
83
83
credentialsForIdentityIdSpy ?. mockReset ( ) ;
84
84
} ) ;
85
+
85
86
test ( 'Should call identityIdClient with no logins to obtain guest creds' , async ( ) => {
86
87
const res = await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
87
88
authenticated : false ,
@@ -138,6 +139,7 @@ describe('Guest Credentials', () => {
138
139
afterAll ( ( ) => {
139
140
credentialsForIdentityIdSpy ?. mockReset ( ) ;
140
141
} ) ;
142
+
141
143
test ( 'Should not throw AuthError when allowGuestAccess is false in the config' , async ( ) => {
142
144
expect (
143
145
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
@@ -146,6 +148,7 @@ describe('Guest Credentials', () => {
146
148
} ) ,
147
149
) . toBe ( undefined ) ;
148
150
} ) ;
151
+
149
152
test ( 'Should not throw AuthError when there is no Cognito object in the config' , async ( ) => {
150
153
expect (
151
154
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
@@ -161,31 +164,47 @@ describe('Primary Credentials', () => {
161
164
let cognitoCredentialsProvider : CognitoAWSCredentialsAndIdentityIdProvider ;
162
165
describe ( 'Happy Path Cases:' , ( ) => {
163
166
beforeEach ( ( ) => {
167
+ const identityIdStore = new DefaultIdentityIdStore ( sharedInMemoryStorage ) ;
168
+ identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
164
169
cognitoCredentialsProvider =
165
- new CognitoAWSCredentialsAndIdentityIdProvider (
166
- new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
167
- ) ;
170
+ new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
168
171
credentialsForIdentityIdSpy . mockImplementation ( async ( ) => {
169
172
return authAPITestParams . CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
170
173
} ) ;
171
174
} ) ;
175
+
172
176
afterEach ( ( ) => {
173
177
cognitoCredentialsProvider . clearCredentials ( ) ;
174
178
credentialsForIdentityIdSpy ?. mockReset ( ) ;
175
179
} ) ;
180
+
176
181
test ( 'Should call identityIdClient with the logins map to obtain primary creds' , async ( ) => {
177
182
const res = await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
178
183
authenticated : true ,
179
184
authConfig : validAuthConfig . Auth ! ,
180
185
tokens : authAPITestParams . ValidAuthTokens ,
181
186
} ) ;
182
- expect ( res ?. credentials . accessKeyId ) . toEqual (
183
- authAPITestParams . CredentialsForIdentityIdResult . Credentials
184
- . AccessKeyId ,
185
- ) ;
187
+ expect ( res ) . toMatchObject ( {
188
+ credentials : {
189
+ accessKeyId :
190
+ authAPITestParams . CredentialsForIdentityIdResult . Credentials
191
+ . AccessKeyId ,
192
+ expiration :
193
+ authAPITestParams . CredentialsForIdentityIdResult . Credentials
194
+ . Expiration ,
195
+ secretAccessKey :
196
+ authAPITestParams . CredentialsForIdentityIdResult . Credentials
197
+ . SecretKey ,
198
+ sessionToken :
199
+ authAPITestParams . CredentialsForIdentityIdResult . Credentials
200
+ . SessionToken ,
201
+ } ,
202
+ identityId : authAPITestParams . CredentialsForIdentityIdResult . IdentityId ,
203
+ } ) ;
186
204
187
205
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
188
206
} ) ;
207
+
189
208
test ( 'in-memory primary creds are returned if not expired and not past TTL' , async ( ) => {
190
209
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
191
210
authenticated : true ,
@@ -212,6 +231,7 @@ describe('Primary Credentials', () => {
212
231
// expecting to be called only once becasue in-memory creds should be returned
213
232
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
214
233
} ) ;
234
+
215
235
test ( 'Should get new credentials when tokens have changed' , async ( ) => {
216
236
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
217
237
authenticated : true ,
@@ -240,19 +260,23 @@ describe('Primary Credentials', () => {
240
260
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 2 ) ;
241
261
} ) ;
242
262
} ) ;
263
+
243
264
describe ( 'Error Path Cases:' , ( ) => {
244
265
beforeEach ( ( ) => {
245
266
cognitoCredentialsProvider =
246
267
new CognitoAWSCredentialsAndIdentityIdProvider (
247
268
new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
248
269
) ;
249
270
} ) ;
271
+
250
272
afterEach ( ( ) => {
251
273
cognitoCredentialsProvider . clearCredentials ( ) ;
252
274
} ) ;
275
+
253
276
afterAll ( ( ) => {
254
277
credentialsForIdentityIdSpy ?. mockReset ( ) ;
255
278
} ) ;
279
+
256
280
test ( 'Should throw AuthError if either Credentials, accessKeyId or secretKey is absent in the response' , async ( ) => {
257
281
credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
258
282
return authAPITestParams . NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
0 commit comments