Skip to content

Commit 15e1772

Browse files
committed
Require a robot.py file in the archive
Fixes #9.
1 parent becfd01 commit 15e1772

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

code_submitter/server.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,18 @@ async def upload(request: Request) -> Response:
8383
)
8484

8585
try:
86-
zipfile.ZipFile(io.BytesIO(contents))
86+
zf = zipfile.ZipFile(io.BytesIO(contents))
8787
except zipfile.BadZipFile:
8888
return Response("Must upload a ZIP file", status_code=400)
8989

90+
try:
91+
zf.getinfo('robot.py')
92+
except KeyError:
93+
return Response(
94+
"ZIP file must contain a 'robot.py' at the root",
95+
status_code=400,
96+
)
97+
9098
archive_id = await database.execute(
9199
Archive.insert().values(
92100
content=contents,

tests/tests_app.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,24 @@ def test_upload_bad_file(self) -> None:
277277
self.database.fetch_all(ChoiceHistory.select()),
278278
)
279279
self.assertEqual([], choices, "Should not have created a choice")
280+
281+
def test_upload_archive_without_robot_py(self) -> None:
282+
contents = io.BytesIO()
283+
with zipfile.ZipFile(contents, mode='w') as zip_file:
284+
zip_file.writestr('main.py', 'print("I am a robot")')
285+
286+
response = self.session.post(
287+
self.url_path_for('upload'),
288+
files={'archive': ('whatever.zip', contents.getvalue(), 'application/zip')},
289+
)
290+
self.assertEqual(400, response.status_code)
291+
292+
archives = self.await_(
293+
self.database.fetch_all(Archive.select()),
294+
)
295+
self.assertEqual([], archives, "Wrong content stored in the database")
296+
297+
choices = self.await_(
298+
self.database.fetch_all(ChoiceHistory.select()),
299+
)
300+
self.assertEqual([], choices, "Should not have created a choice")

0 commit comments

Comments
 (0)