From c3240f133e802452d4c34fa13c1a053f0dd23fe7 Mon Sep 17 00:00:00 2001 From: paulpascal Date: Wed, 8 Jan 2025 15:34:57 +0000 Subject: [PATCH 1/2] fix(#9732): infinite loop on privacy policy page --- api/src/public/privacy-policy/script.js | 11 ++++------- api/src/templates/privacy-policy/index.html | 4 ++-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/api/src/public/privacy-policy/script.js b/api/src/public/privacy-policy/script.js index 637572e6a4c..676359d5ab9 100644 --- a/api/src/public/privacy-policy/script.js +++ b/api/src/public/privacy-policy/script.js @@ -1,8 +1,5 @@ -document.addEventListener('DOMContentLoaded', function() { - const backButton = document.getElementById('back-button'); - if (backButton) { - backButton.addEventListener('click', function() { - history.go(-1); - }, false); - } +document.addEventListener('DOMContentLoaded', () => { + document.querySelectorAll('.back-button').forEach(button => { + button.addEventListener('click', () => window.history.back()); + }); }); diff --git a/api/src/templates/privacy-policy/index.html b/api/src/templates/privacy-policy/index.html index dec65d739f4..7574d1fb31c 100644 --- a/api/src/templates/privacy-policy/index.html +++ b/api/src/templates/privacy-policy/index.html @@ -27,7 +27,7 @@
<% if(showBackButton) { %> - {{ translations.back }} + {{ translations.back }} <% } else { %> {{ translations.login }} <% } %> @@ -40,7 +40,7 @@

{{ translations.title }}

<% if(showBackButton) { %> - {{ translations.back }} + {{ translations.back }} <% } else { %> {{ translations.login }} <% } %> From ccc6dd2c962fed8055ed52ef4e53813e9059dcb3 Mon Sep 17 00:00:00 2001 From: paulpascal Date: Tue, 28 Jan 2025 15:27:08 +0000 Subject: [PATCH 2/2] fix(#9732): address review feedback --- api/src/templates/privacy-policy/index.html | 2 +- .../login-privacy-policy.wdio-spec.js | 56 +++++++++++++++++++ .../default/login/login.wdio.page.js | 7 +++ .../login-privacy-policy.wdio.page.js | 26 +++++++++ 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/e2e/default/privacy-policy/login-privacy-policy.wdio-spec.js create mode 100644 tests/page-objects/default/privacy-policy/login-privacy-policy.wdio.page.js diff --git a/api/src/templates/privacy-policy/index.html b/api/src/templates/privacy-policy/index.html index 7574d1fb31c..7bcde66bded 100644 --- a/api/src/templates/privacy-policy/index.html +++ b/api/src/templates/privacy-policy/index.html @@ -35,7 +35,7 @@

{{ translations.title }}

-
+
{{ policy }}
diff --git a/tests/e2e/default/privacy-policy/login-privacy-policy.wdio-spec.js b/tests/e2e/default/privacy-policy/login-privacy-policy.wdio-spec.js new file mode 100644 index 00000000000..fc15267dadd --- /dev/null +++ b/tests/e2e/default/privacy-policy/login-privacy-policy.wdio-spec.js @@ -0,0 +1,56 @@ +const utils = require('@utils'); +const commonPage = require('@page-objects/default/common/common.wdio.page.js'); +const privacyPolicyFactory = require('@factories/cht/settings/privacy-policy'); +const loginPage = require('@page-objects/default/login/login.wdio.page'); +const loginPrivacyPolicyPage = require('@page-objects/default/privacy-policy/login-privacy-policy.wdio.page'); + +describe('Privacy Policy Navigation for Unauthenticated Users', () => { + const privacyPolicy = privacyPolicyFactory.privacyPolicy().build(); + + afterEach(async () => { + await utils.deleteAllDocs(); + }); + + it('should not display privacy policy page when the privacy policy is not enabled', async () => { + await commonPage.reloadSession(); + + // Assert: privacy policy link is not available on the login page + const privacyPolicyLink = await loginPage.privacyPolicyPageLink(); + expect(await privacyPolicyLink.isDisplayed()).to.equal(false); + }); + + it('should navigate back to the login page when using either back button', async () => { + await utils.saveDocs([privacyPolicy]); + await commonPage.reloadSession(); + + // Navigate to privacy policy page + await loginPage.goToPrivacyPolicyPage(); + const privacyContent = await loginPrivacyPolicyPage.privacyContent(); + expect(await privacyContent.isDisplayed()).to.equal(true); + + // Test navigation using both back buttons + const testBackButton = async (backButtonType) => { + let backButton; + + if (backButtonType === 'top') { + backButton = await loginPrivacyPolicyPage.topBackButton(); + } else if (backButtonType === 'bottom') { + await loginPrivacyPolicyPage.scrollToBottom(); + backButton = await loginPrivacyPolicyPage.bottomBackButton(); + } + + // Click the back button + await loginPrivacyPolicyPage.goBackToLoginPage(backButton); + + // Assert: back button redirects to the login page + expect((await browser.getUrl()).includes('/medic/login')).to.be.true; + + // Navigate back to the privacy policy page for the next iteration + await loginPage.goToPrivacyPolicyPage(); + }; + + // Run tests for both buttons + await testBackButton('top'); + await testBackButton('bottom'); + }); +}); diff --git a/tests/page-objects/default/login/login.wdio.page.js b/tests/page-objects/default/login/login.wdio.page.js index e61b4fd5f82..a965e0d21d4 100644 --- a/tests/page-objects/default/login/login.wdio.page.js +++ b/tests/page-objects/default/login/login.wdio.page.js @@ -11,6 +11,7 @@ const labelForPassword = () => $('label[for="password"]'); const errorMessageField = () => $('p.error.incorrect'); const localeByName = (locale) => $(`.locale[name="${locale}"]`); const tokenLoginError = (reason) => $(`.error.${reason}`); +const privacyPolicyPageLink = () => $('a[translate="privacy.policy"]'); const getErrorMessage = async () => { await (await errorMessageField()).waitForDisplayed(); @@ -133,6 +134,10 @@ const setPasswordValue = async (password) => { await (await passwordField()).setValue(password); }; +const goToPrivacyPolicyPage = async () => { + await (await privacyPolicyPageLink()).click(); +}; + module.exports = { login, cookieLogin, @@ -147,4 +152,6 @@ module.exports = { getErrorMessage, togglePassword, setPasswordValue, + privacyPolicyPageLink, + goToPrivacyPolicyPage }; diff --git a/tests/page-objects/default/privacy-policy/login-privacy-policy.wdio.page.js b/tests/page-objects/default/privacy-policy/login-privacy-policy.wdio.page.js new file mode 100644 index 00000000000..0a8025557e9 --- /dev/null +++ b/tests/page-objects/default/privacy-policy/login-privacy-policy.wdio.page.js @@ -0,0 +1,26 @@ +const privacyContent = () => $('#privacy-policy-content'); +const backButtons = () => $$('a.back-button'); + +const topBackButton = async () => { + return (await backButtons())[0]; +}; + +const bottomBackButton = async () => { + return (await backButtons())[1]; +}; + +const scrollToBottom = async () => { + await (await bottomBackButton()).scrollIntoView(); +}; + +const goBackToLoginPage = async (backButton) => { + await (backButton).click(); +}; + +module.exports = { + privacyContent, + topBackButton, + bottomBackButton, + scrollToBottom, + goBackToLoginPage +};