11"""Test main handlers"""
22
33import time
4- from urllib .parse import quote , urlparse
4+ from urllib .parse import quote
55
66import jwt
77import pytest
88from bs4 import BeautifulSoup
99
10- from binderhub import __version__ as binder_version
11-
1210from .utils import async_requests
1311
1412
15- @pytest .mark .parametrize (
16- "old_url, new_url" ,
17- [
18- (
19- "/repo/binderhub-ci-repos/requirements" ,
20- "/v2/gh/binderhub-ci-repos/requirements/master" ,
21- ),
22- (
23- "/repo/binderhub-ci-repos/requirements/" ,
24- "/v2/gh/binderhub-ci-repos/requirements/master" ,
25- ),
26- (
27- "/repo/binderhub-ci-repos/requirements/notebooks/index.ipynb" ,
28- "/v2/gh/binderhub-ci-repos/requirements/master?urlpath=%2Fnotebooks%2Findex.ipynb" ,
29- ),
30- ],
31- )
32- async def test_legacy_redirect (app , old_url , new_url ):
33- r = await async_requests .get (app .url + old_url , allow_redirects = False )
34- assert r .status_code == 302
35- assert r .headers ["location" ] == new_url
36-
37-
38- def _resolve_url (page_url , url ):
39- """Resolve a URL relative to a page"""
40-
41- # full URL, nothing to resolve
42- if "://" in url :
43- return url
44-
45- parsed = urlparse (page_url )
46-
47- if url .startswith ("/" ):
48- # absolute path
49- return f"{ parsed .scheme } ://{ parsed .netloc } { url } "
50-
51- # relative path URL
52-
53- if page_url .endswith ("/" ):
54- # URL is a directory, resolve relative to dir
55- path = parsed .path
56- else :
57- # URL is not a directory, resolve relative to parent
58- path = parsed .path .rsplit ("/" , 1 )[0 ] + "/"
59-
60- return f"{ parsed .scheme } ://{ parsed .netloc } { path } { url } "
61-
62-
63- @pytest .mark .remote
64- async def test_main_page (app ):
65- """Check the main page and any links on it"""
66- r = await async_requests .get (app .url )
67- assert r .status_code == 200
68- soup = BeautifulSoup (r .text , "html5lib" )
69-
70- # check src links (style, images)
71- for el in soup .find_all (src = True ):
72- url = _resolve_url (app .url , el ["src" ])
73- r = await async_requests .get (url )
74- assert r .status_code == 200 , f"{ r .status_code } { url } "
75-
76- # check hrefs
77- for el in soup .find_all (href = True ):
78- href = el ["href" ]
79- if href .startswith ("#" ):
80- continue
81- url = _resolve_url (app .url , href )
82- r = await async_requests .get (url )
83- assert r .status_code == 200 , f"{ r .status_code } { url } "
84-
85-
8613@pytest .mark .remote
8714@pytest .mark .helm
8815async def test_custom_template (app ):
@@ -92,94 +19,6 @@ async def test_custom_template(app):
9219 assert "test-template" in r .text
9320
9421
95- @pytest .mark .remote
96- async def test_about_handler (app ):
97- # Check that the about page loads
98- r = await async_requests .get (app .url + "/about" )
99- assert r .status_code == 200
100- assert "This website is powered by" in r .text
101- assert binder_version .split ("+" )[0 ] in r .text
102-
103-
104- @pytest .mark .remote
105- async def test_versions_handler (app ):
106- # Check that the about page loads
107- r = await async_requests .get (app .url + "/versions" )
108- assert r .status_code == 200
109-
110- data = r .json ()
111- # builder_info is different for KubernetesExecutor and LocalRepo2dockerBuild
112- try :
113- import repo2docker
114-
115- allowed_builder_info = [{"repo2docker-version" : repo2docker .__version__ }]
116- except ImportError :
117- allowed_builder_info = []
118- allowed_builder_info .append ({"build_image" : app .build_image })
119-
120- assert data ["builder_info" ] in allowed_builder_info
121- assert data ["binderhub" ].split ("+" )[0 ] == binder_version .split ("+" )[0 ]
122-
123-
124- @pytest .mark .parametrize (
125- "provider_prefix,repo,ref,path,path_type,status_code" ,
126- [
127- ("gh" , "binderhub-ci-repos/requirements" , "master" , "" , "" , 200 ),
128- ("gh" , "binderhub-ci-repos%2Frequirements" , "master" , "" , "" , 400 ),
129- ("gh" , "binderhub-ci-repos/requirements" , "master/" , "" , "" , 200 ),
130- (
131- "gh" ,
132- "binderhub-ci-repos/requirements" ,
133- "20c4fe55a9b2c5011d228545e821b1c7b1723652" ,
134- "index.ipynb" ,
135- "file" ,
136- 200 ,
137- ),
138- (
139- "gh" ,
140- "binderhub-ci-repos/requirements" ,
141- "20c4fe55a9b2c5011d228545e821b1c7b1723652" ,
142- "%2Fnotebooks%2Findex.ipynb" ,
143- "url" ,
144- 200 ,
145- ),
146- ("gh" , "binderhub-ci-repos/requirements" , "master" , "has%20space" , "file" , 200 ),
147- (
148- "gh" ,
149- "binderhub-ci-repos/requirements" ,
150- "master/" ,
151- "%2Fhas%20space%2F" ,
152- "file" ,
153- 200 ,
154- ),
155- (
156- "gh" ,
157- "binderhub-ci-repos/requirements" ,
158- "master" ,
159- "%2Fhas%20space%2F%C3%BCnicode.ipynb" ,
160- "file" ,
161- 200 ,
162- ),
163- ],
164- )
165- async def test_loading_page (
166- app , provider_prefix , repo , ref , path , path_type , status_code
167- ):
168- # repo = f'{org}/{repo_name}'
169- spec = f"{ repo } /{ ref } "
170- provider_spec = f"{ provider_prefix } /{ spec } "
171- query = f"{ path_type } path={ path } " if path else ""
172- uri = f"/v2/{ provider_spec } ?{ query } "
173- r = await async_requests .get (app .url + uri )
174- assert r .status_code == status_code , f"{ r .status_code } { uri } "
175- if status_code == 200 :
176- soup = BeautifulSoup (r .text , "html5lib" )
177- assert soup .find (id = "log-container" )
178- nbviewer_url = soup .find (id = "nbviewer-preview" ).find ("iframe" ).attrs ["src" ]
179- r = await async_requests .get (nbviewer_url )
180- assert r .status_code == 200 , f"{ r .status_code } { nbviewer_url } "
181-
182-
18322@pytest .mark .parametrize (
18423 "origin,host,expected_origin" ,
18524 [
0 commit comments