diff --git a/edsdme/scripts/scripts.js b/edsdme/scripts/scripts.js index 8fe6514..48014f8 100644 --- a/edsdme/scripts/scripts.js +++ b/edsdme/scripts/scripts.js @@ -1,4 +1,4 @@ -import { setLibs, redirectLoggedinPartner, updateIMSConfig, preloadResources, getRenewBanner, updateFooter } from './utils.js'; +import { setLibs, redirectLoggedinPartner, updateIMSConfig, preloadResources, getRenewBanner, updateNavigation, updateFooter } from './utils.js'; // Add project-wide style path here. const STYLES = '/edsdme/styles/styles.css'; @@ -72,6 +72,7 @@ const miloLibs = setLibs(LIBS); }()); function setUpPage() { + updateNavigation(CONFIG.locales); updateFooter(CONFIG.locales); } diff --git a/edsdme/scripts/utils.js b/edsdme/scripts/utils.js index 2376953..875c67b 100644 --- a/edsdme/scripts/utils.js +++ b/edsdme/scripts/utils.js @@ -357,6 +357,15 @@ export async function preloadResources(locales, miloLibs) { }); } +export function updateNavigation(locales) { + const { prefix } = getLocale(locales); + const gnavMeta = getMetadata('gnav-source'); + if (!gnavMeta || !isMember()) return; + + const gnavLoggedIn = getMetadataContent('gnav-loggedin-source'); + gnavMeta.content = gnavLoggedIn ?? `${prefix}/edsdme/partners-shared/loggedin-gnav`; +} + export function updateFooter(locales) { const { prefix } = getLocale(locales); const footerMeta = getMetadata('footer-source'); diff --git a/test/scripts/utils.jest.js b/test/scripts/utils.jest.js index ddd2b73..043064a 100644 --- a/test/scripts/utils.jest.js +++ b/test/scripts/utils.jest.js @@ -3,7 +3,7 @@ */ import path from 'path'; import fs from 'fs'; -import { updateFooter } from '../../edsdme/scripts/utils.js'; +import { updateFooter, updateNavigation } from '../../edsdme/scripts/utils.js'; describe('Test utils.js', () => { beforeEach(() => { window = Object.create(window); @@ -75,6 +75,60 @@ describe('Test utils.js', () => { expect(footerPath).not.toEqual(footerPathModified); expect(footerPathModified).toEqual('/de/edsdme/partners-shared/loggedin-footer'); }); + it('Public navigation is shown for non member', async () => { + const cookieObject = { + SPP: { + status: 'MEMBER', + } + }; + document.cookie = `partner_data=${JSON.stringify(cookieObject)}`; + const locales = { + '': { ietf: 'en-US', tk: 'hah7vzn.css' }, + de: { ietf: 'de-DE', tk: 'hah7vzn.css' }, + }; + const gnavPath = document.querySelector('meta[name="gnav-source"]')?.content + updateNavigation(locales); + const gnavPathModified = document.querySelector('meta[name="gnav-source"]')?.content + expect(gnavPath).toEqual(gnavPathModified); + }); + it('Protected navigation is shown for members', async () => { + const cookieObject = { + CPP: { + status: 'MEMBER', + } + }; + document.cookie = `partner_data=${JSON.stringify(cookieObject)}`; + const locales = { + '': { ietf: 'en-US', tk: 'hah7vzn.css' }, + de: { ietf: 'de-DE', tk: 'hah7vzn.css' }, + }; + const gnavPath = document.querySelector('meta[name="gnav-source"]')?.content; + updateNavigation(locales); + const gnavPathModified = document.querySelector('meta[name="gnav-source"]')?.content; + expect(gnavPath).not.toEqual(gnavPathModified); + const protectedGnavPath = document.querySelector('meta[name="gnav-loggedin-source"]')?.content; + expect(gnavPathModified).toEqual(protectedGnavPath); + }); + it('Protected footer is fetched based on locale if gnav-loggeding-source metadata is not present', async () => { + const cookieObject = { + CPP: { + status: 'MEMBER', + } + }; + document.cookie = `partner_data=${JSON.stringify(cookieObject)}`; + window.location.pathname = '/de/channelpartners/'; + const locales = { + '': { ietf: 'en-US', tk: 'hah7vzn.css' }, + de: { ietf: 'de-DE', tk: 'hah7vzn.css' }, + }; + const gnavPath = document.querySelector('meta[name="gnav-source"]')?.content; + const protectedGnav = document.querySelector('meta[name="gnav-loggedin-source"]'); + protectedGnav.remove(); + updateNavigation(locales); + const gnavPathModified = document.querySelector('meta[name="gnav-source"]')?.content; + expect(gnavPath).not.toEqual(gnavPathModified); + expect(gnavPathModified).toEqual('/de/edsdme/partners-shared/loggedin-gnav'); + }); }); });