diff --git a/cms/djangoapps/contentstore/views/tests/test_transcripts.py b/cms/djangoapps/contentstore/views/tests/test_transcripts.py index 8a288c6c8fbb..2d54e5639538 100644 --- a/cms/djangoapps/contentstore/views/tests/test_transcripts.py +++ b/cms/djangoapps/contentstore/views/tests/test_transcripts.py @@ -15,9 +15,11 @@ from django.urls import reverse from edxval.api import create_video from opaque_keys.edx.keys import UsageKey +from openedx_authz.authz import COURSE_AUDITOR, COURSE_EDITOR from organizations.tests.factories import OrganizationFactory from cms.djangoapps.contentstore.tests.utils import CourseTestCase, setup_caption_responses +from openedx.core.djangoapps.authz.tests.mixins import CourseAuthoringAuthzTestMixin from openedx.core.djangoapps.content_libraries import api as lib_api from openedx.core.djangoapps.contentserver.caching import del_cached_content from openedx.core.djangoapps.video_config.transcripts_utils import ( # pylint: disable=wrong-import-order @@ -1173,3 +1175,51 @@ def test_command_for_fallback_transcript(self, mock_get_video_transcript_content 'html5_equal': False, } ) + + +@ddt.ddt +class TestTranscriptsAuthz(CourseAuthoringAuthzTestMixin, BaseTranscripts): + """ + Tests that the '/transcripts/*' views require ``courses.edit_course_content``, + with the legacy studio write access as fallback when AuthZ is disabled. + """ + + def _get_as(self, user, url_name): + """ + Call a transcripts view on the test video as ``user`` and return the response. + """ + self.client.login(username=user.username, password=self.password) + data = { + 'locator': str(self.video_usage_key), + 'videos': [{'type': 'html5', 'video': 'video1', 'mode': 'mp4'}], + } + return self.client.get(reverse(url_name), {'data': json.dumps(data)}) + + def test_editor_can_check_transcripts(self): + self.add_user_to_role_in_course(self.authorized_user, COURSE_EDITOR.external_key, self.course.id) + + response = self._get_as(self.authorized_user, 'check_transcripts') + + assert response.status_code == 200 + + @ddt.data('check_transcripts', 'replace_transcripts') + def test_auditor_cannot_check_or_import_transcripts(self, url_name): + self.add_user_to_role_in_course(self.authorized_user, COURSE_AUDITOR.external_key, self.course.id) + + response = self._get_as(self.authorized_user, url_name) + + assert response.status_code == 403 + + def test_legacy_user_without_write_access_cannot_check_transcripts(self): + self.set_authz_enabled(False) + + response = self._get_as(self.unauthorized_user, 'check_transcripts') + + assert response.status_code == 403 + + def test_legacy_staff_can_check_transcripts_when_authz_disabled(self): + self.set_authz_enabled(False) + + response = self._get_as(self.staff_user, 'check_transcripts') + + assert response.status_code == 200 diff --git a/cms/djangoapps/contentstore/views/transcripts_ajax.py b/cms/djangoapps/contentstore/views/transcripts_ajax.py index ebad3d6c7f9a..85080e31c379 100644 --- a/cms/djangoapps/contentstore/views/transcripts_ajax.py +++ b/cms/djangoapps/contentstore/views/transcripts_ajax.py @@ -21,11 +21,13 @@ from opaque_keys import InvalidKeyError from opaque_keys.edx.keys import UsageKey, UsageKeyV2 from opaque_keys.edx.locator import LibraryLocatorV2 +from openedx_authz.authz import COURSES_EDIT_COURSE_CONTENT from xblock_video.exceptions import TranscriptsGenerationException from cms.djangoapps.contentstore.video_storage_handlers import TranscriptProvider -from common.djangoapps.student.auth import has_course_author_access from common.djangoapps.util.json_request import JsonResponse +from openedx.core.djangoapps.authz.constants import LegacyAuthoringPermission +from openedx.core.djangoapps.authz.decorators import user_has_course_permission from openedx.core.djangoapps.content_libraries import api as lib_api from openedx.core.djangoapps.video_config.transcripts_utils import ( # pylint: disable=wrong-import-order GetTranscriptsFromYouTubeException, @@ -772,12 +774,17 @@ def _get_item(request, data): check_permission=CheckPerm.CAN_EDIT, ) raise TranscriptsRequestValidationException(_('Transcripts are not yet supported for this type of block')) - # This is placed before has_course_author_access() to validate the location, - # because has_course_author_access() raises error if location is invalid. + # This is placed before the permission check to validate the location, + # because the check raises an error if the location is invalid. item = modulestore().get_item(usage_key) # use the item's course_key, because the usage_key might not have the run - if not has_course_author_access(request.user, item.location.course_key): + if not user_has_course_permission( + request.user, + COURSES_EDIT_COURSE_CONTENT.identifier, + item.location.course_key, + LegacyAuthoringPermission.WRITE, + ): raise PermissionDenied() return item