-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(auth): additional granular unit tests for identityIdStore and t…
…okenOrchestrator (#12527)
- Loading branch information
1 parent
86ab927
commit 3cfd338
Showing
3 changed files
with
192 additions
and
10 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
packages/auth/__tests__/providers/cognito/identityIdStore.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DefaultIdentityIdStore } from '../../../src/providers/cognito'; | ||
import { Identity, ResourcesConfig } from '@aws-amplify/core'; | ||
|
||
const mockKeyValueStorage = { | ||
setItem: jest.fn(), | ||
getItem: jest.fn(), | ||
removeItem: jest.fn(), | ||
clear: jest.fn(), | ||
}; | ||
|
||
const validAuthConfig: ResourcesConfig = { | ||
Auth: { | ||
Cognito: { | ||
userPoolId: 'us-east-1_test-id', | ||
identityPoolId: 'us-east-1:test-id', | ||
userPoolClientId: 'test-id', | ||
allowGuestAccess: true, | ||
}, | ||
}, | ||
}; | ||
const validAuthKey = { | ||
identityId: `com.amplify.Cognito.${ | ||
validAuthConfig.Auth!.Cognito!.identityPoolId | ||
}.identityId`, | ||
}; | ||
const validGuestIdentityId: Identity = { type: 'guest', id: 'guest-id' }; | ||
const validPrimaryIdentityId: Identity = { type: 'primary', id: 'primary-id' }; | ||
|
||
const noIdentityPoolIdAuthConfig: ResourcesConfig = { | ||
Auth: { | ||
Cognito: { | ||
userPoolId: 'us-east-1_test-id', | ||
userPoolClientId: 'test-id', | ||
}, | ||
}, | ||
}; | ||
|
||
describe('DefaultIdentityIdStore', () => { | ||
const defaultIdStore = new DefaultIdentityIdStore(mockKeyValueStorage); | ||
describe('Happy Path Cases:', () => { | ||
beforeAll(() => { | ||
defaultIdStore.setAuthConfig(validAuthConfig.Auth!); | ||
}); | ||
it('Should set the Auth config required to form the storage keys', async () => { | ||
expect(defaultIdStore._authKeys).toEqual(validAuthKey); | ||
}); | ||
it('Should store guest identityId in keyValueStorage', async () => { | ||
defaultIdStore.storeIdentityId(validGuestIdentityId); | ||
expect(mockKeyValueStorage.setItem).toBeCalledWith( | ||
validAuthKey.identityId, | ||
validGuestIdentityId.id | ||
); | ||
expect(defaultIdStore._primaryIdentityId).toBeUndefined(); | ||
}); | ||
it('Should load guest identityId from keyValueStorage', async () => { | ||
mockKeyValueStorage.getItem.mockReturnValue(validGuestIdentityId.id); | ||
|
||
expect(await defaultIdStore.loadIdentityId()).toEqual( | ||
validGuestIdentityId | ||
); | ||
}); | ||
it('Should store primary identityId in keyValueStorage', async () => { | ||
defaultIdStore.storeIdentityId(validPrimaryIdentityId); | ||
expect(mockKeyValueStorage.removeItem).toBeCalledWith( | ||
validAuthKey.identityId | ||
); | ||
expect(defaultIdStore._primaryIdentityId).toEqual( | ||
validPrimaryIdentityId.id | ||
); | ||
}); | ||
it('Should load primary identityId from keyValueStorage', async () => { | ||
expect(await defaultIdStore.loadIdentityId()).toEqual( | ||
validPrimaryIdentityId | ||
); | ||
}); | ||
it('Should clear the cached identityId', async () => { | ||
defaultIdStore.clearIdentityId(); | ||
expect(mockKeyValueStorage.removeItem).toBeCalledWith( | ||
validAuthKey.identityId | ||
); | ||
expect(defaultIdStore._primaryIdentityId).toBeUndefined(); | ||
}); | ||
}); | ||
describe('Error Path Cases:', () => { | ||
it('Should assert when identityPoolId is not present while setting the auth config', async () => { | ||
try { | ||
defaultIdStore.setAuthConfig(noIdentityPoolIdAuthConfig.Auth!); | ||
} catch (e) { | ||
expect(e.name).toEqual('InvalidIdentityPoolIdException'); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/auth/__tests__/providers/cognito/tokenOrchestrator.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { TokenOrchestrator } from '../../../src/providers/cognito'; | ||
import { Hub, ResourcesConfig } from '@aws-amplify/core'; | ||
import { authAPITestParams } from './testUtils/authApiTestParams'; | ||
import { AMPLIFY_SYMBOL } from '@aws-amplify/core/internals/utils'; | ||
jest.mock('@aws-amplify/core', () => ({ | ||
...jest.requireActual('@aws-amplify/core'), | ||
Hub: { | ||
dispatch: jest.fn(), | ||
listen: jest.fn(), | ||
}, | ||
})); | ||
|
||
const mockAuthTokenStore = { | ||
getLastAuthUser: jest.fn(), | ||
loadTokens: jest.fn(), | ||
storeTokens: jest.fn(), | ||
clearTokens: jest.fn(), | ||
setKeyValueStorage: jest.fn(), | ||
getDeviceMetadata: jest.fn(), | ||
clearDeviceMetadata: jest.fn(), | ||
}; | ||
const mockTokenRefresher = jest.fn(); | ||
const validAuthConfig: ResourcesConfig = { | ||
Auth: { | ||
Cognito: { | ||
userPoolId: 'us-east-1_test-id', | ||
identityPoolId: 'us-east-1:test-id', | ||
userPoolClientId: 'test-id', | ||
allowGuestAccess: true, | ||
}, | ||
}, | ||
}; | ||
|
||
describe('TokenOrchestrator', () => { | ||
const tokenOrchestrator = new TokenOrchestrator(); | ||
describe('Happy Path Cases:', () => { | ||
beforeAll(() => { | ||
tokenOrchestrator.setAuthConfig(validAuthConfig.Auth!); | ||
tokenOrchestrator.setAuthTokenStore(mockAuthTokenStore); | ||
tokenOrchestrator.setTokenRefresher(mockTokenRefresher); | ||
mockAuthTokenStore.getLastAuthUser.mockResolvedValue('test-username'); | ||
}); | ||
it('Should get tokens', async () => { | ||
mockAuthTokenStore.loadTokens.mockResolvedValue( | ||
authAPITestParams.ValidAuthTokens | ||
); | ||
|
||
const tokensRes = await tokenOrchestrator.getTokens(); | ||
expect(tokensRes).toEqual({ | ||
accessToken: authAPITestParams.ValidAuthTokens.accessToken, | ||
idToken: authAPITestParams.ValidAuthTokens.idToken, | ||
signInDetails: undefined, | ||
}); | ||
}); | ||
it('Should call tokenRefresher and return valid tokens', async () => { | ||
mockAuthTokenStore.loadTokens.mockResolvedValue( | ||
authAPITestParams.ExpiredAuthTokens | ||
); | ||
mockTokenRefresher.mockResolvedValue(authAPITestParams.ValidAuthTokens); | ||
const tokensRes = await tokenOrchestrator.getTokens(); | ||
expect(tokensRes).toEqual({ | ||
accessToken: authAPITestParams.ValidAuthTokens.accessToken, | ||
idToken: authAPITestParams.ValidAuthTokens.idToken, | ||
signInDetails: undefined, | ||
}); | ||
expect(Hub.dispatch).toHaveBeenCalledWith( | ||
'auth', | ||
{ event: 'tokenRefresh' }, | ||
'Auth', | ||
AMPLIFY_SYMBOL | ||
); | ||
}); | ||
}); | ||
}); |