diff --git a/cms/djangoapps/contentstore/tests/test_transcripts_utils.py b/cms/djangoapps/contentstore/tests/test_transcripts_utils.py index 82d7a2e91c..50d7575e25 100644 --- a/cms/djangoapps/contentstore/tests/test_transcripts_utils.py +++ b/cms/djangoapps/contentstore/tests/test_transcripts_utils.py @@ -18,6 +18,7 @@ from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase from xmodule.exceptions import NotFoundError from xmodule.contentstore.django import contentstore from xmodule.video_module import transcripts_utils +from contentstore.tests.utils import mock_requests_get TEST_DATA_CONTENTSTORE = copy.deepcopy(settings.CONTENTSTORE) TEST_DATA_CONTENTSTORE['DOC_STORE_CONFIG']['db'] = 'test_xcontent_%s' % uuid4().hex @@ -321,36 +322,7 @@ class TestDownloadYoutubeSubs(ModuleStoreTestCase): transcript_name = transcripts_utils.youtube_video_transcript_name(youtube_text_api) self.assertIsNone(transcript_name) - def mocked_requests_get(*args, **kwargs): - """ - This method will be used by the mock to replace requests.get - """ - # pylint: disable=no-method-argument - response_transcript_list = """ - - - - - """ - response_transcript = textwrap.dedent(""" - - - Test text 1. - Test text 2. - Test text 3. - - """) - - if kwargs == {'params': {'lang': 'en', 'v': 'good_id_2'}}: - return Mock(status_code=200, text='') - elif kwargs == {'params': {'type': 'list', 'v': 'good_id_2'}}: - return Mock(status_code=200, text=response_transcript_list, content=response_transcript_list) - elif kwargs == {'params': {'lang': 'en', 'v': 'good_id_2', 'name': 'Custom'}}: - return Mock(status_code=200, text=response_transcript, content=response_transcript) - - return Mock(status_code=404, text='') - - @patch('xmodule.video_module.transcripts_utils.requests.get', side_effect=mocked_requests_get) + @patch('xmodule.video_module.transcripts_utils.requests.get', side_effect=mock_requests_get) def test_downloading_subs_using_transcript_name(self, mock_get): """ Download transcript using transcript name in url diff --git a/cms/djangoapps/contentstore/tests/utils.py b/cms/djangoapps/contentstore/tests/utils.py index 551f5ef17a..73f3541441 100644 --- a/cms/djangoapps/contentstore/tests/utils.py +++ b/cms/djangoapps/contentstore/tests/utils.py @@ -3,6 +3,8 @@ Utilities for contentstore tests ''' import json +import textwrap +from mock import Mock from django.conf import settings from django.contrib.auth.models import User @@ -375,6 +377,35 @@ class CourseTestCase(ModuleStoreTestCase): self.assertEqual(value, course2_asset_attrs[key]) +def mock_requests_get(*args, **kwargs): + """ + Returns mock responses for the youtube API. + """ + # pylint: disable=unused-argument + response_transcript_list = """ + + + + + """ + response_transcript = textwrap.dedent(""" + + subs #1 + subs #2 + subs #3 + + """) + + if kwargs == {'params': {'lang': 'en', 'v': 'good_id_2'}}: + return Mock(status_code=200, text='') + elif kwargs == {'params': {'type': 'list', 'v': 'good_id_2'}}: + return Mock(status_code=200, text=response_transcript_list, content=response_transcript_list) + elif kwargs == {'params': {'lang': 'en', 'v': 'good_id_2', 'name': 'Custom'}}: + return Mock(status_code=200, text=response_transcript, content=response_transcript) + + return Mock(status_code=404, text='') + + def get_url(handler_name, key_value, key_name='usage_key_string', kwargs=None): """ Helper function for getting HTML for a page in Studio and checking that it does not error. diff --git a/cms/djangoapps/contentstore/views/tests/test_transcripts.py b/cms/djangoapps/contentstore/views/tests/test_transcripts.py index 0168cac58b..863f8f6b23 100644 --- a/cms/djangoapps/contentstore/views/tests/test_transcripts.py +++ b/cms/djangoapps/contentstore/views/tests/test_transcripts.py @@ -6,12 +6,13 @@ import os import tempfile import textwrap from uuid import uuid4 +from mock import patch from django.core.urlresolvers import reverse from django.test.utils import override_settings from django.conf import settings -from contentstore.tests.utils import CourseTestCase +from contentstore.tests.utils import CourseTestCase, mock_requests_get from cache_toolbox.core import del_cached_content from xmodule.modulestore.django import modulestore from xmodule.contentstore.django import contentstore @@ -628,6 +629,58 @@ class TestCheckTranscripts(BaseTranscripts): } ) + @patch('xmodule.video_module.transcripts_utils.requests.get', side_effect=mock_requests_get) + def test_check_youtube_with_transcript_name(self, mock_get): + """ + Test that the transcripts are fetched correctly when the the transcript name is set + """ + self.item.data = '