Skip to content

Commit

Permalink
feat: feature setting to gate courseware search to verified enrollmen…
Browse files Browse the repository at this point in the history
…ts (#34606)

Adds a Django setting that limits courseware search to users in a verified enrollment track.
  • Loading branch information
zacharis278 authored Apr 25, 2024
1 parent 3fd8037 commit 3852358
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
40 changes: 34 additions & 6 deletions lms/djangoapps/courseware/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3748,6 +3748,8 @@ def test_get_template_and_context(self):
assert context['course'] == self.course


@ddt.ddt
@override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=True)
class TestCoursewareMFESearchAPI(SharedModuleStoreTestCase):
"""
Tests the endpoint to fetch the Courseware Search waffle flag enabled status.
Expand All @@ -3761,24 +3763,50 @@ def setUp(self):
self.client = APIClient()
self.apiUrl = reverse('courseware_search_enabled_view', kwargs={'course_id': str(self.course.id)})

@override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=True)
def test_courseware_mfe_search_enabled(self):
@ddt.data(
(CourseMode.AUDIT, False),
(CourseMode.VERIFIED, True),
(CourseMode.MASTERS, True),
)
@ddt.unpack
@patch.dict('django.conf.settings.FEATURES', {'ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED': True})
def test_courseware_mfe_search_verified_only(self, mode, expected_enabled):
"""
Only verified enrollees may use Courseware Search if ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED
is enabled.
"""
user = UserFactory()
CourseEnrollmentFactory.create(user=user, course_id=self.course.id, mode=mode)

self.client.login(username=user.username, password=TEST_PASSWORD)
response = self.client.get(self.apiUrl, content_type='application/json')
body = json.loads(response.content.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(body, {'enabled': expected_enabled})

@patch.dict('django.conf.settings.FEATURES', {'ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED': True})
def test_courseware_mfe_search_staff_access(self):
"""
Getter to check if user is allowed to use Courseware Search.
Staff users may use Courseware Search regardless of their enrollment status.
"""
user_staff = UserFactory(is_staff=True) # not enrolled

self.client.login(username=user_staff.username, password=TEST_PASSWORD)
response = self.client.get(self.apiUrl, content_type='application/json')
body = json.loads(response.content.decode('utf-8'))

self.assertEqual(response.status_code, 200)
self.assertEqual(body, {'enabled': True})

@override_waffle_flag(COURSEWARE_MICROFRONTEND_SEARCH_ENABLED, active=False)
def test_is_mfe_search_disabled(self):
def test_is_mfe_search_waffle_disabled(self):
"""
Getter to check if user is allowed to use Courseware Search.
Courseware search is only available when the waffle flag is enabled.
"""

user_admin = UserFactory(is_staff=True, is_superuser=True)
CourseEnrollmentFactory.create(user=user_admin, course_id=self.course.id, mode=CourseMode.VERIFIED)
self.client.login(username=user_admin.username, password=TEST_PASSWORD)
response = self.client.get(self.apiUrl, content_type='application/json')
body = json.loads(response.content.decode('utf-8'))

Expand Down
20 changes: 17 additions & 3 deletions lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

from common.djangoapps.course_modes.models import CourseMode, get_course_prices
from common.djangoapps.edxmako.shortcuts import marketing_link, render_to_response, render_to_string
from common.djangoapps.student import auth
from common.djangoapps.student.roles import CourseStaffRole
from common.djangoapps.student.models import CourseEnrollment, UserTestGroup
from common.djangoapps.util.cache import cache, cache_if_anonymous
from common.djangoapps.util.course import course_location_from_key
Expand Down Expand Up @@ -2261,10 +2263,22 @@ def get_learner_username(learner_identifier):
@api_view(['GET'])
def courseware_mfe_search_enabled(request, course_id=None):
"""
Simple GET endpoint to expose whether the course may use Courseware Search.
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

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
else:
enabled = True

payload = {"enabled": courseware_mfe_search_is_enabled(course_key)}
payload = {"enabled": courseware_mfe_search_is_enabled(course_key) if enabled else False}
return JsonResponse(payload)
9 changes: 9 additions & 0 deletions lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,15 @@
# .. toggle_tickets: https://github.com/openedx/edx-platform/pull/33911
'ENABLE_GRADING_METHOD_IN_PROBLEMS': False,

# .. toggle_name: FEATURES['ENABLE_COURSEWARE_SEARCH_VERIFIED_REQUIRED']
# .. toggle_implementation: DjangoSetting
# .. toggle_default: False
# .. toggle_description: When enabled, the courseware search feature will only be enabled
# for users in a verified enrollment track.
# .. toggle_use_cases: open_edx
# .. toggle_creation_date: 2024-04-24
'ENABLE_COURSEWARE_SEARCH_VERIFIED_ENROLLMENT_REQUIRED': False,

# .. toggle_name: FEATURES['ENABLE_BLAKE2B_HASHING']
# .. toggle_implementation: DjangoSetting
# .. toggle_default: False
Expand Down

0 comments on commit 3852358

Please sign in to comment.