Skip to content

Commit

Permalink
Merge pull request #1142 from OneSignal/refactor/rm_ostc_code
Browse files Browse the repository at this point in the history
Remove subdomain code (os.tc)
  • Loading branch information
jkasten2 authored Dec 13, 2023
2 parents 87cba75 + d869a8e commit b7121cd
Show file tree
Hide file tree
Showing 103 changed files with 228 additions and 6,136 deletions.
32 changes: 1 addition & 31 deletions __test__/support/environment/TestContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import { DelayedPromptType } from '../../../src/shared/models/Prompts';
import { APP_ID } from '../constants';
import deepmerge from 'deepmerge';
import ConfigManager from '../../../src/page/managers/ConfigManager';
import { HttpHttpsEnvironment } from '../models/HttpHttpsEnvironment';
import { TestEnvironmentConfig } from './TestEnvironment';

export default class TestContext {
static getFakeServerAppConfig(
configIntegrationKind: ConfigIntegrationKind,
isHttps = true,
overrideServerConfig: RecursivePartial<ServerAppConfig> | null = null,
appId: string = APP_ID,
): ServerAppConfig {
if (configIntegrationKind === ConfigIntegrationKind.Custom) {
const customConfigHttps: ServerAppConfig = {
return {
success: true,
version: 2,
app_id: appId,
Expand Down Expand Up @@ -172,31 +170,6 @@ export default class TestContext {
},
generated_at: 1531177265,
};
if (isHttps) {
return customConfigHttps;
}
return {
...customConfigHttps,
config: {
...customConfigHttps.config,
subdomain: 'helloworld123',
origin: 'http://localhost:3000',
siteInfo: {
name: 'localhost http',
origin: 'http://localhost:3000',
proxyOrigin: 'helloworld123',
defaultIconUrl: null,
proxyOriginEnabled: true,
},
welcomeNotification: {
enable: true,
url: 'http://localhost:3000/?_osp=do_not_open',
title: 'localhost http',
message: 'Thanks for subscribing!',
urlEnabled: false,
},
},
};
}

const remoteConfigMockDefaults: ServerAppConfig = {
Expand Down Expand Up @@ -495,9 +468,6 @@ export default class TestContext {
const fakeUserConfig = config.userConfig || this.getFakeAppUserConfig();
const fakeServerConfig = this.getFakeServerAppConfig(
config.integration || ConfigIntegrationKind.Custom,
config.httpOrHttps
? config.httpOrHttps === HttpHttpsEnvironment.Https
: undefined,
config.overrideServerConfig,
);
const configManager = new ConfigManager();
Expand Down
3 changes: 0 additions & 3 deletions __test__/support/environment/TestEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
stubNotification,
mockUserAgent,
} from './TestEnvironmentHelpers';
import { HttpHttpsEnvironment } from '../models/HttpHttpsEnvironment';
import OperationCache from '../../../src/core/caching/OperationCache';
import 'fake-indexeddb/auto';
import { RecursivePartial } from '../../../src/shared/context/Utils';
Expand All @@ -35,9 +34,7 @@ export interface TestEnvironmentConfig {
permission?: NotificationPermission;
addPrompts?: boolean;
url?: string;
initializeAsIframe?: boolean;
userAgent?: BrowserUserAgent;
httpOrHttps?: HttpHttpsEnvironment;
overrideServerConfig?: RecursivePartial<ServerAppConfig>;
integration?: ConfigIntegrationKind;
useMockIdentityModel?: boolean;
Expand Down
25 changes: 3 additions & 22 deletions __test__/support/environment/TestEnvironmentHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import OneSignal from '../../../src/onesignal/OneSignal';
import { CUSTOM_LINK_CSS_CLASSES } from '../../../src/shared/slidedown/constants';
import { getSlidedownElement } from '../../../src/page/slidedown/SlidedownElement';
import { MockServiceWorkerContainerWithAPIBan } from '../mocks/models/MockServiceWorkerContainerWithAPIBan';
import { HttpHttpsEnvironment } from '../models/HttpHttpsEnvironment';
import BrowserUserAgent from '../models/BrowserUserAgent';
import TestContext from './TestContext';
import { CoreModuleDirector } from '../../../src/core/CoreModuleDirector';
Expand Down Expand Up @@ -83,15 +82,7 @@ export async function stubDomEnvironment(config: TestEnvironmentConfig) {
config = {};
}

let url: string | undefined = undefined;
let isSecureContext: boolean | undefined = undefined;
if (config.httpOrHttps == HttpHttpsEnvironment.Http) {
url = 'http://localhost:3000/webpush/sandbox?http=1';
isSecureContext = false;
} else {
url = 'https://localhost:3001/webpush/sandbox?https=1';
isSecureContext = true;
}
let url = 'https://localhost:3001/webpush/sandbox?https=1';

if (config.url) {
url = config.url.toString();
Expand Down Expand Up @@ -129,24 +120,14 @@ export async function stubDomEnvironment(config: TestEnvironmentConfig) {
new MockServiceWorkerContainerWithAPIBan();
// (windowDef as any).TextEncoder = TextEncoder;
// (windowDef as any).TextDecoder = TextDecoder;
(windowDef as any).isSecureContext = isSecureContext;
(windowDef as any).location = url;

addCustomEventPolyfill(windowDef);

const windowTop: DOMWindow = config.initializeAsIframe
? ({
location: {
get origin() {
throw new Error(
"SecurityError: Permission denied to access property 'origin' on cross-origin object",
);
},
},
} as any)
: windowDef;
const windowTop: DOMWindow = windowDef;
dom.reconfigure({ url, windowTop });
global.window = windowDef;
global.window.isSecureContext = true;
global.document = windowDef.document;
return dom;
}
Expand Down
4 changes: 0 additions & 4 deletions __test__/support/models/HttpHttpsEnvironment.ts

This file was deleted.

2 changes: 0 additions & 2 deletions __test__/unit/core/modelRepo.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import OneSignalUtils from '../../../src/shared/utils/OneSignalUtils';
import ModelCache from '../../../src/core/caching/ModelCache';
import CoreModule from '../../../src/core/CoreModule';
import { CoreModuleDirector } from '../../../src/core/CoreModuleDirector';
Expand Down Expand Up @@ -27,7 +26,6 @@ jest.mock('../../../src/core/operationRepo/Operation');
describe('ModelRepo tests', () => {
beforeEach(async () => {
test.stub(ModelCache.prototype, 'load', Promise.resolve({}));
test.stub(OneSignalUtils, 'isUsingSubscriptionWorkaround', () => false);
jest.useFakeTimers();
core = new CoreModule();
coreDirector = new CoreModuleDirector(core);
Expand Down
122 changes: 1 addition & 121 deletions __test__/unit/helpers/configHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import {
} from '../../../src/shared/models/AppConfig';
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';
import { ConfigHelper } from '../../../src/shared/helpers/ConfigHelper';
import OneSignalUtils from '../../../src/shared/utils/OneSignalUtils';
import TestContext from '../../support/environment/TestContext';

describe('ConfigHelper Tests', () => {
beforeEach(async () => {
await TestEnvironment.initialize({
httpOrHttps: HttpHttpsEnvironment.Https,
});
await TestEnvironment.initialize();
test.stub(ConfigHelper, 'checkRestrictedOrigin');
});

Expand All @@ -38,28 +34,6 @@ describe('ConfigHelper Tests', () => {
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test('promptOptions 2 - autoRegister = true backwards compatibility for custom integration (shows slidedown on HTTP)', async () => {
test.stub(
OneSignalUtils,
'internalIsUsingSubscriptionWorkaround',
Promise.resolve(true),
);

const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
autoRegister: true,
};

const appConfig = await getFinalAppConfig(fakeUserConfig);
const finalPromptOptions = appConfig.userConfig.promptOptions;

expect(finalPromptOptions?.native?.enabled).toBe(false);
expect(finalPromptOptions?.native?.autoPrompt).toBe(false);

expect(finalPromptOptions?.slidedown?.prompts[0].autoPrompt).toBe(true);
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test('promptOptions 3 - autoRegister = false backwards compatibility for custom integration (no enabled prompts)', async () => {
const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
Expand Down Expand Up @@ -97,36 +71,6 @@ describe('ConfigHelper Tests', () => {
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test(`promptOptions 5 - autoRegister backwards compatibility for custom integration (ignores config, shows slidedown on HTTP)`, async () => {
const internalIsUsingSubscriptionWorkaround = test.stub(
OneSignalUtils,
'internalIsUsingSubscriptionWorkaround',
Promise.resolve(true),
);

const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
autoRegister: true,
};

(fakeUserConfig as any).promptOptions = {
slidedown: {
enabled: true,
},
};

const appConfig = await getFinalAppConfig(fakeUserConfig);
const finalPromptOptions = appConfig.userConfig.promptOptions;

expect(finalPromptOptions?.native?.enabled).toBe(false);
expect(finalPromptOptions?.native?.autoPrompt).toBe(false);

expect(finalPromptOptions?.slidedown?.prompts[0].autoPrompt).toBe(true);
expect(finalPromptOptions?.autoPrompt).toBe(true);

internalIsUsingSubscriptionWorkaround.mockRestore();
});

test(`promptOptions 6 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
Expand All @@ -150,37 +94,6 @@ describe('ConfigHelper Tests', () => {
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test(`promptOptions 7 - autoRegister = true backwards compatibility for custom integration (ignores config, shows slidedown on HTTP)`, async () => {
const internalIsUsingSubscriptionWorkaround = test.stub(
OneSignalUtils,
'internalIsUsingSubscriptionWorkaround',
Promise.resolve(true),
);

const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
autoRegister: true,
};

(fakeUserConfig as any).promptOptions = {
slidedown: {
enabled: true,
autoPrompt: false,
},
};

const appConfig = await getFinalAppConfig(fakeUserConfig);
const finalPromptOptions = appConfig.userConfig.promptOptions;

expect(finalPromptOptions?.native?.enabled).toBe(false);
expect(finalPromptOptions?.native?.autoPrompt).toBe(false);

expect(finalPromptOptions?.slidedown?.prompts[0].autoPrompt).toBe(true);
expect(finalPromptOptions?.autoPrompt).toBe(true);

internalIsUsingSubscriptionWorkaround.mockRestore();
});

test(`promptOptions 8 - autoRegister = true backwards compatibility for custom integration (ignores config, shows native on HTTPS)`, async () => {
const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
Expand Down Expand Up @@ -235,39 +148,6 @@ describe('ConfigHelper Tests', () => {
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test(`promptOptions 10 - autoRegister backwards compatibility for custom integration (ignores config, shows slidedown on HTTP)`, async () => {
test.stub(
OneSignalUtils,
'internalIsUsingSubscriptionWorkaround',
Promise.resolve(true),
);

const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
autoRegister: true,
};

(fakeUserConfig as any).promptOptions = {
native: {
enabled: true,
autoPrompt: false,
},
slidedown: {
enabled: true,
autoPrompt: false,
},
};

const appConfig = await getFinalAppConfig(fakeUserConfig);
const finalPromptOptions = appConfig.userConfig.promptOptions;

expect(finalPromptOptions?.native?.enabled).toBe(false);
expect(finalPromptOptions?.native?.autoPrompt).toBe(false);

expect(finalPromptOptions?.slidedown?.prompts[0].autoPrompt).toBe(true);
expect(finalPromptOptions?.autoPrompt).toBe(true);
});

test('autoResubscribe - autoRegister backwards compatibility for custom integration 1', () => {
const fakeUserConfig: AppUserConfig = {
appId: getRandomUuid(),
Expand Down
33 changes: 0 additions & 33 deletions __test__/unit/helpers/initHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import OneSignalUtils from '../../../src/shared/utils/OneSignalUtils';
import InitHelper from '../../../src/shared/helpers/InitHelper';
import { TestEnvironment } from '../../support/environment/TestEnvironment';
import { MessageChannel } from 'worker_threads';
Expand All @@ -13,28 +12,6 @@ describe('InitHelper', () => {
jest.restoreAllMocks();
});

/** registerForPushNotifications */
test('registerForPushNotifications: requesting a modal prompt', async () => {
await InitHelper.registerForPushNotifications({ modalPrompt: true });

expect(OneSignal.subscriptionModalHost).not.toBeUndefined();
expect(OneSignal.subscriptionModalHost.modal).not.toBeUndefined();
});

test('registerForPushNotifications: load fullscreen popup when using subscription workaround', async () => {
const utilsStub = jest
.spyOn(OneSignalUtils, 'isUsingSubscriptionWorkaround')
.mockReturnValue(true);
const loadStub = jest
.spyOn(InitHelper, 'loadSubscriptionPopup')
.mockResolvedValue(undefined);

await InitHelper.registerForPushNotifications();

expect(utilsStub).toHaveBeenCalledTimes(1);
expect(loadStub).toHaveBeenCalledTimes(1);
});

/** onSdkInitialized */
test('onSdkInitialized: ensure public sdk initialized triggered', async () => {
OneSignal.emitter.on(OneSignal.EVENTS.SDK_INITIALIZED_PUBLIC, () => {
Expand All @@ -58,11 +35,6 @@ describe('InitHelper', () => {
const spy = jest
.spyOn(OneSignal.context.updateManager, 'sendOnSessionUpdate')
.mockResolvedValue(undefined);
test.stub(
OneSignalUtils,
'isUsingSubscriptionWorkaround',
Promise.resolve(false),
);

OneSignal.config.userConfig.promptOptions.autoPrompt = false;
OneSignal.config.userConfig.autoResubscribe = false;
Expand All @@ -76,11 +48,6 @@ describe('InitHelper', () => {
const spy = jest
.spyOn(OneSignal.context.updateManager, 'sendOnSessionUpdate')
.mockResolvedValue(undefined);
test.stub(
OneSignalUtils,
'isUsingSubscriptionWorkaround',
Promise.resolve(false),
);

OneSignal.config.userConfig.promptOptions.autoPrompt = true;
OneSignal.config.userConfig.autoResubscribe = true;
Expand Down
5 changes: 1 addition & 4 deletions __test__/unit/helpers/mainHelper.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import MainHelper from '../../../src/shared/helpers/MainHelper';
import { SubscriptionStateKind } from '../../../src/shared/models/SubscriptionStateKind';
import { HttpHttpsEnvironment } from '../../support/models/HttpHttpsEnvironment';
import { TestEnvironment } from '../../support/environment/TestEnvironment';
import { NotificationPermission } from '../../../src/shared/models/NotificationPermission';

describe('MainHelper Tests', () => {
beforeEach(async () => {
jest.useFakeTimers();
await TestEnvironment.initialize({
httpOrHttps: HttpHttpsEnvironment.Https,
});
await TestEnvironment.initialize();
});

afterEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion __test__/unit/notifications/eventListeners.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import EventHelper from '../../../src/shared/helpers/EventHelper';

describe('Notification Events', () => {
beforeEach(async () => {
TestEnvironment.initialize();
await TestEnvironment.initialize();
});

afterEach(() => {
Expand Down
Loading

0 comments on commit b7121cd

Please sign in to comment.