Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cms/djangoapps/contentstore/tests/test_contentstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cms/djangoapps/contentstore/views/tests/test_course_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/course_api/blocks/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 31 additions & 1 deletion lms/djangoapps/course_api/blocks/transformers/video_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
2 changes: 1 addition & 1 deletion lms/djangoapps/courseware/tests/test_block_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 9 additions & 16 deletions lms/djangoapps/courseware/tests/test_video_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Loading
Loading