From 0612046ed24f6c01457299d6d8f0125ba365db3b Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 11:24:06 +0200 Subject: [PATCH 1/8] chore: testing endpoint --- src/features/Endpoint/Endpoint.tsx | 12 ++++------ src/utils/__tests__/utils.test.ts | 16 ++++++------- src/utils/index.ts | 38 +++++++++++++++--------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/features/Endpoint/Endpoint.tsx b/src/features/Endpoint/Endpoint.tsx index d78d43b3..95509bd3 100644 --- a/src/features/Endpoint/Endpoint.tsx +++ b/src/features/Endpoint/Endpoint.tsx @@ -27,9 +27,12 @@ const EndPoint = () => { }, }); + const refreshWhenSubmitted = () => window.location.reload(); + const onSubmit = (data: IEndpointFormValues) => { localStorage.setItem('config.app_id', data.app_id); localStorage.setItem('config.server_url', data.server_url); + refreshWhenSubmitted(); }; const onResetClicked = () => { @@ -38,8 +41,6 @@ const EndPoint = () => { window.location.reload(); }; - const refreshWhenSubmitted = () => window.location.reload(); - const server_url = localStorage.getItem('config.server_url') ?? default_endpoint.server_url; const app_id = localStorage.getItem('config.app_id') ?? default_endpoint.app_id; const current_url = `wss://${server_url}/websockets/v3?app_id=${app_id}&l=EN&brand=deriv`; @@ -107,12 +108,7 @@ const EndPoint = () => {
{current_url}
- diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index ceb92803..ff9920ec 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -157,14 +157,14 @@ describe('Get Server Config', () => { jest.clearAllMocks(); }); - describe('Given we are in SSR ( no browser object ) ', () => { - it('Should return default ws server url and vercel deployment appId', () => { - jest.spyOn(utils, 'getIsBrowser').mockReturnValueOnce(false); - const serverConfig = getServerConfig(); - expect(serverConfig.appId).toEqual(VERCEL_DEPLOYMENT_APP_ID); - expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER); - }); - }); + // describe('Given we are in SSR ( no browser object ) ', () => { + // it('Should return default ws server url and vercel deployment appId', () => { + // jest.spyOn(utils, 'getIsBrowser').mockReturnValueOnce(false); + // const serverConfig = getServerConfig(); + // expect(serverConfig.appId).toEqual(VERCEL_DEPLOYMENT_APP_ID); + // expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER); + // }); + // }); describe('Given we are in Browser', () => { jest.spyOn(utils, 'getIsBrowser').mockReturnValue(true); diff --git a/src/utils/index.ts b/src/utils/index.ts index 8f0e45d0..799fcbb0 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -123,31 +123,31 @@ export const formatTokenScope = (tokenScope: string) => { }; export const getServerConfig = () => { - const isBrowser = getIsBrowser(); - if (isBrowser) { - const config_server_url = localStorage.getItem('config.server_url'); - const config_app_id = localStorage.getItem('config.app_id'); - if (config_app_id && config_server_url) { - return { - serverUrl: config_server_url, - appId: config_app_id, - oauth: config_server_url, - }; - } else { - const isLocalHost = isHost('localhost'); - return { - serverUrl: DEFAULT_WS_SERVER, - appId: getAppId(isLocalHost), - oauth: OAUTH_URL, - }; - } + // const isBrowser = getIsBrowser(); + // if (isBrowser) { + const config_server_url = localStorage.getItem('config.server_url'); + const config_app_id = localStorage.getItem('config.app_id'); + if (config_app_id && config_server_url) { + return { + serverUrl: config_server_url, + appId: config_app_id, + oauth: config_server_url, + }; } else { + const isLocalHost = isHost('localhost'); return { serverUrl: DEFAULT_WS_SERVER, - appId: getAppId(false), + appId: getAppId(isLocalHost), oauth: OAUTH_URL, }; } + // } else { + // return { + // serverUrl: DEFAULT_WS_SERVER, + // appId: getAppId(false), + // oauth: OAUTH_URL, + // }; + // } }; export const generateLoginUrl = (language: string, serverUrl: string, appId: string) => { From cdacf4ab8544423f6547b6e6d49d12bbc12c4652 Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 11:48:22 +0200 Subject: [PATCH 2/8] chore: removing some code --- src/features/Endpoint/Endpoint.tsx | 2 +- src/utils/__tests__/utils.test.ts | 8 -------- src/utils/index.ts | 9 --------- 3 files changed, 1 insertion(+), 18 deletions(-) diff --git a/src/features/Endpoint/Endpoint.tsx b/src/features/Endpoint/Endpoint.tsx index 95509bd3..90c38dae 100644 --- a/src/features/Endpoint/Endpoint.tsx +++ b/src/features/Endpoint/Endpoint.tsx @@ -38,7 +38,7 @@ const EndPoint = () => { const onResetClicked = () => { localStorage.removeItem('config.app_id'); localStorage.removeItem('config.server_url'); - window.location.reload(); + refreshWhenSubmitted(); }; const server_url = localStorage.getItem('config.server_url') ?? default_endpoint.server_url; diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index ff9920ec..d6b5cd14 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -157,14 +157,6 @@ describe('Get Server Config', () => { jest.clearAllMocks(); }); - // describe('Given we are in SSR ( no browser object ) ', () => { - // it('Should return default ws server url and vercel deployment appId', () => { - // jest.spyOn(utils, 'getIsBrowser').mockReturnValueOnce(false); - // const serverConfig = getServerConfig(); - // expect(serverConfig.appId).toEqual(VERCEL_DEPLOYMENT_APP_ID); - // expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER); - // }); - // }); describe('Given we are in Browser', () => { jest.spyOn(utils, 'getIsBrowser').mockReturnValue(true); diff --git a/src/utils/index.ts b/src/utils/index.ts index 799fcbb0..5085938f 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -123,8 +123,6 @@ export const formatTokenScope = (tokenScope: string) => { }; export const getServerConfig = () => { - // const isBrowser = getIsBrowser(); - // if (isBrowser) { const config_server_url = localStorage.getItem('config.server_url'); const config_app_id = localStorage.getItem('config.app_id'); if (config_app_id && config_server_url) { @@ -141,13 +139,6 @@ export const getServerConfig = () => { oauth: OAUTH_URL, }; } - // } else { - // return { - // serverUrl: DEFAULT_WS_SERVER, - // appId: getAppId(false), - // oauth: OAUTH_URL, - // }; - // } }; export const generateLoginUrl = (language: string, serverUrl: string, appId: string) => { From 0467b2fe20448079381550f0192007a386db408e Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 11:54:44 +0200 Subject: [PATCH 3/8] chore: refactoring code --- src/features/Endpoint/Endpoint.tsx | 2 +- src/features/Endpoint/__tests__/Endpoint.test.tsx | 2 +- src/utils/__tests__/utils.test.ts | 9 +++++---- src/utils/index.ts | 7 +++---- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/features/Endpoint/Endpoint.tsx b/src/features/Endpoint/Endpoint.tsx index 90c38dae..23d82bc0 100644 --- a/src/features/Endpoint/Endpoint.tsx +++ b/src/features/Endpoint/Endpoint.tsx @@ -11,7 +11,7 @@ interface IEndpointFormValues { } const EndPoint = () => { const default_endpoint = { - app_id: getAppId(false), + app_id: getAppId(), server_url: OAUTH_URL, }; diff --git a/src/features/Endpoint/__tests__/Endpoint.test.tsx b/src/features/Endpoint/__tests__/Endpoint.test.tsx index d374bd65..47cd2fa7 100644 --- a/src/features/Endpoint/__tests__/Endpoint.test.tsx +++ b/src/features/Endpoint/__tests__/Endpoint.test.tsx @@ -25,7 +25,7 @@ describe('Endpoint', () => { const app_id = screen.getByPlaceholderText('e.g. 9999'); expect(server).toHaveValue('oauth.deriv.com'); - expect(app_id).toHaveValue('35073'); + expect(app_id).toHaveValue('35074'); }); it('validate user inputs, and provides error messages for app id field', async () => { diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index d6b5cd14..73c4653e 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -34,22 +34,23 @@ describe('Get an object with currency data', () => { describe('Get App ID', () => { it("Should return 35074 when it's called in localhost environment", () => { - const appId = getAppId(true); + window.location.hostname = 'localhost'; + const appId = getAppId(); expect(appId).toBe('35074'); }); it("Should return 35073 when it's called in vercel environment", () => { window.location.hostname = 'deriv-api-docs.binary.sx'; - const appId = getAppId(false); + const appId = getAppId(); expect(appId).toBe('35073'); }); it("Should return 36545 when it's called in staging environment", () => { window.location.hostname = 'staging-api.deriv.com'; - const appId = getAppId(false); + const appId = getAppId(); expect(appId).toBe('36545'); }); it("Should return 36544 when it's called in production environment", () => { window.location.hostname = 'api.deriv.com'; - const appId = getAppId(false); + const appId = getAppId(); expect(appId).toBe('36544'); }); }); diff --git a/src/utils/index.ts b/src/utils/index.ts index 5085938f..2f83e99d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -59,10 +59,9 @@ export const isHost = (hostname: string) => { * @param isLocalHost {boolean} pass `true` if the project is running on localhost * @returns {string} proper appId for the project */ -export const getAppId = (isLocalHost: boolean) => { - if (isLocalHost) return LOCALHOST_APP_ID; - +export const getAppId = () => { // if not localhost, then one of the following: + if (isHost('localhost')) return LOCALHOST_APP_ID; if (isHost('staging-api.deriv.com')) return STAGING_APP_ID; if (isHost('deriv-api-docs.binary.sx')) return VERCEL_DEPLOYMENT_APP_ID; if (isHost('api.deriv.com')) return PRODUCTION_APP_ID; @@ -135,7 +134,7 @@ export const getServerConfig = () => { const isLocalHost = isHost('localhost'); return { serverUrl: DEFAULT_WS_SERVER, - appId: getAppId(isLocalHost), + appId: getAppId(), oauth: OAUTH_URL, }; } From 6d75bfc9b02e10ce876d74344cbf5f18c3ea7d5d Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 12:02:58 +0200 Subject: [PATCH 4/8] chore: few small refactors --- src/utils/__tests__/utils.test.ts | 7 +------ src/utils/index.ts | 2 -- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index 73c4653e..c8896bee 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -1,10 +1,5 @@ import * as utils from '@site/src/utils'; -import { - LOCALHOST_APP_ID, - VERCEL_DEPLOYMENT_APP_ID, - OAUTH_URL, - DEFAULT_WS_SERVER, -} from '../constants'; +import { LOCALHOST_APP_ID, DEFAULT_WS_SERVER } from '../constants'; const { getAccountsFromSearchParams, getAppId, diff --git a/src/utils/index.ts b/src/utils/index.ts index 2f83e99d..041d120e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -60,7 +60,6 @@ export const isHost = (hostname: string) => { * @returns {string} proper appId for the project */ export const getAppId = () => { - // if not localhost, then one of the following: if (isHost('localhost')) return LOCALHOST_APP_ID; if (isHost('staging-api.deriv.com')) return STAGING_APP_ID; if (isHost('deriv-api-docs.binary.sx')) return VERCEL_DEPLOYMENT_APP_ID; @@ -131,7 +130,6 @@ export const getServerConfig = () => { oauth: config_server_url, }; } else { - const isLocalHost = isHost('localhost'); return { serverUrl: DEFAULT_WS_SERVER, appId: getAppId(), From a212f0245f394c1745dd606107c75c7e3d0c3b5a Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 12:17:57 +0200 Subject: [PATCH 5/8] chore: adding some test coverage --- src/utils/__tests__/utils.test.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index c8896bee..f83953ff 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -28,6 +28,11 @@ describe('Get an object with currency data', () => { }); describe('Get App ID', () => { + it('By default it should return vercel staging ap id if hostname is not listed', () => { + window.location.hostname = 'asdfasdf'; + const appId = getAppId(); + expect(appId).toBe('35073'); + }); it("Should return 35074 when it's called in localhost environment", () => { window.location.hostname = 'localhost'; const appId = getAppId(); From 7f8df3bd88ce55fad2ba5f4f6c180dbba2635e0e Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 15:00:00 +0200 Subject: [PATCH 6/8] chore: refactoring code --- src/utils/index.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 041d120e..f5e63a62 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -121,13 +121,14 @@ export const formatTokenScope = (tokenScope: string) => { }; export const getServerConfig = () => { - const config_server_url = localStorage.getItem('config.server_url'); - const config_app_id = localStorage.getItem('config.app_id'); - if (config_app_id && config_server_url) { + const isBrowser = getIsBrowser(); + if (isBrowser) { + const config_server_url = localStorage.getItem('config.server_url'); + const config_app_id = localStorage.getItem('config.app_id'); return { - serverUrl: config_server_url, - appId: config_app_id, - oauth: config_server_url, + serverUrl: config_server_url ?? DEFAULT_WS_SERVER, + appId: config_app_id ?? getAppId(), + oauth: config_server_url ?? OAUTH_URL, }; } else { return { From 060f790739ed069d0d2482c7217bef8a9b9cd49f Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 15:07:54 +0200 Subject: [PATCH 7/8] chore: refactoring --- src/utils/__tests__/utils.test.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index f83953ff..0401e451 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -1,5 +1,5 @@ import * as utils from '@site/src/utils'; -import { LOCALHOST_APP_ID, DEFAULT_WS_SERVER } from '../constants'; +import { LOCALHOST_APP_ID, DEFAULT_WS_SERVER, VERCEL_DEPLOYMENT_APP_ID } from '../constants'; const { getAccountsFromSearchParams, getAppId, @@ -158,10 +158,18 @@ describe('Get Server Config', () => { jest.clearAllMocks(); }); + describe('Given we are in SSR ( no browser object ) ', () => { + it('Should return default ws server url and appId while SSR', () => { + jest.spyOn(utils, 'getIsBrowser').mockReturnValueOnce(false); + const serverConfig = getServerConfig(); + expect(serverConfig.appId).toEqual(LOCALHOST_APP_ID); + expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER); + }); + }); + describe('Given we are in Browser', () => { jest.spyOn(utils, 'getIsBrowser').mockReturnValue(true); - - it('Should return default ws server url and vercel deployment appId in LOCALHOST ', () => { + it('Should return default ws server url and localhost appId', () => { const serverConfig = getServerConfig(); expect(serverConfig.appId).toEqual(LOCALHOST_APP_ID); expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER); From 166e588e4cbc1218a981823e945aff3edfbeeaab Mon Sep 17 00:00:00 2001 From: Hubert Koster Date: Wed, 31 May 2023 16:17:25 +0200 Subject: [PATCH 8/8] chore: small refactors --- src/utils/index.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index f5e63a62..9bbebb02 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -56,7 +56,6 @@ export const isHost = (hostname: string) => { /** * @description based on the environment which the project is running we must use different appIds, to get the proper redirect url - * @param isLocalHost {boolean} pass `true` if the project is running on localhost * @returns {string} proper appId for the project */ export const getAppId = () => { @@ -125,6 +124,7 @@ export const getServerConfig = () => { if (isBrowser) { const config_server_url = localStorage.getItem('config.server_url'); const config_app_id = localStorage.getItem('config.app_id'); + return { serverUrl: config_server_url ?? DEFAULT_WS_SERVER, appId: config_app_id ?? getAppId(), @@ -139,8 +139,8 @@ export const getServerConfig = () => { } }; -export const generateLoginUrl = (language: string, serverUrl: string, appId: string) => { - return `https://${serverUrl}/oauth2/authorize?app_id=${appId}&l=${language}`; +export const generateLoginUrl = (language: string, oauthUrl: string, appId: string) => { + return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}`; }; interface IScopesLike {