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 = ''
+ modulestore().update_item(self.item, self.user.id)
+
+ subs = {
+ 'start': [100, 200, 240],
+ 'end': [200, 240, 380],
+ 'text': [
+ 'subs #1',
+ 'subs #2',
+ 'subs #3'
+ ]
+ }
+ self.save_subs_to_store(subs, 'good_id_2')
+ link = reverse('check_transcripts')
+ data = {
+ 'locator': unicode(self.video_usage_key),
+ 'videos': [{
+ 'type': 'youtube',
+ 'video': 'good_id_2',
+ 'mode': 'youtube',
+ }]
+ }
+ resp = self.client.get(link, {'data': json.dumps(data)})
+
+ mock_get.assert_any_call(
+ 'http://video.google.com/timedtext',
+ params={'lang': 'en', 'v': 'good_id_2', 'name': 'Custom'}
+ )
+
+ self.assertEqual(resp.status_code, 200)
+
+ self.assertDictEqual(
+ json.loads(resp.content),
+ {
+ u'status': u'Success',
+ u'subs': u'good_id_2',
+ u'youtube_local': True,
+ u'is_youtube_mode': True,
+ u'youtube_server': True,
+ u'command': u'replace',
+ u'current_item_subs': None,
+ u'youtube_diff': True,
+ u'html5_local': [],
+ u'html5_equal': False,
+ }
+ )
+
def test_fail_data_without_id(self):
link = reverse('check_transcripts')
data = {
diff --git a/cms/djangoapps/contentstore/views/transcripts_ajax.py b/cms/djangoapps/contentstore/views/transcripts_ajax.py
index da0eceac0c..0eecd909a0 100644
--- a/cms/djangoapps/contentstore/views/transcripts_ajax.py
+++ b/cms/djangoapps/contentstore/views/transcripts_ajax.py
@@ -35,7 +35,8 @@ from xmodule.video_module.transcripts_utils import (
copy_or_rename_transcript,
manage_video_subtitles_save,
GetTranscriptsFromYouTubeException,
- TranscriptsRequestValidationException
+ TranscriptsRequestValidationException,
+ youtube_video_transcript_name,
)
from student.auth import has_course_author_access
@@ -251,6 +252,9 @@ def check_transcripts(request):
# youtube server
youtube_text_api = copy.deepcopy(settings.YOUTUBE['TEXT_API'])
youtube_text_api['params']['v'] = youtube_id
+ youtube_transcript_name = youtube_video_transcript_name(youtube_text_api)
+ if youtube_transcript_name:
+ youtube_text_api['params']['name'] = youtube_transcript_name
youtube_response = requests.get('http://' + youtube_text_api['url'], params=youtube_text_api['params'])
if youtube_response.status_code == 200 and youtube_response.text: