From c9718ad63022a3c8485e6ce9832d731cf6318211 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sat, 9 Jan 2021 20:40:47 +0000 Subject: [PATCH] Add mechanism to create a Session --- code_submitter/server.py | 15 +++++++++++++++ templates/index.html | 16 ++++++++++++++++ tests/tests_app.py | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/code_submitter/server.py b/code_submitter/server.py index c382620..c4acee1 100644 --- a/code_submitter/server.py +++ b/code_submitter/server.py @@ -122,6 +122,20 @@ async def upload(request: Request) -> Response: ) +@requires(['authenticated', BLUESHIRT_SCOPE]) +async def create_session(request: Request) -> Response: + user: User = request.user + form = await request.form() + + await utils.create_session(database, form['name'], by_username=user.username) + + return RedirectResponse( + request.url_for('homepage'), + # 302 so that the browser switches to GET + status_code=302, + ) + + @requires(['authenticated', BLUESHIRT_SCOPE]) async def download_submissions(request: Request) -> Response: buffer = io.BytesIO() @@ -142,6 +156,7 @@ async def download_submissions(request: Request) -> Response: routes = [ Route('/', endpoint=homepage, methods=['GET']), Route('/upload', endpoint=upload, methods=['POST']), + Route('/create-session', endpoint=create_session, methods=['POST']), Route('/download-submissions', endpoint=download_submissions, methods=['GET']), ] diff --git a/templates/index.html b/templates/index.html index fb4ec22..e63fdfc 100644 --- a/templates/index.html +++ b/templates/index.html @@ -39,6 +39,22 @@

Virtual Competition Code Submission

+
+
+
+

Create a new session

+
+ + +
+ +
+
+
{% endif %}
{% if request.user.team %} diff --git a/tests/tests_app.py b/tests/tests_app.py index ad91d12..d5f16ce 100644 --- a/tests/tests_app.py +++ b/tests/tests_app.py @@ -4,9 +4,10 @@ from unittest import mock import test_utils +from sqlalchemy.sql import select from starlette.testclient import TestClient -from code_submitter.tables import Archive, ChoiceHistory +from code_submitter.tables import Archive, Session, ChoiceHistory class AppTests(test_utils.DatabaseTestCase): @@ -277,6 +278,42 @@ def test_upload_archive_without_robot_py(self) -> None: ) self.assertEqual([], choices, "Should not have created a choice") + def test_create_session_requires_blueshirt(self) -> None: + response = self.session.post( + self.url_for('create_session'), + data={'name': "Test session"}, + ) + self.assertEqual(403, response.status_code) + + def test_create_session(self) -> None: + self.session.auth = ('blueshirt', 'blueshirt') + + response = self.session.post( + self.url_for('create_session'), + data={'name': "Test session"}, + ) + self.assertEqual(302, response.status_code) + self.assertEqual( + self.url_for('homepage'), + response.headers['location'], + ) + + session, = self.await_( + self.database.fetch_all(select([ + Session.c.name, + Session.c.username, + ])), + ) + + self.assertEqual( + { + 'name': 'Test session', + 'username': 'blueshirt', + }, + dict(session), + "Should have created a session", + ) + def test_no_download_link_for_non_blueshirt(self) -> None: download_url = self.url_for('download_submissions')