diff --git a/__test__/support/utils/Random.ts b/__test__/support/utils/Random.ts index befb491ec..a1bf38df7 100644 --- a/__test__/support/utils/Random.ts +++ b/__test__/support/utils/Random.ts @@ -35,32 +35,4 @@ export default class Random { ), ); } - - public static getRandomUuid(): string { - let uuidStr = ''; - const crypto = - typeof window === 'undefined' - ? (global as any).crypto - : window.crypto || (window).msCrypto; - if (crypto) { - uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( - /[xy]/g, - function (c) { - const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0, - v = c == 'x' ? r : (r & 0x3) | 0x8; - return v.toString(16); - }, - ); - } else { - uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( - /[xy]/g, - function (c) { - const r = (Math.random() * 16) | 0, - v = c == 'x' ? r : (r & 0x3) | 0x8; - return v.toString(16); - }, - ); - } - return uuidStr; - } } diff --git a/__test__/unit/helpers/configHelper.test.ts b/__test__/unit/helpers/configHelper.test.ts index 6204415db..41bfb469c 100644 --- a/__test__/unit/helpers/configHelper.test.ts +++ b/__test__/unit/helpers/configHelper.test.ts @@ -2,7 +2,7 @@ import { AppUserConfig, ConfigIntegrationKind, } from '../../../src/shared/models/AppConfig'; -import Random from '../../support/utils/Random'; +import { getRandomUuid } from '../../../src/shared/utils/utils'; import { TestEnvironment } from '../../support/environment/TestEnvironment'; import { HttpHttpsEnvironment } from '../../support/models/HttpHttpsEnvironment'; import { getFinalAppConfig } from '../../support/helpers/configHelper'; @@ -24,7 +24,7 @@ describe('ConfigHelper Tests', () => { test('promptOptions 1 - autoRegister = true backwards compatibility for custom integration shows native on HTTPS', async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -46,7 +46,7 @@ describe('ConfigHelper Tests', () => { ); const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -62,7 +62,7 @@ describe('ConfigHelper Tests', () => { test('promptOptions 3 - autoRegister = false backwards compatibility for custom integration (no enabled prompts)', async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: false, }; @@ -78,7 +78,7 @@ describe('ConfigHelper Tests', () => { test(`promptOptions 4 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; (fakeUserConfig as any).promptOptions = { @@ -105,7 +105,7 @@ describe('ConfigHelper Tests', () => { ); const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -129,7 +129,7 @@ describe('ConfigHelper Tests', () => { test(`promptOptions 6 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -158,7 +158,7 @@ describe('ConfigHelper Tests', () => { ); const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -183,7 +183,7 @@ describe('ConfigHelper Tests', () => { test(`promptOptions 8 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -210,7 +210,7 @@ describe('ConfigHelper Tests', () => { test(`promptOptions 9 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -243,7 +243,7 @@ describe('ConfigHelper Tests', () => { ); const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -270,7 +270,7 @@ describe('ConfigHelper Tests', () => { test('autoResubscribe - autoRegister backwards compatibility for custom integration 1', () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: true, }; @@ -287,7 +287,7 @@ describe('ConfigHelper Tests', () => { test('autoResubscribe - autoRegister backwards compatibility for custom integration 2', () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), }; const fakeServerConfig = TestContext.getFakeServerAppConfig( @@ -305,7 +305,7 @@ describe('ConfigHelper Tests', () => { test('autoResubscribe - autoRegister backwards compatibility for custom integration 3', () => { const fakeUserConfig: AppUserConfig = { - appId: Random.getRandomUuid(), + appId: getRandomUuid(), autoRegister: false, autoResubscribe: true, }; diff --git a/src/shared/utils/OneSignalUtils.ts b/src/shared/utils/OneSignalUtils.ts index ecd2473e9..eff0a0266 100644 --- a/src/shared/utils/OneSignalUtils.ts +++ b/src/shared/utils/OneSignalUtils.ts @@ -99,30 +99,18 @@ export class OneSignalUtils { } public static getRandomUuid(): string { - let uuidStr = ''; const crypto = typeof window === 'undefined' ? (global as any).crypto : window.crypto || (window).msCrypto; - if (crypto) { - uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( - /[xy]/g, - function (c) { - const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0, - v = c == 'x' ? r : (r & 0x3) | 0x8; - return v.toString(16); - }, - ); - } else { - uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( - /[xy]/g, - function (c) { - const r = (Math.random() * 16) | 0, - v = c == 'x' ? r : (r & 0x3) | 0x8; - return v.toString(16); - }, - ); - } + const uuidStr = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace( + /[xy]/g, + function (c) { + const r = crypto.getRandomValues(new Uint8Array(1))[0] % 16 | 0, + v = c == 'x' ? r : (r & 0x3) | 0x8; + return v.toString(16); + }, + ); return uuidStr; }