From 2d98a608cc53ba05f8e211cbd449843ec5b10f03 Mon Sep 17 00:00:00 2001 From: Steve Date: Tue, 16 Jul 2024 10:53:30 +0200 Subject: [PATCH] Display when competition is full --- server.py | 12 ++++++--- templates/index.html | 10 ++++++++ templates/welcome.html | 24 +++++++++--------- tests/conftest.py | 12 +++++++-- tests/test_server.py | 55 ++++++++++++++++++++++++++++++++++++------ 5 files changed, 89 insertions(+), 24 deletions(-) diff --git a/server.py b/server.py index 25b909a61..e220e4eb1 100644 --- a/server.py +++ b/server.py @@ -34,10 +34,14 @@ def index(): @app.route('/showSummary', methods=['POST']) def showSummary(): - club = [club for club in clubs if club['email'] == request.form['email']][ - 0] - return render_template('welcome.html', club=club, - competitions=competitions) + try: + club = [club for club in clubs if club['email'] == request.form['email']][ + 0] + return render_template('welcome.html', club=club, + competitions=competitions) + except IndexError: + flash('Wrong email-please try again') + return render_template('index.html') @app.route('/book//') diff --git a/templates/index.html b/templates/index.html index 926526b7d..c22b864ef 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,5 +12,15 @@

Welcome to the GUDLFT Registration Portal!

+ + + {% with messages = get_flashed_messages()%} + {% if messages %} + {% for message in messages %} + {{message}} + {% endfor %} + {% endif %} + {% endwith %} + \ No newline at end of file diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261a2..4405feda4 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -7,7 +7,7 @@

Welcome, {{club['email']}}

Logout - {% with messages = get_flashed_messages()%} + {% with messages = get_flashed_messages() %} {% if messages %}
    {% for message in messages %} @@ -15,22 +15,24 @@

    Welcome, {{club['email']}}

    Logout {% endfor %}
{% endif%} - Points available: {{club['points']}} + Points available: {{ club['points'] }}

Competitions:

    - {% for comp in competitions%} + {% for comp in competitions %}
  • - {{comp['name']}}
    - Date: {{comp['date']}}
    - Number of Places: {{comp['numberOfPlaces']}} - {%if comp['numberOfPlaces']|int >0%} - Book Places - {%endif%} +

    {{ comp['name'] }}

    +

    Date: {{ comp['date'] }}

    +

    Number of Places: {{ comp['numberOfPlaces'] }}

    + {% if comp['numberOfPlaces']|int >0 %} + Book Places + {% else %} + - Competition complete + {% endif %}
  • -
    +
    {% endfor %}
- {%endwith%} + {% endwith %} \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 802b1df5c..5a3c904e9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,8 +1,7 @@ -import copy import pytest import json -from server import app +from server import app, competitions @pytest.fixture @@ -22,3 +21,12 @@ def test_clubs(): def test_competitions(): with open('competitions.json') as comps: return json.load(comps)['competitions'] + + +@pytest.fixture +def test_competition_full(): + competitions[:] = [ + {'name': 'Spring Festival', + 'date': '2020-03-27 10:00:00', + 'numberOfPlaces': '0'} + ] diff --git a/tests/test_server.py b/tests/test_server.py index 07e3860c7..c66074ba0 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -2,14 +2,18 @@ from server import loadCompetitions, loadClubs -def test_purchase_places(client): - test_club = loadClubs()[0] - test_competition = loadCompetitions()[0] +def test_purchase_places(client, test_clubs, test_competitions, mocker): + mocker.patch('server.loadClubs', return_value=test_clubs) + mocker.patch('server.loadCompetitions', return_value=test_competitions) + mock_save_club = mocker.patch('server.saveClub') + + mocker.patch.object(server, 'clubs', test_clubs) + mocker.patch.object(server, 'competitions', test_competitions) places_to_purchase = 8 response = client.post('/purchasePlaces', data={ - 'club': test_club['name'], - 'competition': test_competition['name'], + 'club': test_clubs[0]['name'], + 'competition': test_competitions[0]['name'], 'places': places_to_purchase }) @@ -47,8 +51,7 @@ def test_has_sufficient_points(client): assert b'Insufficiant points.' in response.data -def test_purchase_places(client, test_clubs, test_competitions, mocker): - +def test_update_points_after_purchase(client, test_clubs, test_competitions, mocker): mocker.patch('server.loadClubs', return_value=test_clubs) mocker.patch('server.loadCompetitions', return_value=test_competitions) mock_save_club = mocker.patch('server.saveClub') @@ -66,3 +69,41 @@ def test_purchase_places(client, test_clubs, test_competitions, mocker): assert int(test_clubs[0]['points']) == 4 assert b'Great-booking complete!' in response.data + + +def test_login(client): + + response = client.post('/showSummary', data={ + 'email': 'john@simplylift.co' + }) + assert response.status_code == 200 + + +def test_wrong_login(client): + + response = client.post('/showSummary', data={ + 'email': 'wrong-email@test.com' + }) + assert response.status_code == 200 + assert b'Wrong email-please try again' in response.data + + +def test_display_book_available(client): + test_club = loadClubs()[0] + test_competitions = loadCompetitions() + + response = client.post('/showSummary', data={'email': test_club['email']}) + + assert response.status_code == 200 + assert b'Number of Places: 25' in response.data + + +def test_display_book_non_available(client, test_competition_full): + + test_club = loadClubs()[0] + + response = client.post('/showSummary', data={'email': test_club['email']}) + + assert response.status_code == 200 + assert b'Spring Festival' in response.data + assert b'- Competition complete' in response.data