@@ -4,6 +4,7 @@ import { advanceTo } from 'jest-date-mock';
44import { CognitoIdentityClient } from '../CognitoIdentityClient' ;
55import { Credentials } from '@aws-sdk/types' ;
66import { getReadableStream } from '../../test-utils/test-utils' ;
7+ import { IDENTITY_KEY } from '../../utils/constants' ;
78
89const mockCredentials =
910 '{ "IdentityId": "a", "Credentials": { "AccessKeyId": "x", "SecretKey": "y", "SessionToken": "z" } }' ;
@@ -22,6 +23,7 @@ describe('CognitoIdentityClient tests', () => {
2223 beforeEach ( ( ) => {
2324 advanceTo ( 0 ) ;
2425 fetchHandler . mockClear ( ) ;
26+ localStorage . clear ( ) ;
2527
2628 // @ts -ignore
2729 FetchHttpHandler . mockImplementation ( ( ) => {
@@ -74,7 +76,7 @@ describe('CognitoIdentityClient tests', () => {
7476 } ) ;
7577
7678 // Assert
77- return expect (
79+ await expect (
7880 client . getCredentialsForIdentity ( 'my-fake-identity-id' )
7981 ) . rejects . toEqual ( expected ) ;
8082 } ) ;
@@ -175,4 +177,106 @@ describe('CognitoIdentityClient tests', () => {
175177 } )
176178 ) . rejects . toEqual ( expected ) ;
177179 } ) ;
180+
181+ test ( 'when identity Id is retrieved from Cognito then next identity Id is retrieved from localStorage' , async ( ) => {
182+ fetchHandler . mockResolvedValueOnce ( {
183+ response : {
184+ body : getReadableStream ( mockIdCommand )
185+ }
186+ } ) ;
187+
188+ // Init
189+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
190+ fetchRequestHandler : new FetchHttpHandler ( ) ,
191+ region : Utils . AWS_RUM_REGION
192+ } ) ;
193+
194+ // Run
195+ await client . getId ( { IdentityPoolId : 'my-fake-identity-pool-id' } ) ;
196+ const idCommand = await client . getId ( {
197+ IdentityPoolId : 'my-fake-identity-pool-id'
198+ } ) ;
199+
200+ // Assert
201+ expect ( fetchHandler ) . toHaveBeenCalledTimes ( 1 ) ;
202+ expect ( idCommand ) . toMatchObject ( {
203+ IdentityId : 'mockId'
204+ } ) ;
205+ } ) ;
206+
207+ test ( 'when getCredentialsForIdentity returns a ResourceNotFoundException then an error is thrown' , async ( ) => {
208+ fetchHandler . mockResolvedValueOnce ( {
209+ response : {
210+ body : getReadableStream (
211+ '{"__type": "ResourceNotFoundException", "message": ""}'
212+ )
213+ }
214+ } ) ;
215+ const expected : Error = new Error (
216+ `CWR: Failed to retrieve credentials for Cognito identity: Error: ResourceNotFoundException: `
217+ ) ;
218+
219+ // Init
220+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
221+ fetchRequestHandler : new FetchHttpHandler ( ) ,
222+ region : Utils . AWS_RUM_REGION
223+ } ) ;
224+
225+ // Assert
226+ await expect (
227+ client . getCredentialsForIdentity ( 'my-fake-identity-id' )
228+ ) . rejects . toEqual ( expected ) ;
229+ } ) ;
230+
231+ test ( 'when getCredentialsForIdentity returns a ValidationException then an error is thrown' , async ( ) => {
232+ fetchHandler . mockResolvedValueOnce ( {
233+ response : {
234+ body : getReadableStream (
235+ '{"__type": "ValidationException", "message": ""}'
236+ )
237+ }
238+ } ) ;
239+ const expected : Error = new Error (
240+ `CWR: Failed to retrieve credentials for Cognito identity: Error: ValidationException: `
241+ ) ;
242+
243+ // Init
244+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
245+ fetchRequestHandler : new FetchHttpHandler ( ) ,
246+ region : Utils . AWS_RUM_REGION
247+ } ) ;
248+
249+ // Assert
250+ await expect (
251+ client . getCredentialsForIdentity ( 'my-fake-identity-id' )
252+ ) . rejects . toEqual ( expected ) ;
253+ } ) ;
254+
255+ test ( 'when getCredentialsForIdentity returns a ResourceNotFoundException then identity id is removed from localStorage ' , async ( ) => {
256+ localStorage . setItem ( IDENTITY_KEY , 'my-fake-identity-id' ) ;
257+
258+ fetchHandler . mockResolvedValueOnce ( {
259+ response : {
260+ body : getReadableStream (
261+ '{"__type": "ResourceNotFoundException", "message": ""}'
262+ )
263+ }
264+ } ) ;
265+
266+ // Init
267+ const client : CognitoIdentityClient = new CognitoIdentityClient ( {
268+ fetchRequestHandler : new FetchHttpHandler ( ) ,
269+ region : Utils . AWS_RUM_REGION
270+ } ) ;
271+
272+ // Run
273+ try {
274+ await client . getCredentialsForIdentity ( 'my-fake-identity-id' ) ;
275+ } catch ( e ) {
276+ // Ignore
277+ }
278+
279+ // Assert
280+ expect ( localStorage . getItem ( IDENTITY_KEY ) ) . toBe ( null ) ;
281+ } ) ;
178282} ) ;
0 commit comments