diff --git a/competitions.json b/competitions.json index 039fc61bd..4a59f02c3 100644 --- a/competitions.json +++ b/competitions.json @@ -9,6 +9,16 @@ "name": "Fall Classic", "date": "2020-10-22 13:30:00", "numberOfPlaces": "13" + }, + { + "name": "Winter Competition", + "date": "2022-12-01 14:00:00", + "numberOfPlaces": "8" + }, + { + "name": "College League", + "date": "2023-01-15 11:35:00", + "numberOfPlaces": "78" } ] } \ No newline at end of file diff --git a/server.py b/server.py index 1c208b245..ce719c4ed 100644 --- a/server.py +++ b/server.py @@ -46,25 +46,41 @@ def show_summary(): return redirect(url_for('index')) +# ====================================================== ROUTE FOR BOOK PLACES + + @app.route('/book//') def book(competition,club): + """ + Displays : + Competition's name and places available + & the booking form + """ foundClub = [c for c in clubs if c['name'] == club][0] foundCompetition = [c for c in competitions if c['name'] == competition][0] if foundClub and foundCompetition: return render_template('booking.html',club=foundClub,competition=foundCompetition) else: flash("Something went wrong-please try again") - return render_template('welcome.html', club=club, competitions=competitions) + return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date) + + +# ============================================================ ROUTE FOR PURCHASE PLACES @app.route('/purchasePlaces',methods=['POST']) -def purchasePlaces(): +@app.errorhandler(400) +def purchase_places(): competition = [c for c in competitions if c['name'] == request.form['competition']][0] club = [c for c in clubs if c['name'] == request.form['club']][0] placesRequired = int(request.form['places']) + + if competition['date'] < current_date: + flash("Sorry, this competition is over !") + return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date), 400 competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + return render_template('welcome.html', club=club, competitions=competitions, current_date=current_date) # TODO: Add route for points display diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261a2..6e4a1f3f4 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -23,7 +23,9 @@

Competitions:

{{comp['name']}}
Date: {{comp['date']}}
Number of Places: {{comp['numberOfPlaces']}} - {%if comp['numberOfPlaces']|int >0%} + {%if comp['date'] < current_date %} +

This competition is over

+ {%elif comp['numberOfPlaces']|int >0%} Book Places {%endif%} diff --git a/tests/conftest.py b/tests/conftest.py index c06f99ab3..34689eb87 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -12,10 +12,10 @@ def client(): yield client -# @pytest.fixture -# def current_date(): -# now = datetime.datetime.now() -# return now.strftime("%Y-%m-%d, %H:%M:%S") +@pytest.fixture +def current_date(): + now = datetime.datetime.now() + return now.strftime("%Y-%m-%d, %H:%M:%S") @pytest.fixture @@ -58,3 +58,29 @@ def unknown_club(): "points": "16" } ] + + +@pytest.fixture +def competitions_fixture(): + return [ + { + "name": "Aka League", + "date": "2023-02-05 10:00:00", + "numberOfPlaces": "20" + }, + { + "name": "Pink Race", + "date": "2021-05-12 09:00:00", + "numberOfPlaces": "4" + }, + { + "name": "Champion Road", + "date": "2022-10-13 13:00:00", + "numberOfPlaces": "13" + }, + { + "name": "Ice Race", + "date": "2022-12-01 14:00:00", + "numberOfPlaces": "10" + } + ] diff --git a/tests/units/test_past_competition.py b/tests/units/test_past_competition.py new file mode 100644 index 000000000..8e7ef3010 --- /dev/null +++ b/tests/units/test_past_competition.py @@ -0,0 +1,32 @@ +import server + + +def test_purchase_futur_competition(client, mocker, clubs_fixture, competitions_fixture): + """ + GIVEN a connected secretary's club wants to book places in a futur competition. + date of this competition = "2023-02-05 10:00:00". + WHEN this secretary types: 5 places + THEN places are pruchased with status code:200. + """ + mocker.patch.object(server, 'clubs', clubs_fixture) + mocker.patch.object(server, 'competitions', competitions_fixture) + club = clubs_fixture[0]['name'] + competition = competitions_fixture[0]['name'] + response = client.post('/purchasePlaces', data={'competition': competition, 'club': club, 'places': 5}) + assert response.status_code == 200 + + +def test_purchase_past_competition(client, mocker, clubs_fixture, competitions_fixture): + """ + GIVEN a connected secretary's club wants to book places in a past competition. + date of this competition = "2021-05-12 09:00:00". + WHEN this secretary types: 5 places + THEN an error message displays, with status code:400 BAD REQUEST. + """ + mocker.patch.object(server, 'clubs', clubs_fixture) + mocker.patch.object(server, 'competitions', competitions_fixture) + club = clubs_fixture[0]['name'] + competition = competitions_fixture[1]['name'] + response = client.post('/purchasePlaces', data={'competition': competition, 'club': club, 'places': 5}) + assert response.status_code == 400 + assert b'Sorry, this competition is over !' in response.data