Skip to content

Commit a0d1447

Browse files
authored
fix(auth): remove redundant remove guest identityId call (#13789)
1 parent c586b7b commit a0d1447

File tree

3 files changed

+135
-98
lines changed

3 files changed

+135
-98
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
});

packages/auth/__tests__/providers/cognito/identityIdStore.test.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

packages/auth/src/providers/cognito/credentialsProvider/IdentityIdStore.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export class DefaultIdentityIdStore implements IdentityIdStore {
2323
// Used as in-memory storage
2424
_primaryIdentityId: string | undefined;
2525
_authKeys: AuthKeys<string> = {};
26+
_hasGuestIdentityId = false;
27+
2628
setAuthConfig(authConfigParam: AuthConfig) {
2729
assertIdentityPoolIdConfig(authConfigParam.Cognito);
2830
this.authConfig = authConfigParam;
@@ -48,7 +50,10 @@ export class DefaultIdentityIdStore implements IdentityIdStore {
4850
const storedIdentityId = await this.keyValueStorage.getItem(
4951
this._authKeys.identityId,
5052
);
53+
5154
if (storedIdentityId) {
55+
this._hasGuestIdentityId = true;
56+
5257
return {
5358
id: storedIdentityId,
5459
type: 'guest',
@@ -71,10 +76,14 @@ export class DefaultIdentityIdStore implements IdentityIdStore {
7176
this.keyValueStorage.setItem(this._authKeys.identityId, identity.id);
7277
// Clear in-memory storage of primary identityId
7378
this._primaryIdentityId = undefined;
79+
this._hasGuestIdentityId = true;
7480
} else {
7581
this._primaryIdentityId = identity.id;
7682
// Clear locally stored guest id
77-
this.keyValueStorage.removeItem(this._authKeys.identityId);
83+
if (this._hasGuestIdentityId) {
84+
this.keyValueStorage.removeItem(this._authKeys.identityId);
85+
this._hasGuestIdentityId = false;
86+
}
7887
}
7988
}
8089

0 commit comments

Comments
 (0)