diff --git a/cms/djangoapps/contentstore/rest_api/v1/views/tests/test_youtube_transcripts.py b/cms/djangoapps/contentstore/rest_api/v1/views/tests/test_youtube_transcripts.py index 902b1a75c753..2e35df1dc2ea 100644 --- a/cms/djangoapps/contentstore/rest_api/v1/views/tests/test_youtube_transcripts.py +++ b/cms/djangoapps/contentstore/rest_api/v1/views/tests/test_youtube_transcripts.py @@ -11,6 +11,7 @@ from organizations.tests.factories import OrganizationFactory from rest_framework import status from rest_framework.test import APIClient +from xblock_video import VideoBlock from cms.djangoapps.contentstore.rest_api.v0.views.transcripts import ( YoutubeTranscriptCheckView, @@ -23,7 +24,6 @@ from openedx.core.djangoapps.video_config.transcripts_utils import Transcript from xmodule.modulestore.django import modulestore from xmodule.modulestore.tests.factories import CourseFactory -from xmodule.video_block import VideoBlock SRT_TRANSCRIPT_CONTENT = """0 00:00:10,500 --> 00:00:13,000 diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index d09a40ba71ef..759ef4aaeff9 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -29,6 +29,7 @@ from opaque_keys.edx.keys import AssetKey, CourseKey, UsageKey from opaque_keys.edx.locations import CourseLocator from path import Path as path +from xblock_video import VideoBlock from cms.djangoapps.contentstore import toggles from cms.djangoapps.contentstore.config import waffle @@ -58,7 +59,6 @@ from xmodule.modulestore.xml_exporter import export_course_to_xml from xmodule.modulestore.xml_importer import import_course_from_xml, perform_xlint from xmodule.seq_block import SequenceBlock -from xmodule.video_block import VideoBlock TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex # noqa: UP031 diff --git a/cms/djangoapps/contentstore/views/tests/test_course_index.py b/cms/djangoapps/contentstore/views/tests/test_course_index.py index dc184885b3f6..be5d49f6a978 100644 --- a/cms/djangoapps/contentstore/views/tests/test_course_index.py +++ b/cms/djangoapps/contentstore/views/tests/test_course_index.py @@ -355,7 +355,7 @@ def test_reindex_json_responses(self): course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # noqa: PT009 - @mock.patch('xmodule.video_block.VideoBlock.index_dictionary') + @mock.patch('xblock_video.VideoBlock.index_dictionary') def test_reindex_video_error_json_responses(self, mock_index_dictionary): """ Test json response with mocked error data for video @@ -465,7 +465,7 @@ def test_indexing_responses(self): course_id=str(self.course.id)) self.assertEqual(response['total'], 1) # noqa: PT009 - @mock.patch('xmodule.video_block.VideoBlock.index_dictionary') + @mock.patch('xblock_video.VideoBlock.index_dictionary') def test_indexing_video_error_responses(self, mock_index_dictionary): """ Test do_course_reindex response with mocked error data for video diff --git a/cms/djangoapps/contentstore/views/tests/test_transcripts.py b/cms/djangoapps/contentstore/views/tests/test_transcripts.py index 8a288c6c8fbb..3100b9549f0e 100644 --- a/cms/djangoapps/contentstore/views/tests/test_transcripts.py +++ b/cms/djangoapps/contentstore/views/tests/test_transcripts.py @@ -16,6 +16,7 @@ from edxval.api import create_video from opaque_keys.edx.keys import UsageKey from organizations.tests.factories import OrganizationFactory +from xblock_video import VideoBlock # pylint: disable=wrong-import-order from cms.djangoapps.contentstore.tests.utils import CourseTestCase, setup_caption_responses from openedx.core.djangoapps.content_libraries import api as lib_api @@ -32,7 +33,6 @@ from xmodule.contentstore.django import contentstore # pylint: disable=wrong-import-order from xmodule.exceptions import NotFoundError # pylint: disable=wrong-import-order from xmodule.modulestore.django import modulestore # pylint: disable=wrong-import-order -from xmodule.video_block import VideoBlock # pylint: disable=wrong-import-order TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex # noqa: UP031 diff --git a/lms/djangoapps/course_api/blocks/tests/test_api.py b/lms/djangoapps/course_api/blocks/tests/test_api.py index 6b6c73e90bd1..b820cd5c9acd 100644 --- a/lms/djangoapps/course_api/blocks/tests/test_api.py +++ b/lms/djangoapps/course_api/blocks/tests/test_api.py @@ -149,7 +149,7 @@ def setUp(self): self.request = RequestFactory().get("/dummy") self.request.user = self.user - @patch('xmodule.video_block.VideoBlock.student_view_data') + @patch('xblock_video.VideoBlock.student_view_data') def test_video_urls_rewrite(self, video_data_patch): """ Verify the video blocks returned have their URL re-written for diff --git a/lms/djangoapps/course_api/blocks/transformers/tests/test_video_stream_priority.py b/lms/djangoapps/course_api/blocks/transformers/tests/test_video_stream_priority.py index 5573faa95616..508724907619 100644 --- a/lms/djangoapps/course_api/blocks/transformers/tests/test_video_stream_priority.py +++ b/lms/djangoapps/course_api/blocks/transformers/tests/test_video_stream_priority.py @@ -68,7 +68,7 @@ def collect_and_transform(self): @mock.patch('lms.djangoapps.course_blocks.usage_info.CourseUsageInfo') @mock.patch('openedx.core.djangoapps.waffle_utils.CourseWaffleFlag.is_enabled') - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_write_for_deprecated_youtube_flag_on(self, mock_video_data, deprecate_youtube_flag, usage_info): """ Test that video stream priority is written correctly with @@ -129,7 +129,7 @@ def test_write_for_deprecated_youtube_flag_on(self, mock_video_data, deprecate_y @mock.patch('lms.djangoapps.course_blocks.usage_info.CourseUsageInfo') @mock.patch('openedx.core.djangoapps.waffle_utils.CourseWaffleFlag.is_enabled') - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_write_for_deprecated_youtube_flag_off(self, mock_video_data, deprecate_youtube_flag, usage_info): """ Test that video stream priority is written correctly with @@ -188,7 +188,7 @@ def test_write_for_deprecated_youtube_flag_off(self, mock_video_data, deprecate_ else: assert post_transform_data[video_format] == fetched_stream_priority - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_no_priority_for_web_only_videos(self, mock_video_data): """ Verify no write attempt is made for the videos diff --git a/lms/djangoapps/course_api/blocks/transformers/tests/test_video_urls.py b/lms/djangoapps/course_api/blocks/transformers/tests/test_video_urls.py index 486a3d6c36aa..14fdec78c0d6 100644 --- a/lms/djangoapps/course_api/blocks/transformers/tests/test_video_urls.py +++ b/lms/djangoapps/course_api/blocks/transformers/tests/test_video_urls.py @@ -65,7 +65,7 @@ def collect_and_transform(self): block_structure=self.block_structure, ) - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_rewrite_for_encoded_videos(self, mock_video_data): """ Test that video URLs for videos with available encodings @@ -95,7 +95,7 @@ def test_rewrite_for_encoded_videos(self, mock_video_data): for video_format, video_url in post_transform_data.items(): assert pre_transform_data[video_format] != video_url - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_no_rewrite_for_third_party_vendor(self, mock_video_data): """ Test that video URLs aren't re-written for the videos @@ -125,7 +125,7 @@ def test_no_rewrite_for_third_party_vendor(self, mock_video_data): for video_format, video_url in post_transform_data.items(): assert pre_transform_data[video_format] == video_url - @mock.patch('xmodule.video_block.VideoBlock.student_view_data') + @mock.patch('xblock_video.VideoBlock.student_view_data') def test_no_rewrite_for_web_only_videos(self, mock_video_data): """ Verify no rewrite attempt is made for the videos diff --git a/lms/djangoapps/course_api/blocks/transformers/video_urls.py b/lms/djangoapps/course_api/blocks/transformers/video_urls.py index adab00bbfb51..82f9fe795d02 100644 --- a/lms/djangoapps/course_api/blocks/transformers/video_urls.py +++ b/lms/djangoapps/course_api/blocks/transformers/video_urls.py @@ -2,14 +2,44 @@ Video block URL Transformer """ +import logging +from urllib.parse import urlparse from django.conf import settings +from django.core.exceptions import ValidationError +from django.core.validators import URLValidator from openedx.core.djangoapps.content.block_structure.transformer import BlockStructureTransformer -from xmodule.video_block.video_utils import rewrite_video_url # pylint: disable=wrong-import-order from .student_view import StudentViewTransformer +log = logging.getLogger(__name__) + + +def rewrite_video_url(cdn_base_url, original_video_url): + """ + Returns a re-written video URL for cases when an alternate source + has been configured and is selected using factors like user location. + + :param cdn_base_url: The scheme, hostname, port and any relevant path prefix for the alternate CDN. + :param original_video_url: The canonical source for this video. + :return: The re-written URL, or None if the result is not a valid URL. + """ + if (not cdn_base_url) or (not original_video_url): + return None + + parsed = urlparse(original_video_url) + rewritten_url = cdn_base_url.rstrip("/") + "/" + parsed.path.lstrip("/") + validator = URLValidator() + + try: + validator(rewritten_url) + return rewritten_url + except ValidationError: + log.warning("Invalid CDN rewrite URL encountered, %s", rewritten_url) + + return None + class VideoBlockURLTransformer(BlockStructureTransformer): """ diff --git a/lms/djangoapps/courseware/tests/test_block_render.py b/lms/djangoapps/courseware/tests/test_block_render.py index 409b50a2164a..a9ddfa4490f0 100644 --- a/lms/djangoapps/courseware/tests/test_block_render.py +++ b/lms/djangoapps/courseware/tests/test_block_render.py @@ -55,6 +55,7 @@ from xblock_problem.capa.testing.response_xml_factory import ( OptionResponseXMLFactory, # pylint: disable=reimported ) +from xblock_video import VideoBlock # pylint: disable=wrong-import-order from common.djangoapps.course_modes.models import CourseMode # pylint: disable=reimported from common.djangoapps.student.models import CourseEnrollment, anonymous_id_for_user @@ -116,7 +117,6 @@ ) from xmodule.modulestore.tests.test_asides import AsideTestType # pylint: disable=wrong-import-order from xmodule.services import RebindUserServiceError -from xmodule.video_block import VideoBlock # pylint: disable=wrong-import-order from xmodule.x_module import STUDENT_VIEW, ModuleStoreRuntime # pylint: disable=wrong-import-order TEST_DATA_DIR = settings.COMMON_TEST_DATA_ROOT diff --git a/lms/djangoapps/courseware/tests/test_video_handlers.py b/lms/djangoapps/courseware/tests/test_video_handlers.py index 95d8bc607e4c..17bc6cdda70e 100644 --- a/lms/djangoapps/courseware/tests/test_video_handlers.py +++ b/lms/djangoapps/courseware/tests/test_video_handlers.py @@ -11,13 +11,13 @@ import ddt import freezegun import pytest -from django.conf import settings from django.core.files.base import ContentFile from django.test import RequestFactory from django.utils.timezone import now from edxval import api from webob import Request, Response from xblock.django.request import DjangoWebobRequest +from xblock_video import VideoBlock from common.djangoapps.student.tests.factories import UserFactory from common.test.utils import normalize_repr @@ -36,7 +36,6 @@ # noinspection PyUnresolvedReferences from xmodule.tests.helpers import override_descriptor_system # pylint: disable=unused-import # noqa: F401 -from xmodule.video_block import VideoBlock # pylint: disable=wrong-import-order from xmodule.x_module import STUDENT_VIEW from .helpers import BaseTestXmodule @@ -207,17 +206,11 @@ def test_handle_ajax(self): {'demoo�': 'sample'} ] for sample in data: - if settings.USE_EXTRACTED_VIDEO_BLOCK: - handler_url = self.get_url('save_user_state', handler_name='ajax_handler') - response = self.clients[self.users[0].username].post( - handler_url, - sample, - HTTP_X_REQUESTED_WITH='XMLHttpRequest') - else: - response = self.clients[self.users[0].username].post( - self.get_url('save_user_state'), - sample, - HTTP_X_REQUESTED_WITH='XMLHttpRequest') + handler_url = self.get_url('save_user_state', handler_name='ajax_handler') + response = self.clients[self.users[0].username].post( + handler_url, + sample, + HTTP_X_REQUESTED_WITH='XMLHttpRequest') assert response.status_code == 200 assert self.block.speed is None @@ -800,7 +793,7 @@ def test_translation_static_transcript(self, url, dispatch, status_code, sub=Non if sub: assert ('Location', f'/static/dummy/static/subs_{sub}.srt.sjson') in response.headerlist - @patch('xmodule.video_block.VideoBlock.context_key', return_value='not_a_course_locator') + @patch('xblock_video.VideoBlock.context_key', return_value='not_a_course_locator') def test_translation_static_non_course(self, __): # noqa: PT019 """ Test that get_static_transcript short-circuits in the case of a non-CourseLocator. @@ -822,7 +815,7 @@ def _set_static_asset_path(self): store.update_item(self.course, self.user.id) @patch('openedx.core.djangoapps.video_config.transcripts_utils.edxval_api.get_video_transcript_data') - @patch('xmodule.video_block.VideoBlock.get_static_transcript', Mock(return_value=Response(status=404))) + @patch('xblock_video.VideoBlock.get_static_transcript', Mock(return_value=Response(status=404))) def test_translation_fallback_transcript(self, mock_get_video_transcript_data): """ Verify that the val transcript is returned as a fallback, @@ -854,7 +847,7 @@ def test_translation_fallback_transcript(self, mock_get_video_transcript_data): for attribute, value in expected_headers.items(): assert response.headers[attribute] == value - @patch('xmodule.video_block.VideoBlock.get_static_transcript', Mock(return_value=Response(status=404))) + @patch('xblock_video.VideoBlock.get_static_transcript', Mock(return_value=Response(status=404))) def test_translation_fallback_transcript_feature_disabled(self): """ Verify that val transcript is not returned when its feature is disabled. diff --git a/lms/djangoapps/courseware/tests/test_video_mongo.py b/lms/djangoapps/courseware/tests/test_video_mongo.py index b04e56f05a3d..438e26d8a629 100644 --- a/lms/djangoapps/courseware/tests/test_video_mongo.py +++ b/lms/djangoapps/courseware/tests/test_video_mongo.py @@ -5,6 +5,7 @@ import json import shutil +import unittest from collections import OrderedDict from contextlib import contextmanager from tempfile import mkdtemp @@ -35,8 +36,12 @@ from fs.osfs import OSFS from fs.path import combine from lxml import etree +from opaque_keys.edx.locator import CourseLocator from path import Path as path -from xblock_video import bumper_utils +from xblock.field_data import DictFieldData +from xblock.fields import ScopeIds +from xblock_video import VideoBlock, bumper_utils, video_utils +from xblock_video.block import EXPORT_IMPORT_COURSE_DIR, EXPORT_IMPORT_STATIC_DIR from common.djangoapps.xblock_django.constants import ATTR_KEY_REQUEST_COUNTRY_CODE from common.test.utils import assert_dict_contains_subset @@ -56,6 +61,7 @@ from xmodule.exceptions import NotFoundError from xmodule.modulestore.inheritance import own_metadata from xmodule.modulestore.tests.django_utils import TEST_DATA_SPLIT_MODULESTORE +from xmodule.tests import get_test_descriptor_system # noinspection PyUnresolvedReferences from xmodule.tests.helpers import ( # pylint: disable=unused-import # noqa: F401 @@ -63,9 +69,6 @@ override_descriptor_system, ) from xmodule.tests.test_import import DummyModuleStoreRuntime -from xmodule.tests.test_video import VideoBlockTestBase -from xmodule.video_block import VideoBlock, video_utils -from xmodule.video_block.video_block import EXPORT_IMPORT_COURSE_DIR, EXPORT_IMPORT_STATIC_DIR from xmodule.x_module import PUBLIC_VIEW, STUDENT_VIEW from .test_video_handlers import BaseTestVideoXBlock, TestVideo @@ -84,6 +87,55 @@ TRANSCRIPT_FILE_SJSON_DATA = """{\n "start": [10],\n "end": [100],\n "text": ["Hi, welcome to edxval."]\n}""" +def instantiate_block(**field_data): + """ + Instantiate block with most properties. + + Moved from the deleted xmodule/tests/test_video.py; now builds the + xblock_video VideoBlock instead of the removed built-in block. + """ + if field_data.get('data', None): + field_data = VideoBlock.parse_video_xml(field_data['data']) + system = get_test_descriptor_system() + course_key = CourseLocator('org', 'course', 'run') + usage_key = course_key.make_usage_key('video', 'SampleProblem') + return system.construct_xblock_from_class( + VideoBlock, + scope_ids=ScopeIds(None, None, usage_key, usage_key), + field_data=DictFieldData(field_data), + ) + + +class VideoBlockTestBase(unittest.TestCase): + """ + Base class for tests for VideoBlock + + Moved from the deleted xmodule/tests/test_video.py. + """ + + def setUp(self): + super().setUp() + self.block = instantiate_block() + + def assertXmlEqual(self, expected, xml): + """ + Assert that the given XML fragments have the same attributes, text, and + (recursively) children + """ + def get_child_tags(elem): + """Extract the list of tag names for children of elem""" + return [child.tag for child in elem] + + for attr in ['tag', 'attrib', 'text', 'tail']: + expected_attr = getattr(expected, attr) + actual_attr = getattr(xml, attr) + assert expected_attr == actual_attr + + assert get_child_tags(expected) == get_child_tags(xml) + for left, right in zip(expected, xml): # noqa: B905 + self.assertXmlEqual(left, right) + + class TestVideoYouTube(TestVideo): # pylint: disable=missing-class-docstring, test-inherits-tests METADATA = {} @@ -1169,7 +1221,7 @@ def test_get_html_on_toggling_hls_feature(self, hls_feature_enabled, expected_va """ Verify val profiles on toggling HLS Playback feature. """ - with patch('xmodule.video_block.video_block.edxval_api.get_urls_for_profiles') as get_urls_for_profiles: + with patch('edxval.api.get_urls_for_profiles') as get_urls_for_profiles: get_urls_for_profiles.return_value = { 'desktop_webm': 'https://webm.com/dw.webm', 'hls': 'https://hls.com/hls.m3u8', @@ -1193,7 +1245,7 @@ def test_get_html_on_toggling_hls_feature(self, hls_feature_enabled, expected_va 'openedx.core.djangoapps.video_config.services.VideoConfigService.is_hls_playback_enabled', Mock(return_value=True) ) - @patch('xmodule.video_block.video_block.edxval_api.get_urls_for_profiles') + @patch('edxval.api.get_urls_for_profiles') def test_get_html_hls(self, get_urls_for_profiles, mock_render_django_template): """ Verify that hls profile functionality works as expected. @@ -1280,7 +1332,7 @@ def test_html_student_public_view(self, _): # noqa: PT019 assert '"saveStateEnabled": false' in context @patch('xblock.utils.resources.ResourceLoader.render_django_template', side_effect=mock_render_template) - @patch('xmodule.video_block.video_block.edxval_api.get_course_video_image_url') + @patch('edxval.api.get_course_video_image_url') def test_poster_image(self, get_course_video_image_url, _): # noqa: PT019 """ Verify that poster image functionality works as expected. @@ -1294,7 +1346,7 @@ def test_poster_image(self, get_course_video_image_url, _): # noqa: PT019 assert '"poster": "/media/video-images/poster.png"' in context @patch('xblock.utils.resources.ResourceLoader.render_django_template', side_effect=mock_render_template) - @patch('xmodule.video_block.video_block.edxval_api.get_course_video_image_url') + @patch('edxval.api.get_course_video_image_url') def test_poster_image_without_edx_video_id(self, get_course_video_image_url, _): # noqa: PT019 """ Verify that poster image is set to None and there is no crash when no edx_video_id. @@ -1440,7 +1492,7 @@ def test_val_encoding_in_context(self, val_video_encodings, video_url): a video component is initialized. Current implementation considers this youtube source as a valid external youtube source. """ - with patch('xmodule.video_block.video_block.edxval_api.get_urls_for_profiles') as get_urls_for_profiles: + with patch('edxval.api.get_urls_for_profiles') as get_urls_for_profiles: get_urls_for_profiles.return_value = val_video_encodings self.initialize_block( data='' @@ -1480,7 +1532,7 @@ def test_val_encoding_in_context_without_external_youtube_source(self, val_video Accepted order of source priority is: VAL's youtube source > external youtube source > hls > mp4 > webm. """ - with patch('xmodule.video_block.video_block.edxval_api.get_urls_for_profiles') as get_urls_for_profiles: + with patch('edxval.api.get_urls_for_profiles') as get_urls_for_profiles: get_urls_for_profiles.return_value = val_video_encodings # pylint: disable=line-too-long self.initialize_block( @@ -1571,7 +1623,7 @@ def test_editor_saved_with_unstripped_video_id(self): item.editor_saved(self.user, old_metadata, None) assert item.edx_video_id == stripped_video_id - @patch('xmodule.video_block.video_block.edxval_api.get_url_for_profile', Mock(return_value='test_yt_id')) + @patch('edxval.api.get_url_for_profile', Mock(return_value='test_yt_id')) def test_editor_saved_with_yt_val_profile(self): """ Verify editor saved overrides `youtube_id_1_0` when a youtube val profile is there diff --git a/lms/djangoapps/courseware/tests/test_video_xml.py b/lms/djangoapps/courseware/tests/test_video_xml.py index 22276575b3df..7c81c7ff4e2e 100644 --- a/lms/djangoapps/courseware/tests/test_video_xml.py +++ b/lms/djangoapps/courseware/tests/test_video_xml.py @@ -16,8 +16,7 @@ from django.test import TestCase - -from xmodule.video_block import VideoBlock +from xblock_video import VideoBlock SOURCE_XML = """