From d3e9eda937fffd6834b3a641dfb1297dd7367c31 Mon Sep 17 00:00:00 2001 From: yann Date: Mon, 30 Jun 2025 10:32:46 +0200 Subject: [PATCH 01/10] let this fix for issue6 --- server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/server.py b/server.py index 0a12660..b0d68c3 100644 --- a/server.py +++ b/server.py @@ -51,7 +51,6 @@ def purchasePlaces(): points = int(club['points']) if placesRequired <= points: competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired - club['points'] = int(club['points']) - placesRequired if competition['numberOfPlaces'] < 0: competition['numberOfPlaces'] = 0 flash('Great-booking complete!') From 1009ebfc1edf39e7066c9d046e437a971281bd6b Mon Sep 17 00:00:00 2001 From: yann Date: Mon, 30 Jun 2025 14:02:22 +0200 Subject: [PATCH 02/10] added 3 login tests --- Pipfile | 3 ++ Pipfile.lock | 84 +++++++++++++++++++++++++++++++++++++++- server.py | 14 ++++--- templates/welcome.html | 4 +- tests/__init__.py | 0 tests/conftest.py | 7 ++++ tests/test_connection.py | 23 +++++++++++ 7 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/conftest.py create mode 100644 tests/test_connection.py diff --git a/Pipfile b/Pipfile index 9581dc9..02375d7 100644 --- a/Pipfile +++ b/Pipfile @@ -10,6 +10,9 @@ itsdangerous = "==1.1.0" jinja2 = "==2.11.2" markupsafe = "==1.1.1" werkzeug = "==1.0.1" +pytest = "*" +pytest-flask = "*" +bs4 = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index ae1d86f..11b225f 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "55e68c2c6e7fb3f1685b9dfe66180ae537a367985700c08a86ccd664d56ccad7" + "sha256": "4d7a66df30afcd7f894d943098d49a686eaad5e68b7b19762f763bcb22c05499" }, "pipfile-spec": 6, "requires": { @@ -17,6 +17,22 @@ ] }, "default": { + "beautifulsoup4": { + "hashes": [ + "sha256:9bbbb14bfde9d79f38b8cd5f8c7c85f4b8f2523190ebed90e950a8dea4cb1c4b", + "sha256:dbb3c4e1ceae6aefebdaf2423247260cd062430a410e38c66f2baa50a8437195" + ], + "markers": "python_full_version >= '3.7.0'", + "version": "==4.13.4" + }, + "bs4": { + "hashes": [ + "sha256:a48685c58f50fe127722417bae83fe6badf500d54b55f7e39ffe43b798653925", + "sha256:abf8742c0805ef7f662dce4b51cca104cffe52b835238afc169142ab9b3fbccc" + ], + "index": "pypi", + "version": "==0.0.2" + }, "click": { "hashes": [ "sha256:d2b5255c7c6349bc1bd1e59e08cd12acbbd63ce649f2588755783aa94dfb6b1a", @@ -35,6 +51,14 @@ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'", "version": "==1.1.2" }, + "iniconfig": { + "hashes": [ + "sha256:3abbd2e30b36733fee78f9c7f7308f2d0050e88f0087fd25c2645f63c773e1c7", + "sha256:9deba5723312380e77435581c6bf4935c94cbfab9b1ed33ef8d238ea168eb760" + ], + "markers": "python_version >= '3.8'", + "version": "==2.1.0" + }, "itsdangerous": { "hashes": [ "sha256:321b033d07f2a4136d3ec762eac9f16a10ccd60f53c0c91af90217ace7ba1f19", @@ -112,6 +136,64 @@ "markers": "python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'", "version": "==1.1.1" }, + "packaging": { + "hashes": [ + "sha256:29572ef2b1f17581046b3a2227d5c611fb25ec70ca1ba8554b24b0e69331a484", + "sha256:d443872c98d677bf60f6a1f2f8c1cb748e8fe762d2bf9d3148b5599295b0fc4f" + ], + "markers": "python_version >= '3.8'", + "version": "==25.0" + }, + "pluggy": { + "hashes": [ + "sha256:7dcc130b76258d33b90f61b658791dede3486c3e6bfb003ee5c9bfb396dd22f3", + "sha256:e920276dd6813095e9377c0bc5566d94c932c33b27a3e3945d8389c374dd4746" + ], + "markers": "python_version >= '3.9'", + "version": "==1.6.0" + }, + "pygments": { + "hashes": [ + "sha256:636cb2477cec7f8952536970bc533bc43743542f70392ae026374600add5b887", + "sha256:86540386c03d588bb81d44bc3928634ff26449851e99741617ecb9037ee5ec0b" + ], + "markers": "python_version >= '3.8'", + "version": "==2.19.2" + }, + "pytest": { + "hashes": [ + "sha256:539c70ba6fcead8e78eebbf1115e8b589e7565830d7d006a8723f19ac8a0afb7", + "sha256:7c67fd69174877359ed9371ec3af8a3d2b04741818c51e5e99cc1742251fa93c" + ], + "index": "pypi", + "markers": "python_version >= '3.9'", + "version": "==8.4.1" + }, + "pytest-flask": { + "hashes": [ + "sha256:58be1c97b21ba3c4d47e0a7691eb41007748506c36bf51004f78df10691fa95e", + "sha256:c0e36e6b0fddc3b91c4362661db83fa694d1feb91fa505475be6732b5bc8c253" + ], + "index": "pypi", + "markers": "python_version >= '3.7'", + "version": "==1.3.0" + }, + "soupsieve": { + "hashes": [ + "sha256:6e60cc5c1ffaf1cebcc12e8188320b72071e922c2e897f737cadce79ad5d30c4", + "sha256:ad282f9b6926286d2ead4750552c8a6142bc4c783fd66b0293547c8fe6ae126a" + ], + "markers": "python_version >= '3.8'", + "version": "==2.7" + }, + "typing-extensions": { + "hashes": [ + "sha256:8676b788e32f02ab42d9e7c61324048ae4c6d844a399eebace3d4979d75ceef4", + "sha256:a1514509136dd0b477638fc68d6a91497af5076466ad0fa6c338e44e359944af" + ], + "markers": "python_version >= '3.9'", + "version": "==4.14.0" + }, "werkzeug": { "hashes": [ "sha256:2de2a5db0baeae7b2d2664949077c2ac63fbd16d98da0ff71837f7d1dea3fd43", diff --git a/server.py b/server.py index 4112082..4b7168b 100644 --- a/server.py +++ b/server.py @@ -26,11 +26,15 @@ def index(): @app.route('/showSummary',methods=['POST']) 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) - flash("The email isn't found") - return redirect(url_for('index')) + try: + club = [club for club in clubs if club['email'] == request.form['email']][0] + # if club: + return render_template('welcome.html', club=club, competitions=competitions) + # flash("Sorry, that email wasn't found") + # return redirect(url_for('index')) + except IndexError: + flash("Sorry, that email wasn't found") + return redirect(url_for('index')) @app.route('/book//') def book(competition,club): diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261..d9e8d2f 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 %} @@ -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..dde30f7 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,7 @@ +import pytest +from server import app + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..7b6d106 --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,23 @@ +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}) + data = response.data.decode() + 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}) + soup = BeautifulSoup(response.data, 'html.parser') + assert soup.h2.text == "Welcome, admin@irontemple.com" + From 40226a45a64b0b79c71124644728c0ef8d56ca3e Mon Sep 17 00:00:00 2001 From: yann Date: Tue, 1 Jul 2025 09:52:24 +0200 Subject: [PATCH 03/10] removed print, unused decode --- tests/test_connection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 7b6d106..b5e20ed 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -10,7 +10,6 @@ def test_should_status_code_ok(client): def test_should_display_sorry_with_unknown_email(client): email = "test@test.com" response = client.post('/showSummary', data={"email": email}) - data = response.data.decode() assert "_flashes" in session assert session["_flashes"] == [("message", "Sorry, that email wasn't found")] @@ -19,5 +18,5 @@ def test_shoul_display_page_on_known_email(client): email = "admin@irontemple.com" response = client.post('/showSummary', data={"email": email}) soup = BeautifulSoup(response.data, 'html.parser') - assert soup.h2.text == "Welcome, admin@irontemple.com" + assert soup.h2.text == "Welcome, "+email From 90cae5ffc98b3922e91d37b7d4cc8cef188ceec1 Mon Sep 17 00:00:00 2001 From: yann Date: Wed, 2 Jul 2025 11:36:51 +0200 Subject: [PATCH 04/10] first try with fixture for login --- tests/conftest.py | 17 +++++++++++++++++ tests/test_connection.py | 23 +++++++++++++++++++++++ tests/test_purchase.py | 5 +++++ 3 files changed, 45 insertions(+) create mode 100644 tests/conftest.py create mode 100644 tests/test_connection.py create mode 100644 tests/test_purchase.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..50e7972 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,17 @@ +import pytest +from server import app + +EMAIL1 = "admin@irontemple.com" +EMAIL2 = "john@simplylift.co" + +@pytest.fixture +def client(): + with app.test_client() as client: + yield client + + +@pytest.fixture +def connect(client): + response = client.post('/showSummary', data={"email": EMAIL}) + soup = BeautifulSoup(response.data, 'html.parser') + diff --git a/tests/test_connection.py b/tests/test_connection.py new file mode 100644 index 0000000..22b664f --- /dev/null +++ b/tests/test_connection.py @@ -0,0 +1,23 @@ +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}) + 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}) + 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..abb03e3 --- /dev/null +++ b/tests/test_purchase.py @@ -0,0 +1,5 @@ +from bs4 import BeautifulSoup + + +def test_should_not_when_try_more_points_than_available(connect): + print(response.data) From b34382c46c8f4781e0ba130786f7a3217b168a51 Mon Sep 17 00:00:00 2001 From: yann Date: Wed, 2 Jul 2025 15:06:46 +0200 Subject: [PATCH 05/10] first try to get session cookie --- tests/conftest.py | 5 ++++- tests/test_purchase.py | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 50e7972..50ec543 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,7 @@ import pytest from server import app +from bs4 import BeautifulSoup + EMAIL1 = "admin@irontemple.com" EMAIL2 = "john@simplylift.co" @@ -12,6 +14,7 @@ def client(): @pytest.fixture def connect(client): - response = client.post('/showSummary', data={"email": EMAIL}) + response = client.post('/showSummary', data={"email": EMAIL1}) soup = BeautifulSoup(response.data, 'html.parser') + return soup diff --git a/tests/test_purchase.py b/tests/test_purchase.py index abb03e3..15b0926 100644 --- a/tests/test_purchase.py +++ b/tests/test_purchase.py @@ -1,5 +1,13 @@ from bs4 import BeautifulSoup +from flask import session -def test_should_not_when_try_more_points_than_available(connect): - print(response.data) +def test_should_not_when_try_more_points_than_available(client): + data = {"competition": "Spring Festival", "club": "Iron Temple", "places": "5"} + + # response = client.post('/book/Spring%20Festival/Iron%20Temple', data=data) + response = client.post('/purchasePlaces', data=data) + # print(BeautifulSoup(response.data, "html.parser")) + print(session) + #assert "_flashes" in session + #assert session["_flashes"] == [("message", "You don't have enough points")] From 9705789809020189069ea1aa9e861b2249f4e860 Mon Sep 17 00:00:00 2001 From: yann Date: Wed, 2 Jul 2025 15:07:28 +0200 Subject: [PATCH 06/10] works better with init --- tests/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/__init__.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 From 813b7849fe65be24b29b9ba01a7cb2726d68fb2e Mon Sep 17 00:00:00 2001 From: yann Date: Wed, 2 Jul 2025 17:27:06 +0200 Subject: [PATCH 07/10] test Ok on soup --- tests/test_purchase.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/test_purchase.py b/tests/test_purchase.py index 15b0926..350b7a4 100644 --- a/tests/test_purchase.py +++ b/tests/test_purchase.py @@ -3,11 +3,9 @@ from flask import session def test_should_not_when_try_more_points_than_available(client): - data = {"competition": "Spring Festival", "club": "Iron Temple", "places": "5"} - - # response = client.post('/book/Spring%20Festival/Iron%20Temple', data=data) + data = {"competition": "Spring Festival", "club": "Iron Temple", "places": "6"} response = client.post('/purchasePlaces', data=data) - # print(BeautifulSoup(response.data, "html.parser")) - print(session) - #assert "_flashes" in session - #assert session["_flashes"] == [("message", "You don't have enough points")] + soup = BeautifulSoup(response.data, "html.parser") + print(soup.li.text) + assert "You don't have enough points" == soup.li.text + From 0370de60e0a7c27ada0c27202d91c01d2873f448 Mon Sep 17 00:00:00 2001 From: yann Date: Thu, 3 Jul 2025 11:41:17 +0200 Subject: [PATCH 08/10] added ok and nok on points --- server.py | 7 +++---- templates/welcome.html | 6 +++--- tests/conftest.py | 10 +++++++++- tests/test_connection.py | 2 ++ tests/test_purchase.py | 20 ++++++++++++++------ 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/server.py b/server.py index b0d68c3..f9c76cd 100644 --- a/server.py +++ b/server.py @@ -29,7 +29,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) - flash("The email isn't found") + flash("Sorry, that email wasn't found") return redirect(url_for('index')) @app.route('/book//') @@ -53,11 +53,10 @@ def purchasePlaces(): competition['numberOfPlaces'] = int(competition['numberOfPlaces']) - placesRequired if competition['numberOfPlaces'] < 0: competition['numberOfPlaces'] = 0 - flash('Great-booking complete!') + flash("Great-booking complete!") else: flash("You don't have enough points") - return render_template('welcome.html', club=club, - competitions=competitions) + return render_template('welcome.html', club=club, competitions=competitions) diff --git a/templates/welcome.html b/templates/welcome.html index ff6b261..da2841d 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/conftest.py b/tests/conftest.py index 50ec543..ab309ae 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,13 +5,21 @@ 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}) diff --git a/tests/test_connection.py b/tests/test_connection.py index 22b664f..50c2f9b 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -10,6 +10,7 @@ def test_should_status_code_ok(client): 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")] @@ -17,6 +18,7 @@ def test_should_display_sorry_with_unknown_email(client): 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 index 350b7a4..3dbca62 100644 --- a/tests/test_purchase.py +++ b/tests/test_purchase.py @@ -2,10 +2,18 @@ from bs4 import BeautifulSoup from flask import session -def test_should_not_when_try_more_points_than_available(client): - data = {"competition": "Spring Festival", "club": "Iron Temple", "places": "6"} - response = client.post('/purchasePlaces', data=data) - soup = BeautifulSoup(response.data, "html.parser") - print(soup.li.text) - assert "You don't have enough points" == soup.li.text +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) + data = club1.update({"places": points-1}) + response = client.post('/purchasePlaces', data=club1) + soup = BeautifulSoup(response.data, "html.parser") + assert "Great-booking complete!" == soup.li.text From 79a6b00401c599d1ce4cb7b5ea5c5d8ede3c7310 Mon Sep 17 00:00:00 2001 From: yann Date: Mon, 7 Jul 2025 10:27:54 +0200 Subject: [PATCH 09/10] removed print --- tests/test_connection.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 50c2f9b..22b664f 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -10,7 +10,6 @@ def test_should_status_code_ok(client): 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")] @@ -18,7 +17,6 @@ def test_should_display_sorry_with_unknown_email(client): 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 From 798a9a0d9181232ada3bb2ad5849aa052df24b01 Mon Sep 17 00:00:00 2001 From: yann Date: Mon, 7 Jul 2025 10:59:30 +0200 Subject: [PATCH 10/10] add coverage config --- .coveragerc | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..c712d25 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit = tests/*