Skip to content

Commit

Permalink
Add mechanism to create a Session
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterJCLaw committed Jan 9, 2021
1 parent f56b427 commit c9718ad
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
15 changes: 15 additions & 0 deletions code_submitter/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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']),
]

Expand Down
16 changes: 16 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ <h1>Virtual Competition Code Submission</h1>
</a>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<form
action="{{ url_for('create_session') }}"
enctype="multipart/form-data"
method="POST"
>
<h4>Create a new session</h4>
<div class="form-group row">
<label class="col-sm-2 col-form-label" for="name">Name</label>
<input class="col-sm-6" type="text" name="name" required />
</div>
<button class="btn btn-primary" type="submit">Create</button>
</form>
</div>
</div>
{% endif %}
<div class="row">
{% if request.user.team %}
Expand Down
39 changes: 38 additions & 1 deletion tests/tests_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit c9718ad

Please sign in to comment.