Skip to content

Commit 9357edd

Browse files
committed
Use generated url paths universally
1 parent b550e08 commit 9357edd

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

code_submitter/server.py

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

9696
return RedirectResponse(
97-
request.url_for('homepage'),
97+
app.url_path_for('homepage'),
9898
# 302 so that the browser switches to GET
9999
status_code=302,
100100
)

templates/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h1>Virtual Competition Code Submission</h1>
3333
<div class="row">
3434
<form
3535
class="col-sm-6"
36-
action="{{ url_for('upload') }}"
36+
action="{{ request.app.url_path_for('upload') }}"
3737
enctype="multipart/form-data"
3838
method="POST"
3939
>

tests/tests_app.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def setUp(self) -> None:
4646
test_client = TestClient(app)
4747
self.session = test_client.__enter__()
4848
self.session.auth = ('test_user', 'test_pass')
49+
self.url_path_for = app.url_path_for
4950
self.database = database
5051
self.loop = asyncio.get_event_loop()
5152

@@ -54,12 +55,12 @@ def tearDown(self) -> None:
5455
super().tearDown()
5556

5657
def test_app(self) -> None:
57-
response = self.session.get('/')
58+
response = self.session.get(self.url_path_for('homepage'))
5859
self.assertEqual(200, response.status_code)
5960

6061
def test_app_requires_auth(self) -> None:
6162
self.session.auth = None
62-
response = self.session.get('/')
63+
response = self.session.get(self.url_path_for('homepage'))
6364
self.assertEqual(401, response.status_code)
6465
self.assertIn('WWW-Authenticate', response.headers)
6566
self.assertIn(' realm=', response.headers['WWW-Authenticate'])
@@ -94,7 +95,7 @@ def test_shows_own_and_own_team_uploads(self) -> None:
9495
),
9596
))
9697

97-
response = self.session.get('/')
98+
response = self.session.get(self.url_path_for('homepage'))
9899
self.assertEqual(200, response.status_code)
99100

100101
html = response.text
@@ -156,7 +157,7 @@ def test_shows_chosen_archive(self) -> None:
156157
),
157158
))
158159

159-
response = self.session.get('/')
160+
response = self.session.get(self.url_path_for('homepage'))
160161
self.assertEqual(200, response.status_code)
161162

162163
html = response.text
@@ -182,11 +183,14 @@ def test_upload_file(self) -> None:
182183
zip_file.writestr('robot.py', 'print("I am a robot")')
183184

184185
response = self.session.post(
185-
'/upload',
186+
self.url_path_for('upload'),
186187
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
187188
)
188189
self.assertEqual(302, response.status_code)
189-
self.assertEqual('http://testserver/', response.headers['location'])
190+
self.assertEqual(
191+
self.url_path_for('homepage'),
192+
response.headers['location'],
193+
)
190194

191195
archives = self.await_(
192196
self.database.fetch_all(Archive.select()),
@@ -221,12 +225,15 @@ def test_upload_and_choose_file(self) -> None:
221225
zip_file.writestr('robot.py', 'print("I am a robot")')
222226

223227
response = self.session.post(
224-
'/upload',
228+
self.url_path_for('upload'),
225229
data={'choose': 'on'},
226230
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
227231
)
228232
self.assertEqual(302, response.status_code)
229-
self.assertEqual('http://testserver/', response.headers['location'])
233+
self.assertEqual(
234+
self.url_path_for('homepage'),
235+
response.headers['location'],
236+
)
230237

231238
archive, = self.await_(
232239
self.database.fetch_all(Archive.select()),
@@ -254,7 +261,7 @@ def test_upload_and_choose_file(self) -> None:
254261

255262
def test_upload_bad_file(self) -> None:
256263
response = self.session.post(
257-
'/upload',
264+
self.url_path_for('upload'),
258265
files={'archive': ('whatever.zip', b'should-be-a-zip', 'application/zip')},
259266
)
260267
self.assertEqual(400, response.status_code)

0 commit comments

Comments
 (0)