Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mwpw 163801 rewrite links #166

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions edsdme/scripts/rewriteLinks.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 2 additions & 0 deletions edsdme/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -112,4 +113,5 @@ function setUpPage() {
setConfig({ ...CONFIG, miloLibs });
await getRenewBanner(getConfig, loadBlock);
await loadArea();
rewriteLinks();
}());
57 changes: 57 additions & 0 deletions test/scripts/rewriteLinks.jest.js
Original file line number Diff line number Diff line change
@@ -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 = `
<a href="https://cbconnection.adobe.com/home/search">cbc prod Link</a>
<a href="https://partners.adobe.com">Partner prod Link</a>
<a href="https://cbconnection.adobe.com/bin/fusion/modalImsLogin?resource=/home/search">cbc Login Link</a>
<a href = 'https://cbconnection.adobe.com/en/news/enablement-news-partner-lock'></a>
`;

describe('Test rewrite links', () => {
beforeEach(() => {
getConfig.mockReturnValue({ env: { name: 'stage' } });
});
afterEach(() => {
jest.clearAllMocks(); // Clear mocks after each test
document.body.innerHTML = `
<a href="https://cbconnection.adobe.com/home/search">cbc prod Link</a>
<a href="https://partners.adobe.com">Partner prod Link</a>
<a href="https://cbconnection.adobe.com/bin/fusion/modalImsLogin?resource=/home/search">cbc Login Link</a>
<a href = 'https://cbconnection.adobe.com/en/news/enablement-news-partner-lock'></a>
`;
});

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');
});
});
Loading