-
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.
fix: Prevent creating multiple endpoint ids (#13319)
- Loading branch information
Showing
6 changed files
with
152 additions
and
13 deletions.
There are no files selected for viewing
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
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
54 changes: 54 additions & 0 deletions
54
packages/core/__tests__/providers/pinpoint/utils/createEndpointId.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,54 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { | ||
clearCreatedEndpointId, | ||
createEndpointId, | ||
} from '../../../../src/providers/pinpoint/utils/createEndpointId'; | ||
import { amplifyUuid } from '../../../../src/utils/amplifyUuid'; | ||
import { appId, category, uuid } from '../testUtils/data'; | ||
|
||
jest.mock('../../../../src/utils/amplifyUuid'); | ||
|
||
describe('Pinpoint Provider Util: createEndpointId', () => { | ||
// assert mocks | ||
const mockAmplifyUuid = amplifyUuid as jest.Mock; | ||
|
||
afterEach(() => { | ||
mockAmplifyUuid.mockReset(); | ||
}); | ||
|
||
it('returns a new endpoint id for a category', () => { | ||
mockAmplifyUuid.mockReturnValue(uuid); | ||
|
||
expect(createEndpointId(appId, category)).toBe(uuid); | ||
expect(mockAmplifyUuid).toHaveBeenCalled(); | ||
}); | ||
|
||
it('returns the same endpoint id for a category', () => { | ||
const newUuid = `new-${uuid}`; | ||
mockAmplifyUuid.mockReturnValue(newUuid); | ||
|
||
expect(createEndpointId(appId, category)).toBe(uuid); | ||
expect(mockAmplifyUuid).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('returns a new endpoint id for a different category', () => { | ||
const newUuid = `new-${uuid}`; | ||
const newCategory = 'PushNotification'; | ||
mockAmplifyUuid.mockReturnValue(newUuid); | ||
|
||
expect(createEndpointId(appId, newCategory)).toBe(newUuid); | ||
expect(mockAmplifyUuid).toHaveBeenCalled(); | ||
}); | ||
|
||
describe('clearCreatedEndpointId()', () => { | ||
it('can create a new endpoint id for a category after clearing', () => { | ||
const newUuid = `new-${uuid}`; | ||
mockAmplifyUuid.mockReturnValue(newUuid); | ||
clearCreatedEndpointId(appId, category); | ||
|
||
expect(createEndpointId(appId, category)).toBe(newUuid); | ||
}); | ||
}); | ||
}); |
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
39 changes: 39 additions & 0 deletions
39
packages/core/src/providers/pinpoint/utils/createEndpointId.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,39 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { amplifyUuid } from '../../../utils/amplifyUuid'; | ||
import { SupportedCategory } from '../types'; | ||
|
||
import { getCacheKey } from './getCacheKey'; | ||
|
||
const createdEndpointIds: Record<string, string> = {}; | ||
|
||
/** | ||
* Creates an endpoint id and guarantees multiple creations for a category returns the same uuid. | ||
* | ||
* @internal | ||
*/ | ||
export const createEndpointId = ( | ||
appId: string, | ||
category: SupportedCategory, | ||
): string => { | ||
const cacheKey = getCacheKey(appId, category); | ||
if (!createdEndpointIds[cacheKey]) { | ||
createdEndpointIds[cacheKey] = amplifyUuid(); | ||
} | ||
|
||
return createdEndpointIds[cacheKey]; | ||
}; | ||
|
||
/** | ||
* Clears a created endpoint id for a category. | ||
* | ||
* @internal | ||
*/ | ||
export const clearCreatedEndpointId = ( | ||
appId: string, | ||
category: SupportedCategory, | ||
): void => { | ||
const cacheKey = getCacheKey(appId, category); | ||
delete createdEndpointIds[cacheKey]; | ||
}; |
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