diff --git a/packages/auth/__tests__/providers/cognito/identityIdStore.test.ts b/packages/auth/__tests__/providers/cognito/identityIdStore.test.ts new file mode 100644 index 00000000000..3a9fa1ab049 --- /dev/null +++ b/packages/auth/__tests__/providers/cognito/identityIdStore.test.ts @@ -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'); + } + }); + }); +}); diff --git a/packages/auth/__tests__/providers/cognito/testUtils/authApiTestParams.ts b/packages/auth/__tests__/providers/cognito/testUtils/authApiTestParams.ts index bd91ce15a63..f59dcf04153 100644 --- a/packages/auth/__tests__/providers/cognito/testUtils/authApiTestParams.ts +++ b/packages/auth/__tests__/providers/cognito/testUtils/authApiTestParams.ts @@ -148,25 +148,34 @@ export const authAPITestParams = { // Test values ValidAuthTokens: { idToken: decodeJWT( - 'eyJraWQiOiIyd1FTbElUQ2N0bWVMdTYwY3hzRFJPOW9DXC93eDZDdVMzT2lQbHRJRldYVT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIzOGEwODU1Ny1hMTFkLTQzYjEtYjc5Yi03ZTNjNDE2YWUzYzciLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMi5hbWF6b25hd3MuY29tXC91cy1lYXN0LTJfUTRpaTdlZFRJIiwiY29nb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ZSI6IkpvaG4gRG9lIHRoZSBzZWNvbmQiLCJpYXQiOjE1MTYyMzkwMjIsImlzcyI6Imh0dHBzOi8vdGVzdC5jb20iLCJleHAiOjE3MTAyOTMxMzB9.kpvsHfKH4JvCecECmb26Pl6HaedVX7PNiiF_8AlAbYc' + ), + accessTokenExpAt: Date.UTC(2023, 8, 24, 18, 55), + clockDrift: undefined, + metadata: undefined, + }, + ExpiredAuthTokens: { + idToken: decodeJWT( + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE1MTAyOTMxMzB9.1o9dQV9035dCO0nKDgZ-MwFf22Ptmysymt2ENyR5Mko' + ), + accessToken: decodeJWT( + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJleHAiOjE1MTAyOTMxMzB9.1o9dQV9035dCO0nKDgZ-MwFf22Ptmysymt2ENyR5Mko' ), accessTokenExpAt: Date.UTC(2023, 8, 24, 18, 55), - clockDrift: undefined, metadata: undefined, }, NewValidAuthTokens: { idToken: decodeJWT( - 'yJraWQiOiIyd1FTbElUQ2N0bWVMdTYwY3hzRFJPOW9DXC93eDZDdVMzT2lQbHRJRldYVT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIzOGEwODU1Ny1hMTFkLTQzYjEtYjc5Yi03ZTNjNDE2YWUzYzciLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMi5hbWF6b25hd3MuY29tXC91cy1lYXN0LTJfUTRpaTdlZFRJIiwiY29nbml0bzp1c2VybmFtZSI6InRlc3QyIiwib3JpZ2luX2p0aSI6ImRiM2QxOGE1LTViZ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VzdC5jb20ifQ.5eGzqDYCAYmagLpVDc1kqRT1da1wPu0_1FAg6ZNAuj8' ), accessTokenExpAt: Date.UTC(2023, 8, 24, 18, 55), - clockDrift: undefined, metadata: undefined, }, @@ -175,15 +184,15 @@ export const authAPITestParams = { withValidAuthToken: { IdentityId: 'identity-id-test', Logins: { - 'cognito-idp.us-east-2.amazonaws.com/us-east-2_Q4ii7edTI': - 'eyJraWQiOiIyd1FTbElUQ2N0bWVMdTYwY3hzRFJPOW9DXC93eDZDdVMzT2lQbHRJRldYVT0iLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIzOGEwODU1Ny1hMTFkLTQzYjEtYjc5Yi03ZTNjNDE2YWUzYzciLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaXNzIjoiaHR0cHM6XC9cL2NvZ25pdG8taWRwLnVzLWVhc3QtMi5hbWF6b25hd3MuY29tXC91cy1lYXN0LTJfUTRpaTdlZFRJIiwiY29nbml0bzp1c2VybmFtZSI6InRlc3QyIiwib3JpZ2luX2p0aSI6ImRiM2QxOGE1LTViZTAtNDVmOS05Y2RjLTI3OWQyMmJmNzgxZCIsImF1ZCI6IjZ1bG1sYTc0Y245cDlhdmEwZmcxYnV1cjhxIiwiZXZlbnRfaWQiOiJhZjRjMmM5NC04ZTY0LTRkYWYtYjc5ZS02NTE0NTEyMjE3OTAiLCJ0b2tlbl91c2UiOiJpZCIsImF1dGhfdGltZSI6MTY5MDkzMjM0MCwiZXhwIjoxNjkwOTM1OTQwLCJpYXQiOjE2OTA5MzIzNDAsImp0aSI6ImVhM2JmNmNlLWEyZWUtNGJiMC05MjdkLWNjMzRjYzRhMWVjMiIsImVtYWlsIjoiYW16bm1hbm9qQGdtYWlsLmNvbSJ9.i71wkSBPZt8BlBFMZPILJ6RsfDaJx0xqriD9y6ly3LnNB2vNAIOZqPLcCKEi8u0obyoFIK_EY7jKVRva5wbDDcHGt5YrnjT3SsWc1FGVUhrPW6IzEwbfYkUsbVGYjfO1hqTM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sImF1dGhfdGltZSI6MTY5MDkzMjM0MCwiZXhwIjoxNjkwOTM1OTQwLCJpYXQiOjE2OTA5MzIzNDAsImp0aSI6ImVhM2JmNmNlLWEyZWUtNGJiMC05MjdkLWNjMzRjYzRhMWVjMiIsImVtYWlsIjoiYW16bm1hbm9qQGdtYWlsLmNvbSJ9.i71wkSBPZt8BlBFMZPILJ6RsfDaJx0xqriD9y6ly3LnNB2vNAIOZqPLcCKEi8u0obyoFIK_EY7jKVRva5wbDDcHGt5YrnjT3SsWc1FGVUhrPW6IzEwbfYkUsbVGYjfO1hqTMW7q3FHvJ4yFjLDIUHQe-1_NogYeuhjrNxEupOPmE5-52N4dRriZ0DlHD4fe7gqL8B6AJXr5np1XaxZySU4KpdePwIp1Nb2fkolMEGHvOANHqWdBe5I0vRhAh0MDJ6IxvEr65tnaJNgVQuQaZFR4kQlpjemvB7kaVQ-SpH-tV_zXzqpwr_OEH6dgGMcxIsFrBFC8AGQnGXlSsS-5ThQ', + 'test.com': + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIHRoZSBzZWNvbmQiLCJpYXQiOjE1MTYyMzkwMjIsImlzcyI6Imh0dHBzOi8vdGVzdC5jb20iLCJleHAiOjE3MTAyOTMxMzB9.kpvsHfKH4JvCecECmb26Pl6HaedVX7PNiiF_8AlAbYc', }, }, }, diff --git a/packages/auth/__tests__/providers/cognito/tokenOrchestrator.test.ts b/packages/auth/__tests__/providers/cognito/tokenOrchestrator.test.ts new file mode 100644 index 00000000000..70d08deca59 --- /dev/null +++ b/packages/auth/__tests__/providers/cognito/tokenOrchestrator.test.ts @@ -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 + ); + }); + }); +});