|
| 1 | +import type { |
| 2 | + LDContext, |
| 3 | + LDContextCommon, |
| 4 | + LDMultiKindContext, |
| 5 | + LDUser, |
| 6 | +} from '@launchdarkly/js-sdk-common'; |
| 7 | +import { basicPlatform } from '@launchdarkly/private-js-mocks'; |
| 8 | + |
| 9 | +import ensureKey, { addNamespace, getOrGenerateKey } from './ensureKey'; |
| 10 | + |
| 11 | +const { crypto, storage } = basicPlatform; |
| 12 | +describe('ensureKey', () => { |
| 13 | + beforeEach(() => { |
| 14 | + crypto.randomUUID.mockReturnValueOnce('random1').mockReturnValueOnce('random2'); |
| 15 | + }); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.resetAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + test('addNamespace', async () => { |
| 22 | + const nsKey = addNamespace('org'); |
| 23 | + expect(nsKey).toEqual('LaunchDarkly_AnonKeys_org'); |
| 24 | + }); |
| 25 | + |
| 26 | + test('getOrGenerateKey create new key', async () => { |
| 27 | + const key = await getOrGenerateKey('org', basicPlatform); |
| 28 | + |
| 29 | + expect(key).toEqual('random1'); |
| 30 | + expect(crypto.randomUUID).toHaveBeenCalled(); |
| 31 | + expect(storage.get).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org'); |
| 32 | + expect(storage.set).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org', 'random1'); |
| 33 | + }); |
| 34 | + |
| 35 | + test('getOrGenerateKey existing key', async () => { |
| 36 | + storage.get.mockImplementation((nsKind: string) => |
| 37 | + nsKind === 'LaunchDarkly_AnonKeys_org' ? 'random1' : undefined, |
| 38 | + ); |
| 39 | + |
| 40 | + const key = await getOrGenerateKey('org', basicPlatform); |
| 41 | + |
| 42 | + expect(key).toEqual('random1'); |
| 43 | + expect(crypto.randomUUID).not.toHaveBeenCalled(); |
| 44 | + expect(storage.get).toHaveBeenCalledWith('LaunchDarkly_AnonKeys_org'); |
| 45 | + expect(storage.set).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + test('ensureKey should not override anonymous key if specified', async () => { |
| 49 | + const context: LDContext = { kind: 'org', anonymous: true, key: 'Testy Pizza' }; |
| 50 | + const c = await ensureKey(context, basicPlatform); |
| 51 | + |
| 52 | + expect(c.key).toEqual('Testy Pizza'); |
| 53 | + }); |
| 54 | + |
| 55 | + test('ensureKey non-anonymous single context should be unchanged', async () => { |
| 56 | + const context: LDContext = { kind: 'org', key: 'Testy Pizza' }; |
| 57 | + const c = await ensureKey(context, basicPlatform); |
| 58 | + |
| 59 | + expect(c.key).toEqual('Testy Pizza'); |
| 60 | + expect(c.anonymous).toBeFalsy(); |
| 61 | + }); |
| 62 | + |
| 63 | + test('ensureKey non-anonymous contexts in multi should be unchanged', async () => { |
| 64 | + const context: LDContext = { |
| 65 | + kind: 'multi', |
| 66 | + user: { key: 'userKey' }, |
| 67 | + org: { key: 'orgKey' }, |
| 68 | + }; |
| 69 | + |
| 70 | + const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; |
| 71 | + |
| 72 | + expect((c.user as LDContextCommon).key).toEqual('userKey'); |
| 73 | + expect((c.org as LDContextCommon).key).toEqual('orgKey'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('ensureKey should create key for single anonymous context', async () => { |
| 77 | + const context: LDContext = { kind: 'org', anonymous: true, key: '' }; |
| 78 | + const c = await ensureKey(context, basicPlatform); |
| 79 | + expect(c.key).toEqual('random1'); |
| 80 | + }); |
| 81 | + |
| 82 | + test('ensureKey should create key for an anonymous context in multi', async () => { |
| 83 | + const context: LDContext = { |
| 84 | + kind: 'multi', |
| 85 | + user: { anonymous: true, key: '' }, |
| 86 | + org: { key: 'orgKey' }, |
| 87 | + }; |
| 88 | + |
| 89 | + const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; |
| 90 | + |
| 91 | + expect((c.user as LDContextCommon).key).toEqual('random1'); |
| 92 | + expect((c.org as LDContextCommon).key).toEqual('orgKey'); |
| 93 | + }); |
| 94 | + |
| 95 | + test('ensureKey should create key for all anonymous contexts in multi', async () => { |
| 96 | + const context: LDContext = { |
| 97 | + kind: 'multi', |
| 98 | + user: { anonymous: true, key: '' }, |
| 99 | + org: { anonymous: true, key: '' }, |
| 100 | + }; |
| 101 | + |
| 102 | + const c = (await ensureKey(context, basicPlatform)) as LDMultiKindContext; |
| 103 | + |
| 104 | + expect((c.user as LDContextCommon).key).toEqual('random1'); |
| 105 | + expect((c.org as LDContextCommon).key).toEqual('random2'); |
| 106 | + }); |
| 107 | + |
| 108 | + test('ensureKey should create key for anonymous legacy user', async () => { |
| 109 | + const context: LDUser = { |
| 110 | + anonymous: true, |
| 111 | + key: '', |
| 112 | + }; |
| 113 | + const c = await ensureKey(context, basicPlatform); |
| 114 | + expect(c.key).toEqual('random1'); |
| 115 | + }); |
| 116 | +}); |
0 commit comments