From a29ebc5f94768f989ab78ea5442d0a0a4468814c Mon Sep 17 00:00:00 2001 From: Sonja Popovic Date: Thu, 12 Dec 2024 16:25:22 +0100 Subject: [PATCH 1/4] initial --- edsdme/scripts/scripts.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/edsdme/scripts/scripts.js b/edsdme/scripts/scripts.js index c3277ad..e54458a 100644 --- a/edsdme/scripts/scripts.js +++ b/edsdme/scripts/scripts.js @@ -10,6 +10,7 @@ import { enableGeoPopup, PARTNER_LOGIN_QUERY, } from './utils.js'; +import {getConfig} from "../blocks/utils/utils.js"; // Add project-wide style path here. const STYLES = '/edsdme/styles/styles.css'; @@ -101,6 +102,33 @@ function setUpPage() { updateFooter(CONFIG.locales); } +function rewriteLinks() { + const CBC_PROD = 'https://cbconnection.adobe.com'; + const CBC_STAGE = 'https://cbconnection.adobe.com'; + const PARTNERS_PROD = 'https://partners.adobe.com'; + const PARTNERS_STAGE = 'https://partners.stage.adobe.com'; + + const { env } = getConfig(); + let cbcLinks = document.querySelectorAll(`[href][href^="${CBC_PROD}"]`); + cbcLinks.forEach((link) => { + const linkPuzzle1 = env.name !== 'prod' ? CBC_STAGE : CBC_PROD; + const linkPuzzle2 = '/bin/fusion/modalImsLogin?resource='; + if (!link.href) return; + const linkPuzzle3 = link.href.split(CBC_PROD)[1]; + // if link is already correct + if (linkPuzzle3 === linkPuzzle2) return; + //check is it already /bin/fuzionn resource + link.href = linkPuzzle1 + linkPuzzle2 + linkPuzzle3; + }); + + if (env.name !== 'prod') { + const partnersLinks = document.querySelectorAll(`[href][href^="${PARTNERS_PROD}"]`); + partnersLinks.forEach(link => { + link.href = link.href.replace([PARTNERS_PROD, PARTNERS_STAGE]); + }) + } + +} (async function loadPage() { applyPagePersonalization(); setUpPage(); @@ -112,4 +140,6 @@ function setUpPage() { setConfig({ ...CONFIG, miloLibs }); await getRenewBanner(getConfig, loadBlock); await loadArea(); + rewriteLinks(); + }()); From 747c873c3c89141dbb5adf7f38497e83ad50df57 Mon Sep 17 00:00:00 2001 From: Sonja Popovic Date: Fri, 13 Dec 2024 16:00:12 +0100 Subject: [PATCH 2/4] MWPW-163801-link-rewriter --- edsdme/scripts/scripts.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/edsdme/scripts/scripts.js b/edsdme/scripts/scripts.js index e54458a..0cd4c01 100644 --- a/edsdme/scripts/scripts.js +++ b/edsdme/scripts/scripts.js @@ -104,6 +104,7 @@ function setUpPage() { function rewriteLinks() { const CBC_PROD = 'https://cbconnection.adobe.com'; + // todo update value for cbc stage after we got answer on mail const CBC_STAGE = 'https://cbconnection.adobe.com'; const PARTNERS_PROD = 'https://partners.adobe.com'; const PARTNERS_STAGE = 'https://partners.stage.adobe.com'; @@ -111,14 +112,11 @@ function rewriteLinks() { const { env } = getConfig(); let cbcLinks = document.querySelectorAll(`[href][href^="${CBC_PROD}"]`); cbcLinks.forEach((link) => { - const linkPuzzle1 = env.name !== 'prod' ? CBC_STAGE : CBC_PROD; - const linkPuzzle2 = '/bin/fusion/modalImsLogin?resource='; - if (!link.href) return; - const linkPuzzle3 = link.href.split(CBC_PROD)[1]; - // if link is already correct - if (linkPuzzle3 === linkPuzzle2) return; - //check is it already /bin/fuzionn resource - link.href = linkPuzzle1 + linkPuzzle2 + linkPuzzle3; + const domain = env.name !== 'prod' ? CBC_STAGE : CBC_PROD; + const loginPath = '/bin/fusion/modalImsLogin?resource='; + const resource = link.href.split(CBC_PROD)[1]; + if (resource.indexOf(loginPath) > 0) return; + link.href = domain + loginPath + resource; }); if (env.name !== 'prod') { @@ -127,8 +125,8 @@ function rewriteLinks() { link.href = link.href.replace([PARTNERS_PROD, PARTNERS_STAGE]); }) } - } + (async function loadPage() { applyPagePersonalization(); setUpPage(); @@ -141,5 +139,4 @@ function rewriteLinks() { await getRenewBanner(getConfig, loadBlock); await loadArea(); rewriteLinks(); - }()); From 0439c7b05cb6bf32a748118223360849693ae583 Mon Sep 17 00:00:00 2001 From: Sonja Popovic Date: Fri, 13 Dec 2024 16:20:34 +0100 Subject: [PATCH 3/4] MWPW-163801-link-rewriter --- edsdme/scripts/scripts.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/edsdme/scripts/scripts.js b/edsdme/scripts/scripts.js index 0cd4c01..ba18e11 100644 --- a/edsdme/scripts/scripts.js +++ b/edsdme/scripts/scripts.js @@ -10,7 +10,6 @@ import { enableGeoPopup, PARTNER_LOGIN_QUERY, } from './utils.js'; -import {getConfig} from "../blocks/utils/utils.js"; // Add project-wide style path here. const STYLES = '/edsdme/styles/styles.css'; @@ -102,7 +101,7 @@ function setUpPage() { updateFooter(CONFIG.locales); } -function rewriteLinks() { +function rewriteLinks(getConfig) { const CBC_PROD = 'https://cbconnection.adobe.com'; // todo update value for cbc stage after we got answer on mail const CBC_STAGE = 'https://cbconnection.adobe.com'; @@ -110,7 +109,7 @@ function rewriteLinks() { const PARTNERS_STAGE = 'https://partners.stage.adobe.com'; const { env } = getConfig(); - let cbcLinks = document.querySelectorAll(`[href][href^="${CBC_PROD}"]`); + const cbcLinks = document.querySelectorAll(`[href][href^="${CBC_PROD}"]`); cbcLinks.forEach((link) => { const domain = env.name !== 'prod' ? CBC_STAGE : CBC_PROD; const loginPath = '/bin/fusion/modalImsLogin?resource='; @@ -121,9 +120,9 @@ function rewriteLinks() { if (env.name !== 'prod') { const partnersLinks = document.querySelectorAll(`[href][href^="${PARTNERS_PROD}"]`); - partnersLinks.forEach(link => { + partnersLinks.forEach((link) => { link.href = link.href.replace([PARTNERS_PROD, PARTNERS_STAGE]); - }) + }); } } @@ -138,5 +137,5 @@ function rewriteLinks() { setConfig({ ...CONFIG, miloLibs }); await getRenewBanner(getConfig, loadBlock); await loadArea(); - rewriteLinks(); + rewriteLinks(getConfig); }()); From dd04e813a2ac9545134e6d90cc869810c6241bef Mon Sep 17 00:00:00 2001 From: Sonja Popovic Date: Mon, 16 Dec 2024 12:22:10 +0100 Subject: [PATCH 4/4] MWPW-163801-link-rewriter --- edsdme/scripts/rewriteLinks.js | 40 ++++++++++++++++++++++ edsdme/scripts/scripts.js | 28 ++------------- test/scripts/rewriteLinks.jest.js | 57 +++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 edsdme/scripts/rewriteLinks.js create mode 100644 test/scripts/rewriteLinks.jest.js diff --git a/edsdme/scripts/rewriteLinks.js b/edsdme/scripts/rewriteLinks.js new file mode 100644 index 0000000..1bed510 --- /dev/null +++ b/edsdme/scripts/rewriteLinks.js @@ -0,0 +1,40 @@ +import { getConfig } from '../blocks/utils/utils.js'; + +export function rewriteLinks() { + const environments = { + cbcProd: 'https://cbconnection.adobe.com', + cbcStage: 'https://cbconnection-stage.adobe.com', + partnersProd: 'https://partners.adobe.com', + partnersStage: 'https://partners.stage.adobe.com', + }; + + const { env } = getConfig(); + const isProd = env.name === 'prod'; + + const updateLinks = (currentDomain, newDomain, loginPath) => { + document.querySelectorAll(`[href^="${currentDomain}"]`).forEach((link) => { + let url; + try { + url = new URL(link.href); + } catch { + return; + } + url.hostname = new URL(newDomain).hostname; + if (loginPath && !url.pathname.includes(loginPath)) { + const resource = url.pathname; + url.searchParams.append('resource', resource); + url.pathname = loginPath; + } + link.href = url.toString(); + }); + }; + + // Update cbc links + const cbcDomain = isProd ? environments.cbcProd : environments.cbcStage; + updateLinks(environments.cbcProd, cbcDomain, '/bin/fusion/modalImsLogin'); + + // Update partners links if not prod + if (!isProd) { + updateLinks(environments.partnersProd, environments.partnersStage); + } +} diff --git a/edsdme/scripts/scripts.js b/edsdme/scripts/scripts.js index ba18e11..e3b4802 100644 --- a/edsdme/scripts/scripts.js +++ b/edsdme/scripts/scripts.js @@ -10,6 +10,7 @@ import { enableGeoPopup, PARTNER_LOGIN_QUERY, } from './utils.js'; +import { rewriteLinks } from './rewriteLinks.js'; // Add project-wide style path here. const STYLES = '/edsdme/styles/styles.css'; @@ -101,31 +102,6 @@ function setUpPage() { updateFooter(CONFIG.locales); } -function rewriteLinks(getConfig) { - const CBC_PROD = 'https://cbconnection.adobe.com'; - // todo update value for cbc stage after we got answer on mail - const CBC_STAGE = 'https://cbconnection.adobe.com'; - const PARTNERS_PROD = 'https://partners.adobe.com'; - const PARTNERS_STAGE = 'https://partners.stage.adobe.com'; - - const { env } = getConfig(); - const cbcLinks = document.querySelectorAll(`[href][href^="${CBC_PROD}"]`); - cbcLinks.forEach((link) => { - const domain = env.name !== 'prod' ? CBC_STAGE : CBC_PROD; - const loginPath = '/bin/fusion/modalImsLogin?resource='; - const resource = link.href.split(CBC_PROD)[1]; - if (resource.indexOf(loginPath) > 0) return; - link.href = domain + loginPath + resource; - }); - - if (env.name !== 'prod') { - const partnersLinks = document.querySelectorAll(`[href][href^="${PARTNERS_PROD}"]`); - partnersLinks.forEach((link) => { - link.href = link.href.replace([PARTNERS_PROD, PARTNERS_STAGE]); - }); - } -} - (async function loadPage() { applyPagePersonalization(); setUpPage(); @@ -137,5 +113,5 @@ function rewriteLinks(getConfig) { setConfig({ ...CONFIG, miloLibs }); await getRenewBanner(getConfig, loadBlock); await loadArea(); - rewriteLinks(getConfig); + rewriteLinks(); }()); diff --git a/test/scripts/rewriteLinks.jest.js b/test/scripts/rewriteLinks.jest.js new file mode 100644 index 0000000..c847db2 --- /dev/null +++ b/test/scripts/rewriteLinks.jest.js @@ -0,0 +1,57 @@ +/** + * @jest-environment jsdom + */ +import { rewriteLinks } from '../../edsdme/scripts/rewriteLinks.js'; +import { getConfig } from '../../edsdme/blocks/utils/utils.js'; + +jest.mock('../../edsdme/blocks/utils/utils.js', () => ({ getConfig: jest.fn() })); + +// Mock DOM +document.body.innerHTML = ` + cbc prod Link + Partner prod Link + cbc Login Link + +`; + +describe('Test rewrite links', () => { + beforeEach(() => { + getConfig.mockReturnValue({ env: { name: 'stage' } }); + }); + afterEach(() => { + jest.clearAllMocks(); // Clear mocks after each test + document.body.innerHTML = ` + cbc prod Link + Partner prod Link + cbc Login Link + +`; + }); + + test('should update prod links to cbc stage in non-prod, with resource query param and login path', () => { + rewriteLinks(); + const links = document.querySelectorAll('a'); + expect(links[0].href).toBe('https://cbconnection-stage.adobe.com/bin/fusion/modalImsLogin?resource=%2Fhome%2Fsearch'); + expect(links[3].href).toBe('https://cbconnection-stage.adobe.com/bin/fusion/modalImsLogin?resource=%2Fen%2Fnews%2Fenablement-news-partner-lock'); + }); + + test('should update only domain when login path is already there', () => { + rewriteLinks(); + const links = document.querySelectorAll('a'); + expect(links[2].href).toBe('https://cbconnection-stage.adobe.com/bin/fusion/modalImsLogin?resource=/home/search'); + }); + + test('should update partners prod link when on non prod', () => { + rewriteLinks(); + const links = document.querySelectorAll('a'); + expect(links[1].href).toBe('https://partners.stage.adobe.com/'); + }); + + test('should not update partners prod domain and cbc prod domain when on prod', () => { + getConfig.mockReturnValue({ env: { name: 'prod' } }); + rewriteLinks(); + const links = document.querySelectorAll('a'); + expect(links[1].href).toBe('https://partners.adobe.com/'); + expect(links[3].href).toBe('https://cbconnection.adobe.com/bin/fusion/modalImsLogin?resource=%2Fen%2Fnews%2Fenablement-news-partner-lock'); + }); +});