diff --git a/lms/djangoapps/learner_home/serializers.py b/lms/djangoapps/learner_home/serializers.py index 0ce7bf9c6977..b3471715b9dc 100644 --- a/lms/djangoapps/learner_home/serializers.py +++ b/lms/djangoapps/learner_home/serializers.py @@ -15,6 +15,7 @@ from common.djangoapps.course_modes.models import CourseMode from openedx.features.course_experience import course_home_url from xmodule.data import CertificatesDisplayBehaviors +from lms.djangoapps.learner_home.utils import course_progress_url class LiteralField(serializers.Field): @@ -116,7 +117,7 @@ def get_homeUrl(self, instance): return course_home_url(instance.course_id) def get_progressUrl(self, instance): - return reverse("progress", kwargs={"course_id": instance.course_id}) + return course_progress_url(instance.course_id) def get_unenrollUrl(self, instance): return reverse("course_run_refund_status", args=[instance.course_id]) diff --git a/lms/djangoapps/learner_home/test_serializers.py b/lms/djangoapps/learner_home/test_serializers.py index f588af58aee4..6d29795adecc 100644 --- a/lms/djangoapps/learner_home/test_serializers.py +++ b/lms/djangoapps/learner_home/test_serializers.py @@ -58,6 +58,7 @@ random_date, random_string, random_url, + expected_progress_url, ) from xmodule.data import CertificatesDisplayBehaviors from xmodule.modulestore.tests.django_utils import SharedModuleStoreTestCase @@ -224,6 +225,35 @@ def test_missing_resume_url(self): # Then the resumeUrl is None, which is allowed self.assertIsNone(output_data["resumeUrl"]) + def is_progress_url_matching_course_home_mfe_progress_tab_is_active(self, mfe_progress_tab_is_active): + """ + Compares the progress URL generated by CourseRunSerializer to the expected progress URL, + based on the status of mfe_progress_tab_is_active. + + :param mfe_progress_tab_is_active: A boolean matching the return value from the + course_home_mfe_progress_tab_is_active function, + implemented in lms.djangoapps.course_home_api.toggles. + + :return: True if the generated progress URL matches the expected progress URL, False otherwise. + """ + input_data = self.create_test_enrollment() + input_context = self.create_test_context(input_data.course.id) + output_data = CourseRunSerializer(input_data, context=input_context).data + return output_data['progressUrl'] == expected_progress_url(input_data.course.id, mfe_progress_tab_is_active) + + @mock.patch('lms.djangoapps.learner_home.utils.course_home_mfe_progress_tab_is_active') + def test_progress_url(self, mock_course_home_mfe_progress_tab_is_active): + """ + Tests the progress URL generated by the CourseRunSerializer. When course_home_mfe_progress_tab_is_active + is true, the generated progress URL must point to the progress page of the course home (learning) MFE. + Otherwise, it must point to the legacy progress page. + """ + mock_course_home_mfe_progress_tab_is_active.return_value = True + self.assertTrue(self.is_progress_url_matching_course_home_mfe_progress_tab_is_active(True)) + + mock_course_home_mfe_progress_tab_is_active.return_value = False + self.assertTrue(self.is_progress_url_matching_course_home_mfe_progress_tab_is_active(False)) + @ddt.ddt class TestCoursewareAccessSerializer(LearnerDashboardBaseTest): diff --git a/lms/djangoapps/learner_home/test_utils.py b/lms/djangoapps/learner_home/test_utils.py index f8a7dc29f7b8..216491205dd9 100644 --- a/lms/djangoapps/learner_home/test_utils.py +++ b/lms/djangoapps/learner_home/test_utils.py @@ -7,6 +7,8 @@ from time import time from uuid import uuid4 +from django.conf import settings + from common.djangoapps.course_modes.models import CourseMode from common.djangoapps.course_modes.tests.factories import CourseModeFactory from common.djangoapps.student.tests.factories import CourseEnrollmentFactory @@ -86,3 +88,19 @@ def create_test_enrollment(user, course_mode=CourseMode.AUDIT): test_enrollment.course_overview.end = random_date() return test_enrollment + + +def expected_progress_url(course_id, course_home_mfe_progress_tab_is_active): + """ + Returns expected course progress URL based on the status of the course_home_mfe_progress_tab_is_active. + + :param course_id: The course key / ID for which the progress url is being requested. + :param course_home_mfe_progress_tab_is_active: A boolean matching the return value from the + course_home_mfe_progress_tab_is_active function, + implemented in lms.djangoapps.course_home_api.toggles. + + :return: The course progress URL. + """ + if course_home_mfe_progress_tab_is_active: + return f'{settings.LEARNING_MICROFRONTEND_URL}/course/{course_id}/progress' + return f'/courses/{course_id}/progress' diff --git a/lms/djangoapps/learner_home/utils.py b/lms/djangoapps/learner_home/utils.py index 28e4479f9439..96af6a64452b 100644 --- a/lms/djangoapps/learner_home/utils.py +++ b/lms/djangoapps/learner_home/utils.py @@ -4,6 +4,7 @@ import logging +from django.urls import reverse from django.contrib.auth import get_user_model from django.core.exceptions import MultipleObjectsReturned from rest_framework.exceptions import PermissionDenied, NotFound @@ -11,6 +12,8 @@ from common.djangoapps.student.models import ( get_user_by_username_or_email, ) +from lms.djangoapps.course_home_api.toggles import course_home_mfe_progress_tab_is_active +from openedx.features.course_experience.url_helpers import get_learning_mfe_home_url log = logging.getLogger(__name__) User = get_user_model() @@ -54,3 +57,16 @@ def get_masquerade_user(request): ) log.info(success_msg) return masquerade_user + + +def course_progress_url(course_key) -> str: + """ + Returns the course progress page's URL for the current user. + + :param course_key: The course key for which the home url is being requested. + + :return: The course progress page URL. + """ + if course_home_mfe_progress_tab_is_active(course_key): + return get_learning_mfe_home_url(course_key, url_fragment='progress') + return reverse('progress', kwargs={'course_id': course_key})