"""Tests for items views.""" import copy import json import tempfile import textwrap from codecs import BOM_UTF8 from unittest.mock import Mock, patch from uuid import uuid4 import ddt from django.conf import settings from django.test.utils import override_settings from django.urls import reverse from edxval.api import create_video from opaque_keys.edx.keys import UsageKey from organizations.tests.factories import OrganizationFactory from cms.djangoapps.contentstore.tests.utils import CourseTestCase, setup_caption_responses from openedx.core.djangoapps.contentserver.caching import del_cached_content from openedx.core.djangoapps.content_libraries import api as lib_api from xmodule.contentstore.content import StaticContent # lint-amnesty, pylint: disable=wrong-import-order from xmodule.contentstore.django import contentstore # lint-amnesty, pylint: disable=wrong-import-order from xmodule.exceptions import NotFoundError # lint-amnesty, pylint: disable=wrong-import-order from xmodule.modulestore.django import modulestore # lint-amnesty, pylint: disable=wrong-import-order from xmodule.video_block import VideoBlock # lint-amnesty, pylint: disable=wrong-import-order from xmodule.video_block.transcripts_utils import ( # lint-amnesty, pylint: disable=wrong-import-order GetTranscriptsFromYouTubeException, Transcript, get_video_transcript_content, get_transcript, remove_subs_from_store, ) from openedx.core.djangoapps.xblock import api as xblock_api TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex SRT_TRANSCRIPT_CONTENT = """0 00:00:10,500 --> 00:00:13,000 Elephant's Dream 1 00:00:15,000 --> 00:00:18,000 At the left we can see... """ SJSON_TRANSCRIPT_CONTENT = Transcript.convert( SRT_TRANSCRIPT_CONTENT, Transcript.SRT, Transcript.SJSON, ) @override_settings(CONTENTSTORE=TEST_DATA_CONTENTSTORE) class BaseTranscripts(CourseTestCase): """Base test class for transcripts tests.""" def clear_subs_content(self): """Remove, if transcripts content exists.""" for youtube_id in self.get_youtube_ids().values(): filename = f'subs_{youtube_id}.srt.sjson' content_location = StaticContent.compute_location(self.course.id, filename) try: content = contentstore().find(content_location) contentstore().delete(content.get_id()) except NotFoundError: pass def save_subs_to_store(self, subs, subs_id): """ Save transcripts into `StaticContent`. """ filedata = json.dumps(subs, indent=2) mime_type = 'application/json' filename = f'subs_{subs_id}.srt.sjson' content_location = StaticContent.compute_location(self.course.id, filename) content = StaticContent(content_location, filename, mime_type, filedata) contentstore().save(content) del_cached_content(content_location) return content_location def setUp(self): """Create initial data.""" super().setUp() # Add video block data = { 'parent_locator': str(self.course.location), 'category': 'video', 'type': 'video' } resp = self.client.ajax_post('/xblock/', data) self.assertEqual(resp.status_code, 200) self.library = lib_api.create_library( org=OrganizationFactory.create(short_name="org1"), slug="lib", title="Library", ) self.library_block_metadata = lib_api.create_library_block( self.library.key, "video", "video-transcript", ) self.library_block = xblock_api.load_block( self.library_block_metadata.usage_key, self.user, ) self.video_usage_key = self._get_usage_key(resp) self.item = modulestore().get_item(self.video_usage_key) # hI10vDNYz4M - valid Youtube ID with transcripts. # JMD_ifUUfsU, AKqURZnYqpk, DYpADpL7jAY - valid Youtube IDs without transcripts. self.set_fields_from_xml( self.item, '