Files
edx-platform/openedx/features/course_experience/tests/test_url_helpers.py
Feanil Patel 88c7cd7bf3 feat!: Remove Legacy Preview Functionality (#36460)
* feat!: Remove all trivial mentions of PREVIEW_LMS_BASE

There are a few more mentions but these are all the ones that don't need
major further followup.

BREAKING CHANGE: The learning MFE now supports preview functionality
natively and it is no longer necessary to use a different domain on the
LMS to render a preview of course content.

See https://github.com/openedx/frontend-app-learning/issues/1455 for
more details.

* feat: Drop the `in_preview_mode` function.

Since we're no longer using a separate domain, that check always
returned false.  Remove it and update any places/tests where it is used.

* feat: Drop courseware_mfe_is_active function.

With the removal of the preview check this function is also a no-op now
so drop calls to it and update the places where it is called to not
change other behavior.

* feat!: Drop redirect to preview from the legacy courseware index.

The CoursewareIndex view is going to be removed eventually but for now
we're focusing on removing the PREVIEW_LMS_BASE setting.  With this
change, if someone tries to load the legacy courseware URL from the
preview domain it will no longer redirect them to the MFE preview.

This is not a problem that will occur for users coming from existing
studio links because those links have already been updated to go
directly to the new urls.

The only way this path could execute is if someone goes directly to the
old Preview URL that they saved off platform somewhere.  eg. If they
bookmarked it for some reason.

BREAKING CHANGE: Saved links (including bookmarks) to the legacy preview
URLs will no longer redirect to the MFE preview URLs.

* test: Drop the set_preview_mode test helper.

This test helper was setting the preview mode for tests by changing the
hostname that was set while tests were running.  This was mostly not
being used to test preview but to run a bunch of legacy courseware tests
while defaulting to the new learning MFE for the courseware.

This commit updates various tests in the `courseware` app to not rely on
the fact that we're in preview to test legacy courseware behavior and
instead directly patches either the `_redirect_to_learning_mfe` function
or uses the `_get_legacy_courseware_url` or both to be able to have the
tests continue to test the legacy coursewary.

This will hopefully make the tests more accuarte even though hopefully
we'll just be removing many of them soon as a part of the legacy
courseware cleanup.

We're just doing the preview removal separately to reduce the number of
things that are changing at once.

* test: Drop the `_get_urls_function`

With the other recent cleanup, this function is no longer being
referenced by anything so we can just drop it.

* test: Test student access to unpublihsed content.

Ensure that students can't get access to unpublished content.
2025-05-14 08:59:11 -04:00

165 lines
5.5 KiB
Python

"""
Test some of the functions in url_helpers
"""
import ddt
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase
from xmodule.modulestore.tests.factories import CourseFactory, BlockFactory
from .. import url_helpers
@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):
"""
Test get_courseware_url.
"""
@classmethod
def setUpClass(cls):
"""
Set up data used across test functions.
"""
# pylint: disable=super-method-not-called
with super().setUpClassAndTestData():
cls.items = cls.create_test_courses()
@classmethod
def create_test_courses(cls):
"""
We build simple course structures.
Course structure is a non-branching tree from the root Course block down
to the Component-level problem block;
For easy access in the test functions, we return a dict like this:
{
"course_run": <course block for Split Mongo course>,
"section": <chapter block in course run>
"subsection": <sequence block in section>
"unit": <vertical block in subsection>
"component": <problem block in unit>
}
"""
course_run = CourseFactory.create(
org='TestX',
number='UrlHelpers',
run='split',
display_name='URL Helpers Test Course',
)
with cls.store.bulk_operations(course_run.id):
section = BlockFactory.create(
parent_location=course_run.location,
category='chapter',
display_name="Generated Section",
)
subsection = BlockFactory.create(
parent_location=section.location,
category='sequential',
display_name="Generated Subsection",
)
unit = BlockFactory.create(
parent_location=subsection.location,
category='vertical',
display_name="Generated Unit",
)
component = BlockFactory.create(
parent_location=unit.location,
category='problem',
display_name="Generated Problem Component",
)
return {
'course_run': course_run,
'section': section,
'subsection': subsection,
'unit': unit,
'component': component,
}
@ddt.data(
(
'course_run',
'http://learning-mfe/course/course-v1:TestX+UrlHelpers+split'
),
(
'section',
(
'http://learning-mfe/course/course-v1:TestX+UrlHelpers+split' +
'/block-v1:TestX+UrlHelpers+split+type@chapter+block@Generated_Section'
),
),
(
'subsection',
(
'http://learning-mfe/course/course-v1:TestX+UrlHelpers+split' +
'/block-v1:TestX+UrlHelpers+split+type@sequential+block@Generated_Subsection'
),
),
(
'unit',
(
'http://learning-mfe/course/course-v1:TestX+UrlHelpers+split' +
'/block-v1:TestX+UrlHelpers+split+type@sequential+block@Generated_Subsection' +
'/block-v1:TestX+UrlHelpers+split+type@vertical+block@Generated_Unit'
),
),
(
'component',
(
'http://learning-mfe/course/course-v1:TestX+UrlHelpers+split' +
'/block-v1:TestX+UrlHelpers+split+type@sequential+block@Generated_Subsection' +
'/block-v1:TestX+UrlHelpers+split+type@vertical+block@Generated_Unit'
),
),
)
@ddt.unpack
def test_get_courseware_url(
self,
structure_level,
expected_path,
):
"""
Given:
* a `store_type` ('split' or [old] 'mongo'),
* an `active_experience` ('mfe' or 'legacy'),
* and a `structure_level` ('course_run', 'section', 'subsection', 'unit', or 'component'),
check that the expected path (URL without querystring) is returned by `get_courseware_url`.
"""
block = self.items[structure_level]
url = url_helpers.get_courseware_url(block.location)
path = url.split('?')[0]
assert path == expected_path
course_run = self.items['course_run']