|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { Identity, ResourcesConfig } from '@aws-amplify/core'; |
| 5 | + |
| 6 | +import { DefaultIdentityIdStore } from '../../../../src/providers/cognito/credentialsProvider/IdentityIdStore'; |
| 7 | + |
| 8 | +const mockKeyValueStorage = { |
| 9 | + setItem: jest.fn(), |
| 10 | + getItem: jest.fn(), |
| 11 | + removeItem: jest.fn(), |
| 12 | + clear: jest.fn(), |
| 13 | +}; |
| 14 | + |
| 15 | +const validAuthConfig: ResourcesConfig = { |
| 16 | + Auth: { |
| 17 | + Cognito: { |
| 18 | + userPoolId: 'us-east-1_test-id', |
| 19 | + identityPoolId: 'us-east-1:test-id', |
| 20 | + userPoolClientId: 'test-id', |
| 21 | + allowGuestAccess: true, |
| 22 | + }, |
| 23 | + }, |
| 24 | +}; |
| 25 | +const validAuthKey = { |
| 26 | + identityId: `com.amplify.Cognito.${ |
| 27 | + validAuthConfig.Auth!.Cognito!.identityPoolId |
| 28 | + }.identityId`, |
| 29 | +}; |
| 30 | +const validGuestIdentityId: Identity = { type: 'guest', id: 'guest-id' }; |
| 31 | +const validPrimaryIdentityId: Identity = { type: 'primary', id: 'primary-id' }; |
| 32 | + |
| 33 | +const noIdentityPoolIdAuthConfig: ResourcesConfig = { |
| 34 | + Auth: { |
| 35 | + Cognito: { |
| 36 | + userPoolId: 'us-east-1_test-id', |
| 37 | + userPoolClientId: 'test-id', |
| 38 | + }, |
| 39 | + }, |
| 40 | +}; |
| 41 | + |
| 42 | +describe('DefaultIdentityIdStore', () => { |
| 43 | + const defaultIdStore = new DefaultIdentityIdStore(mockKeyValueStorage); |
| 44 | + beforeAll(() => { |
| 45 | + defaultIdStore.setAuthConfig(validAuthConfig.Auth!); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(() => { |
| 49 | + mockKeyValueStorage.setItem.mockClear(); |
| 50 | + mockKeyValueStorage.getItem.mockClear(); |
| 51 | + mockKeyValueStorage.removeItem.mockClear(); |
| 52 | + mockKeyValueStorage.clear.mockClear(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should set the Auth config required to form the storage keys', async () => { |
| 56 | + expect(defaultIdStore._authKeys).toEqual(validAuthKey); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should store guest identityId in keyValueStorage', async () => { |
| 60 | + defaultIdStore.storeIdentityId(validGuestIdentityId); |
| 61 | + expect(mockKeyValueStorage.setItem).toHaveBeenCalledWith( |
| 62 | + validAuthKey.identityId, |
| 63 | + validGuestIdentityId.id, |
| 64 | + ); |
| 65 | + expect(defaultIdStore._primaryIdentityId).toBeUndefined(); |
| 66 | + expect(defaultIdStore._hasGuestIdentityId).toBe(true); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should load guest identityId from keyValueStorage', async () => { |
| 70 | + mockKeyValueStorage.getItem.mockReturnValue(validGuestIdentityId.id); |
| 71 | + |
| 72 | + expect(await defaultIdStore.loadIdentityId()).toEqual(validGuestIdentityId); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should store primary identityId in keyValueStorage', async () => { |
| 76 | + defaultIdStore.storeIdentityId(validPrimaryIdentityId); |
| 77 | + expect(mockKeyValueStorage.removeItem).toHaveBeenCalledWith( |
| 78 | + validAuthKey.identityId, |
| 79 | + ); |
| 80 | + expect(defaultIdStore._primaryIdentityId).toEqual( |
| 81 | + validPrimaryIdentityId.id, |
| 82 | + ); |
| 83 | + expect(defaultIdStore._hasGuestIdentityId).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should load primary identityId from keyValueStorage', async () => { |
| 87 | + expect(await defaultIdStore.loadIdentityId()).toEqual( |
| 88 | + validPrimaryIdentityId, |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should clear the cached identityId', async () => { |
| 93 | + defaultIdStore.clearIdentityId(); |
| 94 | + expect(mockKeyValueStorage.removeItem).toHaveBeenCalledWith( |
| 95 | + validAuthKey.identityId, |
| 96 | + ); |
| 97 | + expect(defaultIdStore._primaryIdentityId).toBeUndefined(); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should throw when identityPoolId is not present while setting the auth config', async () => { |
| 101 | + expect(() => { |
| 102 | + defaultIdStore.setAuthConfig(noIdentityPoolIdAuthConfig.Auth!); |
| 103 | + }).toThrow('Invalid identity pool id provided.'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should return null when the underlying keyValueStorage method returns null', async () => { |
| 107 | + mockKeyValueStorage.getItem.mockReturnValue(null); |
| 108 | + expect(await defaultIdStore.loadIdentityId()).toBeNull(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return null when the underlying keyValueStorage method throws', async () => { |
| 112 | + mockKeyValueStorage.getItem.mockRejectedValue(new Error('Error')); |
| 113 | + expect(await defaultIdStore.loadIdentityId()).toBeNull(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should not call keyValueStorage.removeItem when there is no guest identityId to clear', async () => { |
| 117 | + const refreshIdentityIdStore = new DefaultIdentityIdStore( |
| 118 | + mockKeyValueStorage, |
| 119 | + ); |
| 120 | + refreshIdentityIdStore.setAuthConfig(validAuthConfig.Auth!); |
| 121 | + |
| 122 | + refreshIdentityIdStore.storeIdentityId(validPrimaryIdentityId); |
| 123 | + expect(mockKeyValueStorage.removeItem).not.toHaveBeenCalled(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments