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

fix(#9732): infinite loop on privacy policy page #9733

Merged
11 changes: 4 additions & 7 deletions api/src/public/privacy-policy/script.js
Original file line number Diff line number Diff line change
@@ -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());
});
});
6 changes: 3 additions & 3 deletions api/src/templates/privacy-policy/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,20 @@
<body>
<div>
<% if(showBackButton) { %>
<a id="back-button" href="#">{{ translations.back }}</a>
<a class="back-button" href="#">{{ translations.back }}</a>
<% } else { %>
<a href="/">{{ translations.login }}</a>
<% } %>
</div>
<div>
<h1>{{ translations.title }}</h1>
</div>
<div>
<div id="privacy-policy-content">
{{ policy }}
</div>
<div>
<% if(showBackButton) { %>
<a id="back-button" href="#">{{ translations.back }}</a>
<a class="back-button" href="#">{{ translations.back }}</a>
<% } else { %>
<a href="/">{{ translations.login }}</a>
<% } %>
Expand Down
56 changes: 56 additions & 0 deletions tests/e2e/default/privacy-policy/login-privacy-policy.wdio-spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
7 changes: 7 additions & 0 deletions tests/page-objects/default/login/login.wdio.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -133,6 +134,10 @@ const setPasswordValue = async (password) => {
await (await passwordField()).setValue(password);
};

const goToPrivacyPolicyPage = async () => {
await (await privacyPolicyPageLink()).click();
};

module.exports = {
login,
cookieLogin,
Expand All @@ -147,4 +152,6 @@ module.exports = {
getErrorMessage,
togglePassword,
setPasswordValue,
privacyPolicyPageLink,
goToPrivacyPolicyPage
};
Original file line number Diff line number Diff line change
@@ -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
};