diff --git a/lms/djangoapps/courseware/tests/test_views.py b/lms/djangoapps/courseware/tests/test_views.py index 126233880ae1..cdc59e48c0e6 100644 --- a/lms/djangoapps/courseware/tests/test_views.py +++ b/lms/djangoapps/courseware/tests/test_views.py @@ -3777,7 +3777,7 @@ def test_courseware_mfe_search_staff_access(self): @override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=False) def test_is_mfe_search_waffle_disabled(self): """ - Courseware search is only available when the waffle flag is enabled. + Courseware search is only available when the waffle flag is enabled, if no inclusion date is provided. """ user_admin = UserFactory(is_staff=True, is_superuser=True) CourseEnrollmentFactory.create(user=user_admin, course_id=self.course.id, mode=CourseMode.VERIFIED) @@ -3789,6 +3789,7 @@ def test_is_mfe_search_waffle_disabled(self): self.assertEqual(body, {'enabled': False}) @patch.dict('django.conf.settings.FEATURES', {'COURSEWARE_SEARCH_INCLUSION_DATE': '2020'}) + @override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=False) @ddt.data( (datetime(2013, 9, 18, 11, 30, 00), False), (None, False), diff --git a/lms/djangoapps/courseware/views/views.py b/lms/djangoapps/courseware/views/views.py index 4fd124e1a47e..88f0ed05ccf7 100644 --- a/lms/djangoapps/courseware/views/views.py +++ b/lms/djangoapps/courseware/views/views.py @@ -2316,28 +2316,35 @@ def courseware_mfe_search_enabled(request, course_id=None): Simple GET endpoint to expose whether the user may use Courseware Search for a given course. """ - enabled = False course_key = CourseKey.from_string(course_id) if course_id else None user = request.user + has_required_enrollment = False if settings.FEATURES.get('ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED'): enrollment_mode, _ = CourseEnrollment.enrollment_mode_for_user(user, course_key) if ( auth.user_has_role(user, CourseStaffRole(CourseKey.from_string(course_id))) or (enrollment_mode in CourseMode.VERIFIED_MODES) ): - enabled = True + has_required_enrollment = True else: - enabled = True + has_required_enrollment = True inclusion_date = settings.FEATURES.get('COURSEWARE_SEARCH_INCLUSION_DATE') start_date = CourseOverview.get_from_id(course_key).start + has_valid_inclusion_date = False - # only include courses that have a start date later than the setting-defined inclusion date + # only include courses that have a start date later than the setting-defined inclusion date, if setting exists if inclusion_date: - enabled = enabled and (start_date and start_date.strftime('%Y-%m-%d') > inclusion_date) + has_valid_inclusion_date = start_date and start_date.strftime('%Y-%m-%d') > inclusion_date - payload = {"enabled": courseware_mfe_search_is_enabled(course_key) if enabled else False} + # if the user has the appropriate enrollment, the feature is enabled if the course has a valid start date + # or if the feature is explicitly enabled via waffle flag. + enabled = (has_valid_inclusion_date or courseware_mfe_search_is_enabled(course_key)) \ + if has_required_enrollment \ + else False + + payload = {"enabled": enabled} return JsonResponse(payload)