Run tests with an encoded base_url

to ensure we get our escaping right

Mostly revealed fixes needed in tests so far, not code,
but should catch regressions.
This commit is contained in:
Min RK
2016-05-25 15:56:49 +02:00
parent aa2999210d
commit 05c268e190
5 changed files with 26 additions and 24 deletions

View File

@@ -902,6 +902,7 @@ class JupyterHub(Application):
self.proxy.log = self.log self.proxy.log = self.log
self.proxy.public_server.ip = self.ip self.proxy.public_server.ip = self.ip
self.proxy.public_server.port = self.port self.proxy.public_server.port = self.port
self.proxy.public_server.base_url = self.base_url
self.proxy.api_server.ip = self.proxy_api_ip self.proxy.api_server.ip = self.proxy_api_ip
self.proxy.api_server.port = self.proxy_api_port self.proxy.api_server.port = self.proxy_api_port
self.proxy.api_server.base_url = '/api/routes/' self.proxy.api_server.base_url = '/api/routes/'

View File

@@ -117,6 +117,8 @@ class MockHub(JupyterHub):
last_activity_interval = 2 last_activity_interval = 2
base_url = '/@/space%20word/'
@default('subdomain_host') @default('subdomain_host')
def _subdomain_host_default(self): def _subdomain_host_default(self):
return os.environ.get('JUPYTERHUB_TEST_SUBDOMAIN_HOST', '') return os.environ.get('JUPYTERHUB_TEST_SUBDOMAIN_HOST', '')

View File

@@ -13,7 +13,7 @@ from .. import orm
from ..user import User from ..user import User
from ..utils import url_path_join as ujoin from ..utils import url_path_join as ujoin
from . import mocking from . import mocking
from .mocking import public_url, user_url from .mocking import public_host, public_url, user_url
def check_db_locks(func): def check_db_locks(func):
@@ -105,7 +105,7 @@ def test_auth_api(app):
def test_referer_check(app, io_loop): def test_referer_check(app, io_loop):
url = ujoin(public_url(app), app.hub.server.base_url) url = ujoin(public_host(app), app.hub.server.base_url)
host = urlparse(url).netloc host = urlparse(url).netloc
user = find_user(app.db, 'admin') user = find_user(app.db, 'admin')
if user is None: if user is None:
@@ -351,7 +351,7 @@ def test_spawn(app, io_loop):
status = io_loop.run_sync(app_user.spawner.poll) status = io_loop.run_sync(app_user.spawner.poll)
assert status is None assert status is None
assert user.server.base_url == '/user/%s' % name assert user.server.base_url == ujoin(app.base_url, 'user/%s' % name)
url = user_url(user, app) url = user_url(user, app)
print(url) print(url)
r = requests.get(url) r = requests.get(url)

View File

@@ -12,7 +12,7 @@ from .mocking import FormSpawner, public_url, public_host, user_url
from .test_api import api_request from .test_api import api_request
def get_page(path, app, **kw): def get_page(path, app, **kw):
base_url = ujoin(public_url(app), app.hub.server.base_url) base_url = ujoin(public_host(app), app.hub.server.base_url)
print(base_url) print(base_url)
return requests.get(ujoin(base_url, path), **kw) return requests.get(ujoin(base_url, path), **kw)
@@ -21,11 +21,11 @@ def test_root_no_auth(app, io_loop):
routes = io_loop.run_sync(app.proxy.get_routes) routes = io_loop.run_sync(app.proxy.get_routes)
print(routes) print(routes)
print(app.hub.server) print(app.hub.server)
url = public_url(app) url = ujoin(public_host(app), app.hub.server.base_url)
print(url) print(url)
r = requests.get(url) r = requests.get(url)
r.raise_for_status() r.raise_for_status()
assert r.url == ujoin(url, app.hub.server.base_url, 'login') assert r.url == ujoin(url, 'login')
def test_root_auth(app): def test_root_auth(app):
cookies = app.login_user('river') cookies = app.login_user('river')
@@ -80,7 +80,7 @@ def test_spawn_redirect(app, io_loop):
r.raise_for_status() r.raise_for_status()
print(urlparse(r.url)) print(urlparse(r.url))
path = urlparse(r.url).path path = urlparse(r.url).path
assert path == '/user/%s' % name assert path == ujoin(app.base_url, 'user/%s' % name)
# should have started server # should have started server
status = io_loop.run_sync(u.spawner.poll) status = io_loop.run_sync(u.spawner.poll)
@@ -91,7 +91,7 @@ def test_spawn_redirect(app, io_loop):
r.raise_for_status() r.raise_for_status()
print(urlparse(r.url)) print(urlparse(r.url))
path = urlparse(r.url).path path = urlparse(r.url).path
assert path == '/user/%s' % name assert path == ujoin(app.base_url, '/user/%s' % name)
def test_spawn_page(app): def test_spawn_page(app):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}):
@@ -102,7 +102,7 @@ def test_spawn_page(app):
def test_spawn_form(app, io_loop): def test_spawn_form(app, io_loop):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}):
base_url = ujoin(public_url(app), app.hub.server.base_url) base_url = ujoin(public_host(app), app.hub.server.base_url)
cookies = app.login_user('jones') cookies = app.login_user('jones')
orm_u = orm.User.find(app.db, 'jones') orm_u = orm.User.find(app.db, 'jones')
u = app.users[orm_u] u = app.users[orm_u]
@@ -123,7 +123,7 @@ def test_spawn_form(app, io_loop):
def test_spawn_form_with_file(app, io_loop): def test_spawn_form_with_file(app, io_loop):
with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}): with mock.patch.dict(app.users.settings, {'spawner_class': FormSpawner}):
base_url = ujoin(public_url(app), app.hub.server.base_url) base_url = ujoin(public_host(app), app.hub.server.base_url)
cookies = app.login_user('jones') cookies = app.login_user('jones')
orm_u = orm.User.find(app.db, 'jones') orm_u = orm.User.find(app.db, 'jones')
u = app.users[orm_u] u = app.users[orm_u]
@@ -138,8 +138,6 @@ def test_spawn_form_with_file(app, io_loop):
files={'hello': ('hello.txt', b'hello world\n')} files={'hello': ('hello.txt', b'hello world\n')}
) )
r.raise_for_status() r.raise_for_status()
print(u.spawner)
print(u.spawner.user_options)
assert u.spawner.user_options == { assert u.spawner.user_options == {
'energy': '511keV', 'energy': '511keV',
'bounds': [-1, 1], 'bounds': [-1, 1],
@@ -158,21 +156,23 @@ def test_user_redirect(app):
r.raise_for_status() r.raise_for_status()
print(urlparse(r.url)) print(urlparse(r.url))
path = urlparse(r.url).path path = urlparse(r.url).path
assert path == '/user/%s' % name assert path == ujoin(app.base_url, '/user/%s' % name)
r = get_page('/user/baduser/test.ipynb', app, cookies=cookies) r = get_page('/user/baduser/test.ipynb', app, cookies=cookies)
r.raise_for_status() r.raise_for_status()
print(urlparse(r.url)) print(urlparse(r.url))
path = urlparse(r.url).path path = urlparse(r.url).path
assert path == '/user/%s/test.ipynb' % name assert path == ujoin(app.base_url, '/user/%s/test.ipynb' % name)
r = get_page('/user/baduser/test.ipynb', app) r = get_page('/user/baduser/test.ipynb', app)
r.raise_for_status() r.raise_for_status()
print(urlparse(r.url)) print(urlparse(r.url))
path = urlparse(r.url).path path = urlparse(r.url).path
assert path == '/hub/login' assert path == ujoin(app.base_url, '/hub/login')
query = urlparse(r.url).query query = urlparse(r.url).query
assert query == urlencode({'next': '/hub/user/baduser/test.ipynb'}) assert query == urlencode({
'next': ujoin(app.base_url, '/hub/user/baduser/test.ipynb')
})
def test_login_fail(app): def test_login_fail(app):
@@ -233,8 +233,7 @@ def test_login_no_whitelist_adds_user(app):
def test_static_files(app): def test_static_files(app):
base_url = ujoin(public_url(app), app.hub.server.base_url) base_url = ujoin(public_host(app), app.hub.server.base_url)
print(base_url)
r = requests.get(ujoin(base_url, 'logo')) r = requests.get(ujoin(base_url, 'logo'))
r.raise_for_status() r.raise_for_status()
assert r.headers['content-type'] == 'image/png' assert r.headers['content-type'] == 'image/png'

View File

@@ -4,12 +4,12 @@ import json
import os import os
from queue import Queue from queue import Queue
from subprocess import Popen from subprocess import Popen
from urllib.parse import urlparse from urllib.parse import urlparse, unquote
from .. import orm from .. import orm
from .mocking import MockHub from .mocking import MockHub
from .test_api import api_request from .test_api import api_request
from ..utils import wait_for_http_server from ..utils import wait_for_http_server, url_path_join as ujoin
def test_external_proxy(request, io_loop): def test_external_proxy(request, io_loop):
"""Test a proxy started before the Hub""" """Test a proxy started before the Hub"""
@@ -63,7 +63,7 @@ def test_external_proxy(request, io_loop):
r.raise_for_status() r.raise_for_status()
routes = io_loop.run_sync(app.proxy.get_routes) routes = io_loop.run_sync(app.proxy.get_routes)
user_path = '/user/river' user_path = unquote(ujoin(app.base_url, 'user/river'))
if app.subdomain_host: if app.subdomain_host:
domain = urlparse(app.subdomain_host).hostname domain = urlparse(app.subdomain_host).hostname
user_path = '/%s.%s' % (name, domain) + user_path user_path = '/%s.%s' % (name, domain) + user_path
@@ -136,14 +136,14 @@ def test_check_routes(app, io_loop):
assert zoe is not None assert zoe is not None
zoe = app.users[zoe] zoe = app.users[zoe]
before = sorted(io_loop.run_sync(app.proxy.get_routes)) before = sorted(io_loop.run_sync(app.proxy.get_routes))
assert zoe.proxy_path in before assert unquote(zoe.proxy_path) in before
io_loop.run_sync(lambda : app.proxy.check_routes(app.users)) io_loop.run_sync(lambda : app.proxy.check_routes(app.users))
io_loop.run_sync(lambda : proxy.delete_user(zoe)) io_loop.run_sync(lambda : proxy.delete_user(zoe))
during = sorted(io_loop.run_sync(app.proxy.get_routes)) during = sorted(io_loop.run_sync(app.proxy.get_routes))
assert zoe.proxy_path not in during assert unquote(zoe.proxy_path) not in during
io_loop.run_sync(lambda : app.proxy.check_routes(app.users)) io_loop.run_sync(lambda : app.proxy.check_routes(app.users))
after = sorted(io_loop.run_sync(app.proxy.get_routes)) after = sorted(io_loop.run_sync(app.proxy.get_routes))
assert zoe.proxy_path in after assert unquote(zoe.proxy_path) in after
assert before == after assert before == after