From fdf600a5273d8bf85b6262650310dca1eafaa121 Mon Sep 17 00:00:00 2001 From: Matjaz Gregoric Date: Tue, 8 Jun 2021 10:23:17 +0200 Subject: [PATCH] fix: support Learning MFE hosted on subpath. The Referrer header from the MFE hosted on a different origin does not include the path, so make sure to ignore the path portion of the learning MFE base URL when determining whether the request is coming from the new MFE or the old courseware view. --- .../tests/test_url_helpers.py | 30 +++++++++++++++++++ .../features/course_experience/url_helpers.py | 12 ++++---- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/openedx/features/course_experience/tests/test_url_helpers.py b/openedx/features/course_experience/tests/test_url_helpers.py index 31c469022f..4728858cf0 100644 --- a/openedx/features/course_experience/tests/test_url_helpers.py +++ b/openedx/features/course_experience/tests/test_url_helpers.py @@ -4,6 +4,9 @@ Test some of the functions in url_helpers from unittest import mock import ddt +from django.test import TestCase +from django.test.client import RequestFactory +from django.test.utils import override_settings from xmodule.modulestore import ModuleStoreEnum from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase @@ -20,6 +23,33 @@ def _patch_courseware_mfe_is_active(ret_val): ) +@ddt.ddt +class IsLearningMfeTests(TestCase): + """ + Test is_request_from_learning_mfe. + """ + + def setUp(self): + super().setUp() + self.request_factory = RequestFactory() + + @ddt.data( + ('', '', False,), + ('https://mfe-url/', 'https://platform-url/course', False,), + ('https://mfe-url/', 'https://mfe-url/course', True,), + ('https://mfe-url/', 'https://mfe-url/', True,), + ('https://mfe-url/subpath/', 'https://platform-url/course', False,), + ('https://mfe-url/subpath/', 'https://mfe-url/course', True,), + ('https://mfe-url/subpath/', 'https://mfe-url/', True,), + ) + @ddt.unpack + def test_is_request_from_learning_mfe(self, mfe_url, referrer_url, is_mfe): + with override_settings(LEARNING_MICROFRONTEND_URL=mfe_url): + request = self.request_factory.get('/course') + request.META['HTTP_REFERER'] = referrer_url + assert url_helpers.is_request_from_learning_mfe(request) == is_mfe + + @ddt.ddt class GetCoursewareUrlTests(SharedModuleStoreTestCase): """ diff --git a/openedx/features/course_experience/url_helpers.py b/openedx/features/course_experience/url_helpers.py index c0ab5f6262..ecc5515f89 100644 --- a/openedx/features/course_experience/url_helpers.py +++ b/openedx/features/course_experience/url_helpers.py @@ -13,7 +13,7 @@ from django.contrib.auth import get_user_model from django.http import HttpRequest from django.urls import reverse from opaque_keys.edx.keys import CourseKey, UsageKey -from six.moves.urllib.parse import urlencode +from six.moves.urllib.parse import urlencode, urlparse from lms.djangoapps.courseware.toggles import courseware_mfe_is_active from xmodule.modulestore.django import modulestore @@ -213,7 +213,9 @@ def is_request_from_learning_mfe(request: HttpRequest): """ Returns whether the given request was made by the frontend-app-learning MFE. """ - return ( - settings.LEARNING_MICROFRONTEND_URL and - request.META.get('HTTP_REFERER', '').startswith(settings.LEARNING_MICROFRONTEND_URL) - ) + if not settings.LEARNING_MICROFRONTEND_URL: + return False + + url = urlparse(settings.LEARNING_MICROFRONTEND_URL) + mfe_url_base = f'{url.scheme}://{url.netloc}' + return request.META.get('HTTP_REFERER', '').startswith(mfe_url_base)