Commit 6f064e2b by Muhammad Ammar Committed by Muhammmad Ammar

Configure youtube stub for bok-choy

parent 83853098
......@@ -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,
......
......@@ -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)
"""
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)
......
......@@ -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, {})
......@@ -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(
......
......@@ -47,7 +47,6 @@ VIDEO_MENUS = {
}
@js_defined('window.Video', 'window.RequireJS.require', 'window.jQuery')
class VideoPage(PageObject):
"""
......
......@@ -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)
......
......@@ -182,6 +182,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)
......@@ -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)
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment