From be366bec1a434736fbc4b561103adfa9e6e08db2 Mon Sep 17 00:00:00 2001 From: Imenbr Date: Thu, 5 Sep 2024 11:40:45 +0200 Subject: [PATCH] (fix) Handle booking more 12 places --- server.py | 16 ++++-- templates/booking.html | 2 +- .../test_clubs_should_not_use_more_than_12.py | 15 +++++ ...s_should_not_use_more_than_their_points.py | 2 +- .../test_clubs_should_not_use_more_than_12.py | 57 +++++++++++++++++++ ...s_should_not_use_more_than_their_points.py | 2 +- 6 files changed, 87 insertions(+), 7 deletions(-) create mode 100644 tests/integration_tests/test_clubs_should_not_use_more_than_12.py create mode 100644 tests/unit_tests/test_clubs_should_not_use_more_than_12.py diff --git a/server.py b/server.py index 3f7669913..188eec454 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,10 @@ import json from flask import Flask, render_template, request, redirect, flash, url_for +app = Flask(__name__) +app.secret_key = "something_special" +app.config["COMPETITIONS_FILE"] = "competitions.json" + def loadClubs(): with open("clubs.json") as c: @@ -9,14 +13,11 @@ def loadClubs(): def loadCompetitions(): - with open("competitions.json") as comps: + with open(app.config["COMPETITIONS_FILE"], "r") as comps: listOfCompetitions = json.load(comps)["competitions"] return listOfCompetitions -app = Flask(__name__) -app.secret_key = "something_special" - competitions = loadCompetitions() clubs = loadClubs() @@ -86,6 +87,9 @@ def get_club_from_name(name): def check_places(places, club): if not places or int(places) < 1: return "Places required must be a positive integer" + if int(places) > 12: + return ("Places required must be a positive integer " + "that does not exceed 12") if int(places) > int(club["points"]): return "Places required exceed club's total points" @@ -127,3 +131,7 @@ def purchasePlaces(): @app.route("/logout") def logout(): return redirect(url_for("index")) + + +if __name__ == "__main__": + app.run(debug=True) diff --git a/templates/booking.html b/templates/booking.html index 5037049c1..b0f4938ff 100644 --- a/templates/booking.html +++ b/templates/booking.html @@ -20,7 +20,7 @@

{{competition['name']}}

- +
diff --git a/tests/integration_tests/test_clubs_should_not_use_more_than_12.py b/tests/integration_tests/test_clubs_should_not_use_more_than_12.py new file mode 100644 index 000000000..4780a5440 --- /dev/null +++ b/tests/integration_tests/test_clubs_should_not_use_more_than_12.py @@ -0,0 +1,15 @@ + +def test_purchase_places_exceeds_12(client): + """ + Test purchasing more places than 12. + """ + response = client.post("/purchasePlaces", data={ + "competition": "Competition 1", + "club": "Club 1", + "places": "13" + }) + + assert response.status_code == 302 # Redirect expected + response = client.get(response.headers["Location"]) # Follow the redirect + assert b"Places required must be a positive integer" + b" that does not exceed 12" in response.data diff --git a/tests/integration_tests/test_clubs_should_not_use_more_than_their_points.py b/tests/integration_tests/test_clubs_should_not_use_more_than_their_points.py index 985ae9115..c762890a3 100644 --- a/tests/integration_tests/test_clubs_should_not_use_more_than_their_points.py +++ b/tests/integration_tests/test_clubs_should_not_use_more_than_their_points.py @@ -21,7 +21,7 @@ def test_purchase_places_insufficient_points(client): response = client.post("/purchasePlaces", data={ "competition": "Competition 1", "club": "Club 1", - "places": "20" # Club 1 has only 10 points + "places": "11" # Club 1 has only 10 points }) assert response.status_code == 302 # Redirect expected diff --git a/tests/unit_tests/test_clubs_should_not_use_more_than_12.py b/tests/unit_tests/test_clubs_should_not_use_more_than_12.py new file mode 100644 index 000000000..a7c4bfff4 --- /dev/null +++ b/tests/unit_tests/test_clubs_should_not_use_more_than_12.py @@ -0,0 +1,57 @@ +from unittest.mock import patch +from .utils import mock_clubs_list, mock_competitions_list + + +def test_check_places_invalid_more_than_12(): + from server import check_places + + error = check_places("13", mock_clubs_list[0]) + assert error == ("Places required must be a positive" + " integer that does not exceed 12") + + +# Unit test for booking with error in places +@patch("server.redirect") +@patch("server.url_for") +@patch("server.flash") +@patch("server.check_places") +@patch("server.get_club_from_name") +@patch("server.get_competition_from_name") +def test_purchase_places_invalid_places( + mock_get_competition, + mock_get_club, + mock_check_places, + mock_flash, + mock_url_for, + mock_redirect, + client, +): + # Mock the functions to simulate an error in places + mock_get_competition.return_value = mock_competitions_list[0] + mock_get_club.return_value = mock_clubs_list[0] + mock_check_places.return_value = ( + "Places required must be a positive integer that does not exceed 12" + ) + + # Mock the redirect and url_for to return a redirect response + mock_url_for.return_value = "/book" + + # Simulate POST request to the route + client.post( + "/purchasePlaces", + data={"competition": "Competition 1", "club": "Club 1", "places": "20"}, + ) + + # Assert that the necessary functions are called with the correct parameters + mock_get_competition.assert_called_once_with("Competition 1") + mock_get_club.assert_called_once_with("Club 1") + mock_check_places.assert_called_once_with("20", mock_clubs_list[0]) + mock_flash.assert_called_once_with( + "Places required must be a positive integer that does not exceed 12" + ) + mock_url_for.assert_called_once_with( + "book", + competition=mock_competitions_list[0]["name"], + club=mock_clubs_list[0]["name"], + ) + mock_redirect.assert_called_once_with("/book") diff --git a/tests/unit_tests/test_clubs_should_not_use_more_than_their_points.py b/tests/unit_tests/test_clubs_should_not_use_more_than_their_points.py index 5170fd669..0b30e47b0 100644 --- a/tests/unit_tests/test_clubs_should_not_use_more_than_their_points.py +++ b/tests/unit_tests/test_clubs_should_not_use_more_than_their_points.py @@ -63,7 +63,7 @@ def test_check_places_invalid_negative(): def test_check_places_exceeds_points(): from server import check_places - error = check_places("20", mock_clubs_list[0]) + error = check_places("11", mock_clubs_list[0]) assert error == "Places required exceed club's total points"