diff --git a/cms/djangoapps/contentstore/features/video.py b/cms/djangoapps/contentstore/features/video.py index a19d7c2e67..9e7b2f3151 100644 --- a/cms/djangoapps/contentstore/features/video.py +++ b/cms/djangoapps/contentstore/features/video.py @@ -37,8 +37,6 @@ def configure_youtube_api(_step, action): @step('I have created a Video component$') def i_created_a_video_component(_step): - assert_less(world.youtube.config['youtube_api_response'].status_code, 400, "Real Youtube server is unavailable") - world.create_course_with_unit() world.create_component_instance( step=_step, diff --git a/cms/envs/bok_choy.py b/cms/envs/bok_choy.py index a3411dc7a8..526f404d70 100644 --- a/cms/envs/bok_choy.py +++ b/cms/envs/bok_choy.py @@ -49,3 +49,9 @@ FEATURES['AUTOMATIC_AUTH_FOR_TESTING'] = True # Unfortunately, we need to use debug mode to serve staticfiles DEBUG = True + +# Point the URL used to test YouTube availability to our stub YouTube server +YOUTUBE_PORT = 9080 +YOUTUBE['API'] = "127.0.0.1:{0}/get_youtube_api/".format(YOUTUBE_PORT) +YOUTUBE['TEST_URL'] = "127.0.0.1:{0}/test_youtube/".format(YOUTUBE_PORT) +YOUTUBE['TEXT_API']['url'] = "127.0.0.1:{0}/test_transcripts_youtube/".format(YOUTUBE_PORT) diff --git a/common/djangoapps/terrain/start_stubs.py b/common/djangoapps/terrain/start_stubs.py index a4dd93ad4c..f1ce715bcd 100644 --- a/common/djangoapps/terrain/start_stubs.py +++ b/common/djangoapps/terrain/start_stubs.py @@ -1,7 +1,6 @@ """ Initialize and teardown stub and video HTTP services for use in acceptance tests. """ -import requests from lettuce import before, after, world from django.conf import settings from terrain.stubs.youtube import StubYouTubeService @@ -16,8 +15,6 @@ SERVICES = { "lti": {"port": settings.LTI_PORT, "class": StubLtiService}, } -YOUTUBE_API_RESPONSE = requests.get('http://www.youtube.com/iframe_api') - @before.all # pylint: disable=E1101 def start_video_server(): @@ -49,8 +46,6 @@ def start_stubs(_scenario): """ for name, service in SERVICES.iteritems(): fake_server = service['class'](port_num=service['port']) - if name == 'youtube': - fake_server.config['youtube_api_response'] = YOUTUBE_API_RESPONSE setattr(world, name, fake_server) diff --git a/common/djangoapps/terrain/stubs/tests/test_youtube_stub.py b/common/djangoapps/terrain/stubs/tests/test_youtube_stub.py index e2603a82fe..aded4808f0 100644 --- a/common/djangoapps/terrain/stubs/tests/test_youtube_stub.py +++ b/common/djangoapps/terrain/stubs/tests/test_youtube_stub.py @@ -60,3 +60,17 @@ class StubYouTubeServiceTest(unittest.TestCase): def test_transcript_not_found(self): response = requests.get(self.url + 'test_transcripts_youtube/some_id') self.assertEqual(404, response.status_code) + + def test_reset_configuration(self): + + reset_config_url = self.url + 'del_config' + + # add some configuration data + self.server.config['test_reset'] = 'This is a reset config test' + + # reset server configuration + response = requests.delete(reset_config_url) + self.assertEqual(response.status_code, 200) + + # ensure that server config dict is empty after successful reset + self.assertEqual(self.server.config, {}) diff --git a/common/djangoapps/terrain/stubs/youtube.py b/common/djangoapps/terrain/stubs/youtube.py index a5b5db8644..d165fdee2c 100644 --- a/common/djangoapps/terrain/stubs/youtube.py +++ b/common/djangoapps/terrain/stubs/youtube.py @@ -17,6 +17,7 @@ To start this stub server on its own from Vagrant: you get "Unused url" message inside the browser. """ +import textwrap from .http import StubHttpRequestHandler, StubHttpService import json import time @@ -25,6 +26,17 @@ from urlparse import urlparse from collections import OrderedDict +IFRAME_API_RESPONSE = textwrap.dedent( + "if (!window['YT']) {var YT = {loading: 0,loaded: 0};}if (!window['YTConfig']) {var YTConfig" + " = {};}if (!YT.loading) {YT.loading = 1;(function(){var l = [];YT.ready = function(f) {if (" + "YT.loaded) {f();} else {l.push(f);}};window.onYTReady = function() {YT.loaded = 1;for (var " + "i = 0; i < l.length; i++) {try {l[i]();} catch (e) {}}};YT.setConfig = function(c) {for (var" + " k in c) {if (c.hasOwnProperty(k)) {YTConfig[k] = c[k];}}};var a = document.createElement" + "('script');a.id = 'www-widgetapi-script';a.src = 'http:' + '" + "//s.ytimg.com/yts/jsbin/www-widgetapi-vflxHr_AR.js';a.async = true;var b = " + "document.getElementsByTagName('script')[0];b.parentNode.insertBefore(a, b);})();}") + + class StubYouTubeHandler(StubHttpRequestHandler): """ A handler for Youtube GET requests. @@ -33,6 +45,17 @@ class StubYouTubeHandler(StubHttpRequestHandler): # Default number of seconds to delay the response to simulate network latency. DEFAULT_DELAY_SEC = 0.5 + def do_DELETE(self): # pylint: disable=C0103 + """ + Allow callers to delete all the server configurations using the /del_config URL. + """ + if self.path == "/del_config" or self.path == "/del_config/": + self.server.config = dict() + self.log_message("Reset Server Configuration.") + self.send_response(200) + else: + self.send_response(404) + def do_GET(self): """ Handle a GET request from the client and sends response back. @@ -80,8 +103,7 @@ class StubYouTubeHandler(StubHttpRequestHandler): if self.server.config.get('youtube_api_blocked'): self.send_response(404, content='', headers={'Content-type': 'text/plain'}) else: - response = self.server.config['youtube_api_response'] - self.send_response(200, content=response.text, headers={'Content-type': 'text/html'}) + self.send_response(200, content=IFRAME_API_RESPONSE, headers={'Content-type': 'text/html'}) else: self.send_response( diff --git a/common/test/acceptance/pages/lms/video.py b/common/test/acceptance/pages/lms/video.py index 0974cea65c..fe07f4d1ba 100644 --- a/common/test/acceptance/pages/lms/video.py +++ b/common/test/acceptance/pages/lms/video.py @@ -47,7 +47,6 @@ VIDEO_MENUS = { } - @js_defined('window.Video', 'window.RequireJS.require', 'window.jQuery') class VideoPage(PageObject): """ diff --git a/lms/djangoapps/courseware/features/video.py b/lms/djangoapps/courseware/features/video.py index 6bdcadf8d8..f979d72fc6 100644 --- a/lms/djangoapps/courseware/features/video.py +++ b/lms/djangoapps/courseware/features/video.py @@ -178,8 +178,6 @@ def add_videos_to_course(course, player_mode=None, display_names=None, hashes=No def add_video_to_course(course, parent_location=None, player_mode=None, data=None, display_name='Video'): - assert_less(world.youtube.config['youtube_api_response'].status_code, 400, "Real Youtube server is unavailable") - if not parent_location: parent_location = add_vertical_to_course(course) kwargs = get_metadata(parent_location, player_mode, data, display_name=display_name) diff --git a/lms/envs/acceptance.py b/lms/envs/acceptance.py index fcea155e82..ee6256f5fb 100644 --- a/lms/envs/acceptance.py +++ b/lms/envs/acceptance.py @@ -186,6 +186,6 @@ XQUEUE_INTERFACE = { } # Point the URL used to test YouTube availability to our stub YouTube server -YOUTUBE['API'] = 'youtube.com/iframe_api' +YOUTUBE['API'] = "127.0.0.1:{0}/get_youtube_api/".format(YOUTUBE_PORT) YOUTUBE['TEST_URL'] = "127.0.0.1:{0}/test_youtube/".format(YOUTUBE_PORT) YOUTUBE['TEXT_API']['url'] = "127.0.0.1:{0}/test_transcripts_youtube/".format(YOUTUBE_PORT) diff --git a/lms/envs/bok_choy.py b/lms/envs/bok_choy.py index 88f7f62c7b..07969726a7 100644 --- a/lms/envs/bok_choy.py +++ b/lms/envs/bok_choy.py @@ -5,6 +5,7 @@ Settings for bok choy tests import os from path import path + CONFIG_ROOT = path(__file__).abspath().dirname() # pylint: disable=E1120 TEST_ROOT = CONFIG_ROOT.dirname().dirname() / "test_root" @@ -60,3 +61,9 @@ for log_name, log_level in LOG_OVERRIDES: # Unfortunately, we need to use debug mode to serve staticfiles DEBUG = True + +# Point the URL used to test YouTube availability to our stub YouTube server +YOUTUBE_PORT = 9080 +YOUTUBE['API'] = "127.0.0.1:{0}/get_youtube_api/".format(YOUTUBE_PORT) +YOUTUBE['TEST_URL'] = "127.0.0.1:{0}/test_youtube/".format(YOUTUBE_PORT) +YOUTUBE['TEXT_API']['url'] = "127.0.0.1:{0}/test_transcripts_youtube/".format(YOUTUBE_PORT) diff --git a/rakelib/bok_choy.rake b/rakelib/bok_choy.rake index 06bf342693..758695f1b8 100644 --- a/rakelib/bok_choy.rake +++ b/rakelib/bok_choy.rake @@ -54,7 +54,13 @@ BOK_CHOY_STUBS = { :port => 8777, :log => File.join(BOK_CHOY_LOG_DIR, "bok_choy_video_sources.log"), :config => "root_dir=#{VIDEO_SOURCE_DIR}" + }, + + :youtube => { + :port => 9080, + :log => File.join(BOK_CHOY_LOG_DIR, "bok_choy_youtube.log") } + } # For the time being, stubs are used by both the bok-choy and lettuce acceptance tests