From 20736bd00156fad2e6e315a451c8b3e5bb0fb667 Mon Sep 17 00:00:00 2001 From: Imenbr Date: Wed, 29 May 2024 17:32:43 +0200 Subject: [PATCH] (fix) handle unknown email in login --- .coverage | Bin 0 -> 53248 bytes .coveragerc | 6 ++ .flake8 | 12 +++ .gitignore | 5 +- pyproject.toml | 2 + pytest.ini | 3 + requirements.txt | 3 + server.py | 81 +++++++++----- templates/index.html | 13 ++- templates/welcome.html | 6 +- tests/__init__.py | 0 tests/integration_tests/__init__.py | 0 tests/integration_tests/conftest.py | 48 +++++++++ tests/integration_tests/test_unknown_email.py | 24 +++++ tests/unit_tests/__init__.py | 0 tests/unit_tests/conftest.py | 8 ++ tests/unit_tests/test_unknown_email.py | 101 ++++++++++++++++++ tests/unit_tests/utils.py | 23 ++++ 18 files changed, 301 insertions(+), 34 deletions(-) create mode 100644 .coverage create mode 100644 .coveragerc create mode 100644 .flake8 create mode 100644 pyproject.toml create mode 100644 pytest.ini create mode 100644 tests/__init__.py create mode 100644 tests/integration_tests/__init__.py create mode 100644 tests/integration_tests/conftest.py create mode 100644 tests/integration_tests/test_unknown_email.py create mode 100644 tests/unit_tests/__init__.py create mode 100644 tests/unit_tests/conftest.py create mode 100644 tests/unit_tests/test_unknown_email.py create mode 100644 tests/unit_tests/utils.py diff --git a/.coverage b/.coverage new file mode 100644 index 0000000000000000000000000000000000000000..e361004f7f162b57579d9fcceca1f3ab6d62707d GIT binary patch literal 53248 zcmeI)+i%-c90zdQr%RSJ!9&xCs%rEI3EHY{LN&25co+rxFfnNx6kgECoaChOi0#aF z*5(0C-H;|B@dtnq{3-kkpz%KO0(+UzB$$wfRDM5Sa@m@#+RL=&YvtnD=NzB&Ikz|$ ze{%7h9V*%Me8&pq3GIlc>)Lx#YMPd#dy($xlBJcY^aVZZL+j&Kb6WYzFGce+t&sUv zGruUFH&+Tj6yKQswczBwnf@_rP5R!J>?mJ0?Y5G(+pz0aXuF#-Y^gMKH&6|p z=x7^FbsVr9#pkuE-Jn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in competitions if c['name'] == competition][0] +@app.route("/book//") +def book(competition, club): + 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) + 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 + ) -@app.route('/purchasePlaces',methods=['POST']) +@app.route("/purchasePlaces", methods=["POST"]) 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) + 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) # TODO: Add route for points display -@app.route('/logout') +@app.route("/logout") def logout(): - return redirect(url_for('index')) \ No newline at end of file + return redirect(url_for("index")) diff --git a/templates/index.html b/templates/index.html index 926526b7d..0d9e52c9f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,3 +1,4 @@ + @@ -6,10 +7,20 @@

Welcome to the GUDLFT Registration Portal!

+ {% with messages = get_flashed_messages() %} + {% if messages %} +
    + {% for message in messages %} +
  • {{message}}
  • + {% endfor %} +
+ {% endif %} + {% endwith %} + Please enter your secretary email to continue:
- +
diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261a2..db7c1e47e 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,6 +15,7 @@

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

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

Competitions:

    @@ -30,7 +31,6 @@

    Competitions:


    {% endfor %}
- {%endwith%} - + {% endwith %} \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/integration_tests/__init__.py b/tests/integration_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/integration_tests/conftest.py b/tests/integration_tests/conftest.py new file mode 100644 index 000000000..4cf8514f0 --- /dev/null +++ b/tests/integration_tests/conftest.py @@ -0,0 +1,48 @@ +import json +import pytest +from unittest.mock import mock_open, patch + +# Mock data for clubs.json and competitions.json +mock_clubs_json = json.dumps( + { + "clubs": [ + {"name": "Club 1", "email": "club1@example.com"}, + {"name": "Club 2", "email": "club2@example.com"}, + ] + } +) + +mock_competitions_json = json.dumps( + { + "competitions": [ + {"name": "Competition 1", "numberOfPlaces": "25"}, + {"name": "Competition 2", "numberOfPlaces": "15"}, + ] + } +) + + +def mocked_open(file, *args, **kwargs): + """ + Mock open function to return mock data for + clubs.json and competitions.json + """ + if file == "clubs.json": + return mock_open(read_data=mock_clubs_json)() + elif file == "competitions.json": + return mock_open(read_data=mock_competitions_json)() + else: + raise FileNotFoundError(f"File {file} not found") + + +# Patch open before importing the app to ensure clubs +# and competitions are loaded with mock data +with patch("builtins.open", side_effect=mocked_open): + from server import app # Import app after patching + + +@pytest.fixture +def client(): + app.config["TESTING"] = True + with app.test_client() as client: + yield client diff --git a/tests/integration_tests/test_unknown_email.py b/tests/integration_tests/test_unknown_email.py new file mode 100644 index 000000000..f77e75899 --- /dev/null +++ b/tests/integration_tests/test_unknown_email.py @@ -0,0 +1,24 @@ + +# Integration test for valid email +def test_showSummary_valid_email_integration(client): + # Simulate POST request with a valid email + response = client.post("/showSummary", data={"email": "club1@example.com"}) + + # Check if the welcome page is rendered with the correct club data + assert response.status_code == 200 + assert b"Welcome" in response.data + + +# Integration test for invalid email +def test_showSummary_invalid_email_integration(client): + # Simulate POST request with an invalid email + response = client.post( + "/showSummary", data={"email": "invalid@example.com"} + ) + + # Validate that the response redirects to the index page + assert response.status_code == 302 + response = client.get(response.headers["Location"]) # Follow the redirect + + # Check if the flash message appears in the redirected page + assert b"Sorry, that email wasn't found." in response.data diff --git a/tests/unit_tests/__init__.py b/tests/unit_tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit_tests/conftest.py b/tests/unit_tests/conftest.py new file mode 100644 index 000000000..1c3ff9b09 --- /dev/null +++ b/tests/unit_tests/conftest.py @@ -0,0 +1,8 @@ +import pytest +from server import app + + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client diff --git a/tests/unit_tests/test_unknown_email.py b/tests/unit_tests/test_unknown_email.py new file mode 100644 index 000000000..d0bd5446e --- /dev/null +++ b/tests/unit_tests/test_unknown_email.py @@ -0,0 +1,101 @@ +import json +from unittest.mock import mock_open, patch + +from server import get_club_from_email, loadClubs, loadCompetitions +from .utils import ( + mock_clubs_list, + mock_competitions_list, + mock_clubs_json, + mock_competitions_json, +) + + +# Test for loadClubs function +@patch("builtins.open", new_callable=mock_open, read_data=mock_clubs_json) +@patch("server.json.load") +def test_loadClubs(mock_json_load, mock_file): + # Mock json.load to return the parsed data + mock_json_load.return_value = json.loads(mock_clubs_json) + + # Call loadClubs and check if it returns the correct data + clubs = loadClubs() + assert len(clubs) == 2 + assert clubs[0]["name"] == "Club 1" + assert clubs[1]["email"] == "club2@example.com" + + +# Test for loadCompetitions function +@patch( + "builtins.open", new_callable=mock_open, read_data=mock_competitions_json +) +@patch("server.json.load") +def test_loadCompetitions(mock_json_load, mock_file): + # Mock json.load to return the parsed data + mock_json_load.return_value = json.loads(mock_competitions_json) + + # Call loadCompetitions and check if it returns the correct data + competitions = loadCompetitions() + assert len(competitions) == 2 + assert competitions[0]["name"] == "Competition 1" + assert competitions[1]["numberOfPlaces"] == "15" + + +@patch("server.clubs", mock_clubs_list) +def test_get_club_from_email_valid(): + club = get_club_from_email("club1@example.com") + assert club is not None + assert club["name"] == "Club 1" + + +@patch("server.clubs", mock_clubs_list) +def test_get_club_from_email_invalid(): + club = get_club_from_email("invalid@example.com") + assert club is None + + +# Unit test for showSummary with valid email +@patch( + "server.competitions", mock_competitions_list +) # Directly patch the value of competitions +@patch("server.get_club_from_email") +@patch("server.render_template") +def test_showSummary_valid_email(mock_render_template, mock_get_club, client): + # Mock get_club_from_email to return a valid club + mock_get_club.return_value = mock_clubs_list[0] + + # Simulate POST request with a valid email + response = client.post("/showSummary", data={"email": "club1@example.com"}) + + # Check if render_template was called with the correct arguments + mock_render_template.assert_called_once_with( + "welcome.html", + club=mock_clubs_list[0], + competitions=mock_competitions_list, + ) + assert response.status_code == 200 + + +# Unit test for showSummary with invalid email +@patch("server.get_club_from_email") +@patch("server.flash") +@patch("server.redirect") +@patch("server.url_for") +def test_showSummary_invalid_email( + mock_url_for, mock_redirect, mock_flash, mock_get_club, client +): + # Mock get_club_from_email to return None (simulating an invalid email) + mock_get_club.return_value = None + + # Mock url_for to return a URL for the index page + mock_url_for.return_value = "/" + + # Simulate POST request with an invalid email + client.post( + "/showSummary", data={"email": "invalid@example.com"} + ) + + # Check if flash was called with the correct message + mock_flash.assert_called_once_with("Sorry, that email wasn't found.") + + # Check if redirect was called with the correct arguments + mock_redirect.assert_called_once_with("/") diff --git a/tests/unit_tests/utils.py b/tests/unit_tests/utils.py new file mode 100644 index 000000000..3a095deab --- /dev/null +++ b/tests/unit_tests/utils.py @@ -0,0 +1,23 @@ + +import json + + +# Mock data for clubs and competitions +mock_clubs_list = [ + {"name": "Club 1", "email": "club1@example.com"}, + {"name": "Club 2", "email": "club2@example.com"} +] + +mock_competitions_list = [ + {"name": "Competition 1", "numberOfPlaces": "25"}, + {"name": "Competition 2", "numberOfPlaces": "15"} +] + +# Mock data for clubs.json and competitions.json +mock_clubs_json = json.dumps({ + "clubs": mock_clubs_list +}) + +mock_competitions_json = json.dumps({ + "competitions": mock_competitions_list +})