@@ -69,18 +69,20 @@ describe('Guest Credentials', () => {
69
69
70
70
describe ( 'Happy Path Cases:' , ( ) => {
71
71
beforeEach ( ( ) => {
72
+ const identityIdStore = new DefaultIdentityIdStore ( sharedInMemoryStorage ) ;
73
+ identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
72
74
cognitoCredentialsProvider =
73
- new CognitoAWSCredentialsAndIdentityIdProvider (
74
- new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
75
- ) ;
75
+ new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
76
76
credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
77
77
return authAPITestParams . CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
78
78
} ) ;
79
79
} ) ;
80
+
80
81
afterEach ( ( ) => {
81
82
cognitoCredentialsProvider . clearCredentials ( ) ;
82
83
credentialsForIdentityIdSpy ?. mockReset ( ) ;
83
84
} ) ;
85
+
84
86
test ( 'Should call identityIdClient with no logins to obtain guest creds' , async ( ) => {
85
87
const res = await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
86
88
authenticated : false ,
@@ -137,6 +139,7 @@ describe('Guest Credentials', () => {
137
139
afterAll ( ( ) => {
138
140
credentialsForIdentityIdSpy ?. mockReset ( ) ;
139
141
} ) ;
142
+
140
143
test ( 'Should not throw AuthError when allowGuestAccess is false in the config' , async ( ) => {
141
144
expect (
142
145
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
@@ -145,6 +148,7 @@ describe('Guest Credentials', () => {
145
148
} ) ,
146
149
) . toBe ( undefined ) ;
147
150
} ) ;
151
+
148
152
test ( 'Should not throw AuthError when there is no Cognito object in the config' , async ( ) => {
149
153
expect (
150
154
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
@@ -160,31 +164,47 @@ describe('Primary Credentials', () => {
160
164
let cognitoCredentialsProvider : CognitoAWSCredentialsAndIdentityIdProvider ;
161
165
describe ( 'Happy Path Cases:' , ( ) => {
162
166
beforeEach ( ( ) => {
167
+ const identityIdStore = new DefaultIdentityIdStore ( sharedInMemoryStorage ) ;
168
+ identityIdStore . setAuthConfig ( validAuthConfig . Auth ! ) ;
163
169
cognitoCredentialsProvider =
164
- new CognitoAWSCredentialsAndIdentityIdProvider (
165
- new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
166
- ) ;
170
+ new CognitoAWSCredentialsAndIdentityIdProvider ( identityIdStore ) ;
167
171
credentialsForIdentityIdSpy . mockImplementation ( async ( ) => {
168
172
return authAPITestParams . CredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
169
173
} ) ;
170
174
} ) ;
175
+
171
176
afterEach ( ( ) => {
172
177
cognitoCredentialsProvider . clearCredentials ( ) ;
173
178
credentialsForIdentityIdSpy ?. mockReset ( ) ;
174
179
} ) ;
180
+
175
181
test ( 'Should call identityIdClient with the logins map to obtain primary creds' , async ( ) => {
176
182
const res = await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
177
183
authenticated : true ,
178
184
authConfig : validAuthConfig . Auth ! ,
179
185
tokens : authAPITestParams . ValidAuthTokens ,
180
186
} ) ;
181
- expect ( res ?. credentials . accessKeyId ) . toEqual (
182
- authAPITestParams . CredentialsForIdentityIdResult . Credentials
183
- . AccessKeyId ,
184
- ) ;
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
+ } ) ;
185
204
186
205
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
187
206
} ) ;
207
+
188
208
test ( 'in-memory primary creds are returned if not expired and not past TTL' , async ( ) => {
189
209
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
190
210
authenticated : true ,
@@ -211,6 +231,7 @@ describe('Primary Credentials', () => {
211
231
// expecting to be called only once becasue in-memory creds should be returned
212
232
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 1 ) ;
213
233
} ) ;
234
+
214
235
test ( 'Should get new credentials when tokens have changed' , async ( ) => {
215
236
await cognitoCredentialsProvider . getCredentialsAndIdentityId ( {
216
237
authenticated : true ,
@@ -239,19 +260,23 @@ describe('Primary Credentials', () => {
239
260
expect ( credentialsForIdentityIdSpy ) . toHaveBeenCalledTimes ( 2 ) ;
240
261
} ) ;
241
262
} ) ;
263
+
242
264
describe ( 'Error Path Cases:' , ( ) => {
243
265
beforeEach ( ( ) => {
244
266
cognitoCredentialsProvider =
245
267
new CognitoAWSCredentialsAndIdentityIdProvider (
246
268
new DefaultIdentityIdStore ( sharedInMemoryStorage ) ,
247
269
) ;
248
270
} ) ;
271
+
249
272
afterEach ( ( ) => {
250
273
cognitoCredentialsProvider . clearCredentials ( ) ;
251
274
} ) ;
275
+
252
276
afterAll ( ( ) => {
253
277
credentialsForIdentityIdSpy ?. mockReset ( ) ;
254
278
} ) ;
279
+
255
280
test ( 'Should throw AuthError if either Credentials, accessKeyId or secretKey is absent in the response' , async ( ) => {
256
281
credentialsForIdentityIdSpy . mockImplementationOnce ( async ( ) => {
257
282
return authAPITestParams . NoAccessKeyCredentialsForIdentityIdResult as GetCredentialsForIdentityOutput ;
0 commit comments