Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions openedx/core/djangoapps/courseware_api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,55 @@ def test_learning_assistant_enabled(self, setting_enabled):
self.assertEqual(learning_assistant_enabled, setting_enabled) # noqa: PT009


@skip_unless_lms
@override_settings(COURSE_ABOUT_VISIBILITY_PERMISSION='see_about_page')
class CourseApiCatalogVisibilityNoneTests(MasqueradeMixin, SharedModuleStoreTestCase):
"""
The courseware API on a course whose about page is hidden (catalog_visibility='none')
must answer with JSON, never with a redirect to the dashboard.
"""
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.course = ToyCourseFactory.create(catalog_visibility='none', emit_signals=True)
cls.url = f'/api/courseware/course/{cls.course.id}'

def setUp(self):
super().setUp()
self.learner = UserFactory(password='foo')
CourseEnrollment.enroll(self.learner, self.course.id, CourseMode.AUDIT)
self.instructor = UserFactory(password='foo')
CourseInstructorRole(self.course.id).add_users(self.instructor)

def assert_catalog_visibility_403(self, response):
assert response.status_code == 403
assert 'not currently accessible' in response.json()['detail']

def test_enrolled_learner_gets_403(self):
self.client.login(username=self.learner.username, password='foo')
# DRF marks the test's wrapping transaction for rollback on the 403, which breaks
# the session save in middleware. The view itself runs non-atomic in production.
with mock.patch('rest_framework.views.set_rollback'):
response = self.client.get(self.url)
self.assert_catalog_visibility_403(response)

def test_anonymous_user_gets_403(self):
self.assert_catalog_visibility_403(self.client.get(self.url))

def test_course_staff_gets_200(self):
self.client.login(username=self.instructor.username, password='foo')
response = self.client.get(self.url)
assert response.status_code == 200
assert response.json()['show_courseware_link'] is True

def test_course_staff_masquerading_as_learner_gets_200(self):
self.client.login(username=self.instructor.username, password='foo')
self.update_masquerade(course=self.course, role='student')
response = self.client.get(self.url)
assert response.status_code == 200
assert response.json()['show_courseware_link'] is False


@ddt.ddt
class SequenceApiTestViews(MasqueradeMixin, BaseCoursewareTests):
"""
Expand Down
31 changes: 24 additions & 7 deletions openedx/core/djangoapps/courseware_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey, UsageKey
from rest_framework import status
from rest_framework.exceptions import NotFound
from rest_framework.exceptions import NotFound, PermissionDenied
from rest_framework.generics import RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -41,7 +41,9 @@
get_course_with_access,
get_permission_for_course_about,
)
from lms.djangoapps.courseware.courseware_access_exception import CoursewareAccessException
from lms.djangoapps.courseware.entrance_exams import course_has_entrance_exam, user_has_passed_entrance_exam
from lms.djangoapps.courseware.exceptions import CourseAccessRedirect
from lms.djangoapps.courseware.masquerade import (
is_masquerading_as_non_audit_enrollment,
is_masquerading_as_specific_student,
Expand Down Expand Up @@ -388,7 +390,12 @@ def show_courseware_link(self):
"""
with modulestore().bulk_operations(self.course_key):
permission = get_permission_for_course_about()
course_with_access = get_course_with_access(self.request.user, permission, self.course_key)
try:
course_with_access = get_course_with_access(self.request.user, permission, self.course_key)
except (CourseAccessRedirect, CoursewareAccessException):
# The (possibly masqueraded) user can't see the about page, so there is no
# link to show. Don't let the redirect escape and turn this API into a 302.
return False
return bool(
self.request.user.has_perm(VIEW_COURSEWARE, course_with_access)
or settings.ENABLE_LMS_MIGRATION
Expand Down Expand Up @@ -667,6 +674,7 @@ class CoursewareInformation(RetrieveAPIView):
for an authenticated request.
* 403 if a user who does not have permission to masquerade as
another user specifies a username other than their own.
* 403 if the course's catalog visibility hides it from the user.
* 404 if the course is not available or cannot be seen.
"""

Expand Down Expand Up @@ -708,11 +716,20 @@ def get_object(self):
else:
username = self.request.user.username
course_key = CourseKey.from_string(self.kwargs['course_key_string'])
overview = CoursewareMeta(
course_key,
self.request,
username=username,
)
try:
overview = CoursewareMeta(
course_key,
self.request,
username=username,
)
except CourseAccessRedirect as exc:
# Answer with a 403 instead of redirecting the API call to the dashboard,
# matching the course home metadata API.
error = exc.access_error
raise PermissionDenied(
detail=error.user_message if error is not None else 'You do not have access to this course.',
code=error.error_code if error is not None else None,
) from exc
# Record course goals user activity for learning mfe courseware on web
UserActivity.record_user_activity(self.request.user, course_key)

Expand Down
Loading