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 c3277ad..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'; @@ -112,4 +113,5 @@ function setUpPage() { setConfig({ ...CONFIG, miloLibs }); await getRenewBanner(getConfig, loadBlock); await loadArea(); + 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'); + }); +});