From 96970cf6075a5b02e8c5b25acfc18eed02cde010 Mon Sep 17 00:00:00 2001 From: Ejaz Ahmad <86868918+jajjibhai008@users.noreply.github.com> Date: Fri, 25 Oct 2024 17:24:53 +0500 Subject: [PATCH] refactor: improve the sanitization for expiration modal (#1213) --- .../expired-subscription-modal/index.jsx | 31 +++++++------------ .../tests/ExpiredSubscriptionModal.test.jsx | 16 +++++----- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/components/expired-subscription-modal/index.jsx b/src/components/expired-subscription-modal/index.jsx index ff8dbedc30..5e69f184f4 100644 --- a/src/components/expired-subscription-modal/index.jsx +++ b/src/components/expired-subscription-modal/index.jsx @@ -1,6 +1,7 @@ import { useToggle, AlertModal, Button, ActionRow, } from '@openedx/paragon'; +import DOMPurify from 'dompurify'; import { useSubscriptions } from '../app/data'; const ExpiredSubscriptionModal = () => { @@ -9,37 +10,27 @@ const ExpiredSubscriptionModal = () => { if (!customerAgreement?.hasCustomLicenseExpirationMessaging) { return null; } - const onClickHandler = () => { - let url = customerAgreement?.urlForButtonInModal; - - if (url) { - // Check if the URL starts with 'http://' or 'https://' - if (!url.startsWith('http://') && !url.startsWith('https://')) { - // Prepend 'https://' if the URL is missing the protocol - url = `https://${url}`; - } - - // Navigate to the full URL - window.open(url, '_blank'); // Opening in a new tab - } - }; return ( {customerAgreement?.modalHeaderText}} + title={

{customerAgreement.modalHeaderText}

} isOpen={isOpen} isBlocking footerNode={( - )} > {/* eslint-disable-next-line react/no-danger */} -
+
); }; diff --git a/src/components/expired-subscription-modal/tests/ExpiredSubscriptionModal.test.jsx b/src/components/expired-subscription-modal/tests/ExpiredSubscriptionModal.test.jsx index eb847b106b..bc053adfa5 100644 --- a/src/components/expired-subscription-modal/tests/ExpiredSubscriptionModal.test.jsx +++ b/src/components/expired-subscription-modal/tests/ExpiredSubscriptionModal.test.jsx @@ -1,5 +1,6 @@ import { screen } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; +import userEvent from '@testing-library/user-event'; import ExpiredSubscriptionModal from '../index'; import { useSubscriptions } from '../../app/data'; import { renderWithRouter } from '../../../utils/tests'; @@ -79,24 +80,21 @@ describe('', () => { modalHeaderText: 'Expired Subscription', buttonLabelInModal: 'Continue Learning', expiredSubscriptionModalMessaging: '

Your subscription has expired.

', - urlForButtonInModal: 'example.com', + urlForButtonInModal: 'https://example.com', }, }, }); - // Mock window.open - const windowOpenSpy = jest.spyOn(window, 'open').mockImplementation(() => {}); - // Render the component renderWithRouter(); + // Find the Continue Learning button const continueButton = screen.getByText('Continue Learning'); - continueButton.click(); - // Assert window.open was called with the correct URL - expect(windowOpenSpy).toHaveBeenCalledWith('https://example.com', '_blank'); + // Simulate a click on the button + userEvent.click(continueButton); - // Restore window.open after the test - windowOpenSpy.mockRestore(); + // Check that the button was rendered and clicked + expect(continueButton).toBeInTheDocument(); }); });