@@ -251,16 +251,108 @@ describe('CognitoIdentityClient tests', () => {
251251 ) . rejects . toEqual ( expected ) ;
252252 } ) ;
253253
254- test ( 'when getCredentialsForIdentity returns a ResourceNotFoundException then identity id is removed from localStorage ' , async ( ) => {
254+ test ( 'when getCredentialsForIdentity returns bad response then an error is thrown' , async ( ) => {
255+ fetchHandler . mockResolvedValueOnce ( {
256+ response : {
257+ body : getReadableStream ( '{}' )
258+ }
259+ } ) ;
260+ const expected : Error = new Error (
261+ `CWR: Failed to retrieve credentials for Cognito identity: Error: Unknown Credentials response`
262+ ) ;
263+
264+ // Init
265+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
266+ fetchRequestHandler : new FetchHttpHandler ( ) ,
267+ region : Utils . AWS_RUM_REGION
268+ } ) ;
269+
270+ // Assert
271+ await expect (
272+ client . getCredentialsForIdentity ( 'my-fake-identity-id' )
273+ ) . rejects . toEqual ( expected ) ;
274+ } ) ;
275+
276+ test ( 'when getCredentialsForIdentity returns bad response then identity id is removed from localStorage ' , async ( ) => {
255277 localStorage . setItem ( IDENTITY_KEY , 'my-fake-identity-id' ) ;
256278
279+ fetchHandler . mockResolvedValueOnce ( {
280+ response : {
281+ body : getReadableStream ( 'not-json' )
282+ }
283+ } ) ;
284+
285+ // Init
286+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
287+ fetchRequestHandler : new FetchHttpHandler ( ) ,
288+ region : Utils . AWS_RUM_REGION
289+ } ) ;
290+
291+ // Run
292+ try {
293+ await client . getCredentialsForIdentity ( 'my-fake-identity-id' ) ;
294+ } catch ( e ) {
295+ // Ignore
296+ }
297+
298+ // Assert
299+ expect ( localStorage . getItem ( IDENTITY_KEY ) ) . toBe ( null ) ;
300+ } ) ;
301+
302+ test ( 'when getOpenIdToken returns a ResourceNotFoundException then an error is thrown' , async ( ) => {
257303 fetchHandler . mockResolvedValueOnce ( {
258304 response : {
259305 body : getReadableStream (
260306 '{"__type": "ResourceNotFoundException", "message": ""}'
261307 )
262308 }
263309 } ) ;
310+ const expected : Error = new Error (
311+ `CWR: Failed to retrieve Cognito OpenId token: Error: ResourceNotFoundException: `
312+ ) ;
313+
314+ // Init
315+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
316+ fetchRequestHandler : new FetchHttpHandler ( ) ,
317+ region : Utils . AWS_RUM_REGION
318+ } ) ;
319+
320+ // Assert
321+ await expect (
322+ client . getOpenIdToken ( { IdentityId : 'my-fake-identity-id' } )
323+ ) . rejects . toEqual ( expected ) ;
324+ } ) ;
325+
326+ test ( 'when getOpenIdToken returns a bad response then an error is thrown' , async ( ) => {
327+ fetchHandler . mockResolvedValueOnce ( {
328+ response : {
329+ body : getReadableStream ( '{}' )
330+ }
331+ } ) ;
332+ const expected : Error = new Error (
333+ `CWR: Failed to retrieve Cognito OpenId token: Error: Unknown OpenIdToken response`
334+ ) ;
335+
336+ // Init
337+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
338+ fetchRequestHandler : new FetchHttpHandler ( ) ,
339+ region : Utils . AWS_RUM_REGION
340+ } ) ;
341+
342+ // Assert
343+ await expect (
344+ client . getOpenIdToken ( { IdentityId : 'my-fake-identity-id' } )
345+ ) . rejects . toEqual ( expected ) ;
346+ } ) ;
347+
348+ test ( 'when getOpenIdToken returns a bad response then identity id is removed from localStorage ' , async ( ) => {
349+ localStorage . setItem ( IDENTITY_KEY , 'my-fake-identity-id' ) ;
350+
351+ fetchHandler . mockResolvedValueOnce ( {
352+ response : {
353+ body : getReadableStream ( 'not-json' )
354+ }
355+ } ) ;
264356
265357 // Init
266358 const client : CognitoIdentityClient = new CognitoIdentityClient ( {
@@ -270,7 +362,7 @@ describe('CognitoIdentityClient tests', () => {
270362
271363 // Run
272364 try {
273- await client . getCredentialsForIdentity ( 'my-fake-identity-id' ) ;
365+ await client . getOpenIdToken ( { IdentityId : 'my-fake-identity-id' } ) ;
274366 } catch ( e ) {
275367 // Ignore
276368 }
0 commit comments