"""Tests for items views.""" import copy import ddt import json import os import tempfile import textwrap from uuid import uuid4 from django.conf import settings from django.core.urlresolvers import reverse from django.test.utils import override_settings from mock import patch, Mock from opaque_keys.edx.keys import UsageKey from contentstore.tests.utils import CourseTestCase, mock_requests_get from openedx.core.djangoapps.contentserver.caching import del_cached_content from xmodule.contentstore.content import StaticContent from xmodule.contentstore.django import contentstore from xmodule.exceptions import NotFoundError from xmodule.modulestore.django import modulestore from xmodule.video_module import transcripts_utils TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex @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 = 'subs_{0}.srt.sjson'.format(youtube_id) 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 = 'subs_{0}.srt.sjson'.format(subs_id) 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(BaseTranscripts, self).setUp() # Add video module data = { 'parent_locator': unicode(self.course.location), 'category': 'video', 'type': 'video' } resp = self.client.ajax_post('/xblock/', data) self.assertEqual(resp.status_code, 200) 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.item.data = '