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.
This commit is contained in:
Matjaz Gregoric
2021-06-08 10:23:17 +02:00
parent c0bed87954
commit fdf600a527
2 changed files with 37 additions and 5 deletions

View File

@@ -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):
"""

View File

@@ -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)