From 2e4e1ce82f3e3efb8f2df6868fbce30ff6c3bb26 Mon Sep 17 00:00:00 2001 From: Min RK Date: Tue, 11 Sep 2018 09:52:17 +0200 Subject: [PATCH] test token page with html parsing --- dev-requirements.txt | 1 + jupyterhub/tests/test_pages.py | 44 ++++++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/dev-requirements.txt b/dev-requirements.txt index 84612025..9638d968 100644 --- a/dev-requirements.txt +++ b/dev-requirements.txt @@ -1,5 +1,6 @@ -r requirements.txt mock +beautifulsoup4 codecov cryptography pytest-cov diff --git a/jupyterhub/tests/test_pages.py b/jupyterhub/tests/test_pages.py index e9f66bd0..fad6960c 100644 --- a/jupyterhub/tests/test_pages.py +++ b/jupyterhub/tests/test_pages.py @@ -1,7 +1,9 @@ """Tests for HTML pages""" +import sys from urllib.parse import urlencode, urlparse +from bs4 import BeautifulSoup from tornado import gen from tornado.httputil import url_concat @@ -605,20 +607,42 @@ def test_token_page(app): r = yield get_page("token", app, cookies=cookies) r.raise_for_status() assert urlparse(r.url).path.endswith('/hub/token') - assert "Request new API token" in r.text - assert "API Tokens" in r.text - assert "Server at %s" % app.users[name].url in r.text - # no oauth tokens yet, shouldn't have that section - assert "Authorized Applications" not in r.text + def extract_body(r): + soup = BeautifulSoup(r.text, "html5lib") + import re + # trim empty lines + return re.sub(r"(\n\s*)+", "\n", soup.body.find(class_="container").text) + body = extract_body(r) + assert "Request new API token" in body, body + # no tokens yet, no lists + assert "API Tokens" not in body, body + assert "Authorized Applications" not in body, body - # spawn the user to trigger oauth, etc. - r = yield get_page("spawn", app, cookies=cookies) - r.raise_fo_status() + # request an API token + user = app.users[name] + token = user.new_api_token(expires_in=60, note="my-test-token") + app.db.commit() r = yield get_page("token", app, cookies=cookies) r.raise_for_status() - assert "API Tokens" in r.text - assert "Authorized Applications" not in r.text + body = extract_body(r) + assert "API Tokens" in body, body + assert "my-test-token" in body, body + # no oauth tokens yet, shouldn't have that section + assert "Authorized Applications" not in body, body + + # spawn the user to trigger oauth, etc. + # request an oauth token + user.spawner.cmd = [sys.executable, '-m', 'jupyterhub.singleuser'] + r = yield get_page("spawn", app, cookies=cookies) + r.raise_for_status() + + r = yield get_page("token", app, cookies=cookies) + r.raise_for_status() + body = extract_body(r) + assert "API Tokens" in body, body + assert "Server at %s" % user.base_url in body, body + assert "Authorized Applications" in body, body @pytest.mark.gen_test