diff --git a/.gitignore b/.gitignore index 2cba99d87..07ede88a6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,7 @@ lib .Python tests/ .envrc -__pycache__ \ No newline at end of file +__pycache__ +.env +.vscode/ +env/ \ No newline at end of file diff --git a/conftest.py b/conftest.py new file mode 100644 index 000000000..af2edad74 --- /dev/null +++ b/conftest.py @@ -0,0 +1,12 @@ +import pytest +from server import app + + +@pytest.fixture +def client(): + # active le mode test de Flask + app.config["TESTING"] = True + # crée un faux navigateur + with app.test_client() as client: + # donne ce navigateur aux tests + yield client \ No newline at end of file diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 000000000..b0e5a945f --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +filterwarnings = + ignore::DeprecationWarning \ No newline at end of file diff --git a/server.py b/server.py index 4084baeac..18c67ce57 100644 --- a/server.py +++ b/server.py @@ -13,7 +13,7 @@ def loadCompetitions(): listOfCompetitions = json.load(comps)['competitions'] return listOfCompetitions - +a = 1 app = Flask(__name__) app.secret_key = 'something_special' @@ -26,8 +26,15 @@ 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) + club = [club for club in clubs if club['email'] == request.form['email']] + + if not club: + flash("Sorry, that email was not found") + return redirect(url_for("index")) + + return render_template('welcome.html', + club=club[0], + competitions=competitions) @app.route('/book//') @@ -46,9 +53,15 @@ 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']) - competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired - flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + + point_club = int(club['points']) + if point_club < placesRequired: + flash('You are not authorized to book this number of places!') + return render_template('welcome.html', club=club, competitions=competitions) + else: + flash('Great-booking complete!') + competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired + return render_template('welcome.html', club=club, competitions=competitions) # TODO: Add route for points display diff --git a/templates/index.html b/templates/index.html index 926526b7d..b7ae99c98 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,15 @@

Welcome to the GUDLFT Registration Portal!

Please enter your secretary email to continue: + + {% with messages = get_flashed_messages() %} + {% if messages %} + {% for message in messages %} +

{{ message }}

+ {% endfor %} + {% endif %} + {% endwith %} +
diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261a2..d2ea76a30 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -5,7 +5,8 @@ Summary | GUDLFT Registration -

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

Logout +

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

Logout {% with messages = get_flashed_messages()%} {% if messages %} @@ -15,7 +16,9 @@

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

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

Competitions:

    {% for comp in competitions%} diff --git a/test/test_server.py b/test/test_server.py new file mode 100644 index 000000000..e2f845a9b --- /dev/null +++ b/test/test_server.py @@ -0,0 +1,41 @@ + +def test_show_summary_with_valid_email(client): + response = client.post("/showSummary", data={ + "email": "admin@irontemple.com" + }) + + assert response.status_code == 200 + assert b"Welcome" in response.data + +def test_show_summary_with_unknown_email(client): + response = client.post("/showSummary", data={ + "email": "unknown@test.com" + }, follow_redirects=True) + + assert response.status_code == 200 + assert b"Sorry, that email was not found" in response.data + + + +def test_purchase_places_with_valid_number_of_places(client): + response = client.post("/purchasePlaces", data={ + "competition": "Spring Festival", + "club": "Iron Temple", + "places": "2" + }, follow_redirects=True) + + assert response.status_code == 200 + assert b"Great-booking complete!" in response.data + +def test_purchase_places_with_too_many_places(client): + response = client.post("/purchasePlaces", data={ + "competition": "Spring Festival", + "club": "Iron Temple", + "places": "5", + }) + + assert response.status_code == 200 + assert b"You are not authorized to book this number of places" in response.data + + +