diff --git a/.gitignore b/.gitignore index 950060a15..23264e70d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,9 @@ include lib .Python .envrc -__pycache__ \ No newline at end of file +__pycache__.DS_Store +env/ +__pycache__/ +*.pyc +.DS_Store +.pytest_cache/ \ No newline at end of file diff --git a/server.py b/server.py index a58f8b437..1e874403b 100644 --- a/server.py +++ b/server.py @@ -50,6 +50,11 @@ def purchasePlaces(): 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 placesRequired > int(club['points']): + flash("You do not have enough points to book that many places.") + return render_template('welcome.html', club=club, competitions=competitions) + competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired club['points'] = int(club['points']) - placesRequired flash('Great-booking complete!') diff --git a/tests/integration/test_purchase.py b/tests/integration/test_purchase.py index 39748596c..6923ffe0a 100644 --- a/tests/integration/test_purchase.py +++ b/tests/integration/test_purchase.py @@ -22,4 +22,29 @@ def test_purchase_deducts_points_from_club(client): ) points_apres = int(club['points']) - assert points_apres == points_avant - places_reservees \ No newline at end of file + assert points_apres == points_avant - places_reservees + +def test_purchase_more_than_points_is_blocked(client): + """Bug #2 : un club ne peut pas reserver plus de places qu'il n'a de points.""" + club = next(c for c in server.clubs if c['name'] == "Iron Temple") # 4 points + competition = next( + c for c in server.competitions if c['name'] == "Spring Festival" + ) + + points_avant = int(club['points']) + places_demandees = points_avant + 5 # plus que le solde : impossible + + response = client.post( + "/purchasePlaces", + data={ + "competition": competition['name'], + "club": club['name'], + "places": str(places_demandees), + }, + follow_redirects=True, + ) + + # Les points ne doivent pas avoir changé (rien n'a ete reserve) + assert int(club['points']) == points_avant + # Et un message d'erreur doit apparaitre + assert b"do not have enough points" in response.data \ No newline at end of file