Skip to content

Commit 5e275a9

Browse files
committed
Use absolute paths so proxying can work
1 parent dc67fe0 commit 5e275a9

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

code_submitter/server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ async def upload(request: Request) -> Response:
111111
)
112112

113113
return RedirectResponse(
114-
app.url_path_for('homepage'),
114+
request.url_for('homepage'),
115115
# 302 so that the browser switches to GET
116116
status_code=302,
117117
)

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>Virtual Competition Code Submission</h1>
3434
{% if request.user.team %}
3535
<form
3636
class="col-sm-6"
37-
action="{{ request.app.url_path_for('upload') }}"
37+
action="{{ url_for('upload') }}"
3838
enctype="multipart/form-data"
3939
method="POST"
4040
>

tests/tests_app.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,32 @@ def setUp(self) -> None:
4343
# App import must happen after TESTING environment setup
4444
from code_submitter.server import app, database
4545

46+
def url_for(name: str) -> str:
47+
# While it makes for uglier tests, we do need to use more absolute
48+
# paths here so that the urls emitted contain the root_path from the
49+
# ASGI server and in turn work correctly under proxy.
50+
return 'http://testserver{}'.format(app.url_path_for(name))
51+
4652
test_client = TestClient(app)
4753
self.session = test_client.__enter__()
4854
self.session.auth = ('test_user', 'test_pass')
49-
self.url_path_for = app.url_path_for
55+
self.url_for = url_for
5056
self.database = database
5157

5258
def tearDown(self) -> None:
5359
self.session.__exit__(None, None, None)
5460
super().tearDown()
5561

5662
def test_app(self) -> None:
57-
response = self.session.get(self.url_path_for('homepage'))
63+
response = self.session.get(self.url_for('homepage'))
5864
self.assertEqual(200, response.status_code)
5965

6066
html = response.text
6167
self.assertIn("Upload a new submission", html)
6268

6369
def test_app_requires_auth(self) -> None:
6470
self.session.auth = None
65-
response = self.session.get(self.url_path_for('homepage'))
71+
response = self.session.get(self.url_for('homepage'))
6672
self.assertEqual(401, response.status_code)
6773
self.assertIn('WWW-Authenticate', response.headers)
6874
self.assertIn(' realm=', response.headers['WWW-Authenticate'])
@@ -97,7 +103,7 @@ def test_shows_own_and_own_team_uploads(self) -> None:
97103
),
98104
))
99105

100-
response = self.session.get(self.url_path_for('homepage'))
106+
response = self.session.get(self.url_for('homepage'))
101107
self.assertEqual(200, response.status_code)
102108

103109
html = response.text
@@ -159,7 +165,7 @@ def test_shows_chosen_archive(self) -> None:
159165
),
160166
))
161167

162-
response = self.session.get(self.url_path_for('homepage'))
168+
response = self.session.get(self.url_for('homepage'))
163169
self.assertEqual(200, response.status_code)
164170

165171
html = response.text
@@ -185,12 +191,12 @@ def test_upload_file(self) -> None:
185191
zip_file.writestr('robot.py', 'print("I am a robot")')
186192

187193
response = self.session.post(
188-
self.url_path_for('upload'),
194+
self.url_for('upload'),
189195
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
190196
)
191197
self.assertEqual(302, response.status_code)
192198
self.assertEqual(
193-
self.url_path_for('homepage'),
199+
self.url_for('homepage'),
194200
response.headers['location'],
195201
)
196202

@@ -227,13 +233,13 @@ def test_upload_and_choose_file(self) -> None:
227233
zip_file.writestr('robot.py', 'print("I am a robot")')
228234

229235
response = self.session.post(
230-
self.url_path_for('upload'),
236+
self.url_for('upload'),
231237
data={'choose': 'on'},
232238
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
233239
)
234240
self.assertEqual(302, response.status_code)
235241
self.assertEqual(
236-
self.url_path_for('homepage'),
242+
self.url_for('homepage'),
237243
response.headers['location'],
238244
)
239245

@@ -263,7 +269,7 @@ def test_upload_and_choose_file(self) -> None:
263269

264270
def test_upload_bad_file(self) -> None:
265271
response = self.session.post(
266-
self.url_path_for('upload'),
272+
self.url_for('upload'),
267273
files={'archive': ('whatever.zip', b'should-be-a-zip', 'application/zip')},
268274
)
269275
self.assertEqual(400, response.status_code)
@@ -284,7 +290,7 @@ def test_upload_archive_without_robot_py(self) -> None:
284290
zip_file.writestr('main.py', 'print("I am a robot")')
285291

286292
response = self.session.post(
287-
self.url_path_for('upload'),
293+
self.url_for('upload'),
288294
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
289295
)
290296
self.assertEqual(400, response.status_code)

0 commit comments

Comments
 (0)