2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
4
import {
5
- GetCredentialsForIdentityOutput ,
6
5
ResourcesConfig ,
7
- getCredentialsForIdentity ,
6
+ createGetCredentialsForIdentityClient ,
8
7
sharedInMemoryStorage ,
9
8
} from '@aws-amplify/core' ;
10
9
@@ -17,7 +16,7 @@ import { authAPITestParams } from '../testUtils/authApiTestParams';
17
16
18
17
jest . mock ( '@aws-amplify/core' , ( ) => ( {
19
18
...jest . requireActual ( '@aws-amplify/core' ) ,
20
- getCredentialsForIdentity : jest . fn ( ) ,
19
+ createGetCredentialsForIdentityClient : jest . fn ( ) ,
21
20
} ) ) ;
22
21
23
22
jest . mock (
@@ -62,9 +61,23 @@ const disallowGuestAccessConfig: ResourcesConfig = {
62
61
} ,
63
62
} ;
64
63
65
- const credentialsForIdentityIdSpy = getCredentialsForIdentity as jest . Mock ;
64
+ const mockCreateGetIdentityForIdentityClient = jest . mocked (
65
+ createGetCredentialsForIdentityClient ,
66
+ ) ;
67
+
68
+ const mockGetCredentialsForIdentity : jest . MockedFunction <
69
+ ReturnType < typeof createGetCredentialsForIdentityClient >
70
+ > = jest . fn (
71
+ async ( _config , _params ) => authAPITestParams . CredentialsForIdentityIdResult ,
72
+ ) ;
66
73
67
74
describe ( 'credentialsProvider' , ( ) => {
75
+ beforeAll ( ( ) => {
76
+ mockCreateGetIdentityForIdentityClient . mockReturnValue (
77
+ mockGetCredentialsForIdentity ,
78
+ ) ;
79
+ } ) ;
80
+
68
81
describe ( 'Guest Credentials' , ( ) => {
69
82
let cognitoCredentialsProvider : CognitoAWSCredentialsAndIdentityIdProvider ;
70
83
@@ -76,14 +89,14 @@ describe('credentialsProvider', () => {
76
89
identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
77
90
cognitoCredentialsProvider =
78
91
new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
79
- credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
92
+ mockGetCredentialsForIdentity . mockImplementationOnce ( async ( ) => {
80
93
return authAPITestParams . CredentialsForIdentityIdResult ;
81
94
} ) ;
82
95
} ) ;
83
96
84
97
afterEach ( ( ) => {
85
98
cognitoCredentialsProvider . clearCredentials ( ) ;
86
- credentialsForIdentityIdSpy ?. mockReset ( ) ;
99
+ mockGetCredentialsForIdentity ?. mockReset ( ) ;
87
100
} ) ;
88
101
89
102
test ( 'Should call identityIdClient with no logins to obtain guest creds' , async ( ) => {
@@ -97,8 +110,8 @@ describe('credentialsProvider', () => {
97
110
. AccessKeyId ,
98
111
) ;
99
112
100
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
101
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledWith (
113
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
114
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledWith (
102
115
{ region : 'us-east-1' } ,
103
116
{ IdentityId : 'identity-id-test' } ,
104
117
) ;
@@ -112,7 +125,7 @@ describe('credentialsProvider', () => {
112
125
authenticated : false ,
113
126
authConfig : validAuthConfig . Auth ! ,
114
127
} ) ;
115
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
128
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
116
129
const res =
117
130
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
118
131
authenticated : false ,
@@ -123,7 +136,7 @@ describe('credentialsProvider', () => {
123
136
. AccessKeyId ,
124
137
) ;
125
138
// expecting to be called only once becasue in-memory creds should be returned
126
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
139
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
127
140
} ) ;
128
141
} ) ;
129
142
@@ -133,7 +146,7 @@ describe('credentialsProvider', () => {
133
146
new CognitoAWSCredentialsAndIdentityIdProvider (
134
147
new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
135
148
) ;
136
- credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
149
+ mockGetCredentialsForIdentity . mockImplementationOnce ( async ( ) => {
137
150
return authAPITestParams . NoAccessKeyCredentialsForIdentityIdResult ;
138
151
} ) ;
139
152
} ) ;
@@ -142,7 +155,7 @@ describe('credentialsProvider', () => {
142
155
cognitoCredentialsProvider . clearCredentials ( ) ;
143
156
} ) ;
144
157
afterAll ( ( ) => {
145
- credentialsForIdentityIdSpy ?. mockReset ( ) ;
158
+ mockGetCredentialsForIdentity ?. mockReset ( ) ;
146
159
} ) ;
147
160
148
161
test ( 'Should not throw AuthError when allowGuestAccess is false in the config' , async ( ) => {
@@ -173,8 +186,8 @@ describe('credentialsProvider', () => {
173
186
requestId : '123' ,
174
187
} ,
175
188
} ;
176
- credentialsForIdentityIdSpy . mockReset ( ) ;
177
- credentialsForIdentityIdSpy . mockRejectedValue ( mockServiceErrorParams ) ;
189
+ mockGetCredentialsForIdentity . mockReset ( ) ;
190
+ mockGetCredentialsForIdentity . mockRejectedValue ( mockServiceErrorParams ) ;
178
191
try {
179
192
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
180
193
authenticated : false ,
@@ -198,14 +211,14 @@ describe('credentialsProvider', () => {
198
211
identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
199
212
cognitoCredentialsProvider =
200
213
new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
201
- credentialsForIdentityIdSpy . mockImplementation ( async ( ) => {
202
- return authAPITestParams . CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
214
+ mockGetCredentialsForIdentity . mockImplementation ( async ( ) => {
215
+ return authAPITestParams . CredentialsForIdentityIdResult ;
203
216
} ) ;
204
217
} ) ;
205
218
206
219
afterEach ( ( ) => {
207
220
cognitoCredentialsProvider . clearCredentials ( ) ;
208
- credentialsForIdentityIdSpy ?. mockReset ( ) ;
221
+ mockGetCredentialsForIdentity ?. mockReset ( ) ;
209
222
} ) ;
210
223
211
224
test ( 'Should call identityIdClient with the logins map to obtain primary creds' , async ( ) => {
@@ -234,7 +247,7 @@ describe('credentialsProvider', () => {
234
247
authAPITestParams . CredentialsForIdentityIdResult . IdentityId ,
235
248
} ) ;
236
249
237
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
250
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
238
251
} ) ;
239
252
240
253
test ( 'in-memory primary creds are returned if not expired and not past TTL' , async ( ) => {
@@ -243,13 +256,13 @@ describe('credentialsProvider', () => {
243
256
authConfig : validAuthConfig . Auth ! ,
244
257
tokens : authAPITestParams . ValidAuthTokens ,
245
258
} ) ;
246
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledWith (
259
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledWith (
247
260
{
248
261
region : authAPITestParams . CredentialsClientRequest . region ,
249
262
} ,
250
263
authAPITestParams . CredentialsClientRequest . withValidAuthToken ,
251
264
) ;
252
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
265
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
253
266
254
267
const res =
255
268
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
@@ -262,7 +275,7 @@ describe('credentialsProvider', () => {
262
275
. AccessKeyId ,
263
276
) ;
264
277
// expecting to be called only once becasue in-memory creds should be returned
265
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
278
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
266
279
} ) ;
267
280
268
281
test ( 'Should get new credentials when tokens have changed' , async ( ) => {
@@ -271,26 +284,26 @@ describe('credentialsProvider', () => {
271
284
authConfig : validAuthConfig . Auth ! ,
272
285
tokens : authAPITestParams . ValidAuthTokens ,
273
286
} ) ;
274
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledWith (
287
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledWith (
275
288
{
276
289
region : authAPITestParams . CredentialsClientRequest . region ,
277
290
} ,
278
291
authAPITestParams . CredentialsClientRequest . withValidAuthToken ,
279
292
) ;
280
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
293
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 1 ) ;
281
294
282
295
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
283
296
authenticated : true ,
284
297
authConfig : validAuthConfig . Auth ! ,
285
298
tokens : authAPITestParams . NewValidAuthTokens ,
286
299
} ) ;
287
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledWith (
300
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledWith (
288
301
{
289
302
region : authAPITestParams . CredentialsClientRequest . region ,
290
303
} ,
291
304
authAPITestParams . CredentialsClientRequest . withNewValidAuthToken ,
292
305
) ;
293
- expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 2 ) ;
306
+ expect ( mockGetCredentialsForIdentity ) . toHaveBeenCalledTimes ( 2 ) ;
294
307
} ) ;
295
308
} ) ;
296
309
@@ -307,11 +320,11 @@ describe('credentialsProvider', () => {
307
320
} ) ;
308
321
309
322
afterAll ( ( ) => {
310
- credentialsForIdentityIdSpy ?. mockReset ( ) ;
323
+ mockGetCredentialsForIdentity ?. mockReset ( ) ;
311
324
} ) ;
312
325
313
326
test ( 'Should throw AuthError if either Credentials, accessKeyId or secretKey is absent in the response' , async ( ) => {
314
- credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
327
+ mockGetCredentialsForIdentity . mockImplementationOnce ( async ( ) => {
315
328
return authAPITestParams . NoAccessKeyCredentialsForIdentityIdResult ;
316
329
} ) ;
317
330
expect (
@@ -321,8 +334,8 @@ describe('credentialsProvider', () => {
321
334
tokens : authAPITestParams . ValidAuthTokens ,
322
335
} ) ,
323
336
) . rejects . toThrow ( AuthError ) ;
324
- credentialsForIdentityIdSpy . mockClear ( ) ;
325
- credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
337
+ mockGetCredentialsForIdentity . mockClear ( ) ;
338
+ mockGetCredentialsForIdentity . mockImplementationOnce ( async ( ) => {
326
339
return authAPITestParams . NoCredentialsForIdentityIdResult ;
327
340
} ) ;
328
341
expect (
@@ -332,8 +345,8 @@ describe('credentialsProvider', () => {
332
345
tokens : authAPITestParams . ValidAuthTokens ,
333
346
} ) ,
334
347
) . rejects . toThrow ( AuthError ) ;
335
- credentialsForIdentityIdSpy . mockClear ( ) ;
336
- credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
348
+ mockGetCredentialsForIdentity . mockClear ( ) ;
349
+ mockGetCredentialsForIdentity . mockImplementationOnce ( async ( ) => {
337
350
return authAPITestParams . NoSecretKeyInCredentialsForIdentityIdResult ;
338
351
} ) ;
339
352
expect (
@@ -355,8 +368,8 @@ describe('credentialsProvider', () => {
355
368
requestId : '123' ,
356
369
} ,
357
370
} ;
358
- credentialsForIdentityIdSpy . mockReset ( ) ;
359
- credentialsForIdentityIdSpy . mockRejectedValue ( mockServiceErrorParams ) ;
371
+ mockGetCredentialsForIdentity . mockReset ( ) ;
372
+ mockGetCredentialsForIdentity . mockRejectedValue ( mockServiceErrorParams ) ;
360
373
try {
361
374
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
362
375
authenticated : true ,
0 commit comments