From ada3a5cc761ef98f3a7dcf5954c8b189f6df17d9 Mon Sep 17 00:00:00 2001 From: yann Date: Fri, 4 Jul 2025 15:38:55 +0200 Subject: [PATCH] test old competition hasn`t link to book --- clubs.json | 4 ++-- competitions.json | 2 +- server.py | 5 ++-- templates/welcome.html | 6 ++--- tests/__init__.py | 0 tests/conftest.py | 31 +++++++++++++++++++++++++ tests/test_connection.py | 27 ++++++++++++++++++++++ tests/test_purchase.py | 49 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_connection.py create mode 100644 tests/test_purchase.py diff --git a/clubs.json b/clubs.json index 46bed31..b3800e6 100644 --- a/clubs.json +++ b/clubs.json @@ -7,10 +7,10 @@ { "name":"Iron Temple", "email": "admin@irontemple.com", - "points":"47" + "points":"4" }, { "name":"She Lifts", "email": "kate@shelifts.co.uk", "points":"12" } -]} \ No newline at end of file +]} diff --git a/competitions.json b/competitions.json index 039fc61..ea636df 100644 --- a/competitions.json +++ b/competitions.json @@ -7,7 +7,7 @@ }, { "name": "Fall Classic", - "date": "2020-10-22 13:30:00", + "date": "2025-10-22 13:30:00", "numberOfPlaces": "13" } ] diff --git a/server.py b/server.py index bc1b2d7..fa42478 100644 --- a/server.py +++ b/server.py @@ -32,7 +32,7 @@ def showSummary(): club = [club for club in clubs if club['email'] == request.form['email']] if club: return render_template('welcome.html', club=club[0], competitions=competitions, now=now) - flash("The email isn't found") + flash("Sorry, that email wasn't found") return redirect(url_for('index')) @app.route('/book//') @@ -65,10 +65,11 @@ def purchasePlaces(): if places[competition['name']] <= 12: if placesRequired <= points: competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired - print(club['points']) if not competition['name'] in session: session[competition['name']] = placesRequired flash(f"Great ! {placesRequired} places booked for {competition['name']}") + # flash('Great-booking complete!') + else: flash("You don't have enough points") else: diff --git a/templates/welcome.html b/templates/welcome.html index 642c93e..2116d89 100644 --- a/templates/welcome.html +++ b/templates/welcome.html @@ -5,7 +5,7 @@ Summary | GUDLFT Registration -

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

Logout +

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

Logout {% with messages = get_flashed_messages()%} {% if messages %} @@ -15,7 +15,7 @@ {% endfor %} {% endif%} - Points available: {{club['points']}} + Points available: {{club['points']}}

Competitions:

    {% for comp in competitions%} @@ -33,4 +33,4 @@ {%endwith%} - \ No newline at end of file + diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..b06c74b --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,31 @@ +import pytest +from server import app +from bs4 import BeautifulSoup + + +EMAIL1 = "admin@irontemple.com" +EMAIL2 = "john@simplylift.co" + + +@pytest.fixture +def club1(): + data = {"competition": "Spring Festival", "club": "Iron Temple"} + return data + +@pytest.fixture +def club2(): + data = {"competition": "Fall Classic", "club": "Iron Temple"} + return data + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client + +@pytest.fixture +def connect(client): + response = client.post('/showSummary', data={"email": EMAIL1}) + soup = BeautifulSoup(response.data, 'html.parser') + return soup + + diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..5db9bfd --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,27 @@ +from flask import session +from bs4 import BeautifulSoup + + +def test_should_status_code_ok(client): + response = client.get('/') + assert response.status_code == 200 + + +def test_should_display_sorry_with_unknown_email(client): + email = "test@test.com" + response = client.post('/showSummary', data={"email": email}) + print(session) + assert "_flashes" in session + assert session["_flashes"] == [("message", "Sorry, that email wasn't found")] + + +def test_shoul_display_page_on_known_email(client): + email = "admin@irontemple.com" + response = client.post('/showSummary', data={"email": email}) + print(session) + soup = BeautifulSoup(response.data, 'html.parser') + assert soup.h2.text == "Welcome, "+email + + + + diff --git a/tests/test_purchase.py b/tests/test_purchase.py new file mode 100644 index 0000000..7002b79 --- /dev/null +++ b/tests/test_purchase.py @@ -0,0 +1,49 @@ +from bs4 import BeautifulSoup +from flask import session + + +class TestPoints: + + def test_should_nok_when_too_much_points(self, client, connect, club1): + points = int(connect.span.text) + club1.update({"places": points+1}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + assert "You don't have enough points" == soup.li.text + + def test_should_ok_when_enough_points(self, client, connect, club1): + points = int(connect.span.text) + club1.update({"places": points-1}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + # assert "Great-booking complete!" == soup.li.text + assert f"Great ! "+str(points-1)+" places booked for "+club1['competition'] == soup.li.text + + +class TestPlaces: + + def test_should_refuse_more_12_once(self, client, club1): + club1.update({"places": 13}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + assert "You can't book more than 12 places" == soup.li.text + + + def test_should_refuse_more_12_total(self, client, club1): + club1.update({"places": 2}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + assert "Great ! 2 places booked for "+club1['competition'] == soup.li.text + club1.update({"places": 12}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + assert "You already booked 12 places for "+club1['competition'] == soup.li.text + + +class TestDate: + + def test_should_not_display_book_link_for_past_competitions(self, connect): + li = connect.find_all("li") + assert not li[0].a + assert li[1].a +