From c259388cb608ace0e9cc7865e53c22793184cff7 Mon Sep 17 00:00:00 2001 From: zionhigt Date: Sun, 27 Mar 2022 12:36:45 +0200 Subject: [PATCH 1/3] includes tests --- .gitignore | 1 - tests/__init__.py | 0 tests/unit_test/__init__.py | 0 tests/unit_test/conftest.py | 32 +++++++++++++++++++ tests/unit_test/mocks_files/fixture_load.json | 16 ++++++++++ tests/unit_test/test_server.py | 20 ++++++++++++ 6 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/unit_test/__init__.py create mode 100644 tests/unit_test/conftest.py create mode 100644 tests/unit_test/mocks_files/fixture_load.json create mode 100644 tests/unit_test/test_server.py diff --git a/.gitignore b/.gitignore index 1007cb110..8b4d1421a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ bin include lib .Python -tests/ .envrc __pycache__ launch_flask.bat diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit_test/__init__.py b/tests/unit_test/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/unit_test/conftest.py b/tests/unit_test/conftest.py new file mode 100644 index 000000000..82868db79 --- /dev/null +++ b/tests/unit_test/conftest.py @@ -0,0 +1,32 @@ +import pytest + +from server import app + +@pytest.fixture(scope="class") +def client(request): + with app.test_client() as client: + request.cls.client = client + + +@pytest.fixture(scope="class") +def clubs(request): + fixture = [ + { + "name":"TEST_CLUB", + "email":"TEST_CLUB_EMAIL", + "points":"13" + } + ] + request.cls.clubs = fixture + + +@pytest.fixture(scope="class") +def competitions(request): + fixture = [ + { + "name": "TEST_COMPETITION", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + } + ] + request.cls.competitions = fixture \ No newline at end of file diff --git a/tests/unit_test/mocks_files/fixture_load.json b/tests/unit_test/mocks_files/fixture_load.json new file mode 100644 index 000000000..1d724612b --- /dev/null +++ b/tests/unit_test/mocks_files/fixture_load.json @@ -0,0 +1,16 @@ +{ + "clubs":[ + { + "name":"TEST_CLUB", + "email":"TEST_CLUB_EMAIL", + "points":"13" + } + ], + "competitions": [ + { + "name": "TEST_COMPETITION", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + } + ] +} \ No newline at end of file diff --git a/tests/unit_test/test_server.py b/tests/unit_test/test_server.py new file mode 100644 index 000000000..88cb00ebb --- /dev/null +++ b/tests/unit_test/test_server.py @@ -0,0 +1,20 @@ +from unittest.mock import patch +from unittest import TestCase + +from pytest import mark + +from server import loadClubs, loadCompetitions + + +@mark.usefixtures('client', 'clubs', 'competitions') +class TestServer(TestCase): + + def test_loadClubs(self): + self.assertListEqual(loadClubs("./tests/unit_test/mocks_files/fixture_load.json"), self.clubs) + + def test_loadCompetitions(self): + self.assertListEqual(loadCompetitions("./tests/unit_test/mocks_files/fixture_load.json"), self.competitions) + + def test_sould_login_in_out(self): + response = self.client.get("/logout") + self.assertEqual(response.status_code, 302) From 7490430a7c4811ac4f9b344c1aea1962168afe6a Mon Sep 17 00:00:00 2001 From: zionhigt Date: Sun, 27 Mar 2022 13:21:46 +0200 Subject: [PATCH 2/3] Test for issue #1 --- tests/unit_test/conftest.py | 39 +++++++++++++++++++++------------- tests/unit_test/test_server.py | 10 ++++++++- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/tests/unit_test/conftest.py b/tests/unit_test/conftest.py index 82868db79..7bd2cb951 100644 --- a/tests/unit_test/conftest.py +++ b/tests/unit_test/conftest.py @@ -1,32 +1,41 @@ import pytest +from unittest.mock import patch -from server import app - -@pytest.fixture(scope="class") -def client(request): - with app.test_client() as client: - request.cls.client = client +import server -@pytest.fixture(scope="class") -def clubs(request): - fixture = [ +def get_clubs(): + return [ { "name":"TEST_CLUB", "email":"TEST_CLUB_EMAIL", "points":"13" } ] - request.cls.clubs = fixture - -@pytest.fixture(scope="class") -def competitions(request): - fixture = [ +def get_competitions(): + return [ { "name": "TEST_COMPETITION", "date": "2020-03-27 10:00:00", "numberOfPlaces": "25" } ] - request.cls.competitions = fixture \ No newline at end of file + +@pytest.fixture(scope="class") +def clubs(request): + request.cls.clubs = get_clubs() + yield + +@pytest.fixture(scope="class") +def competitions(request): + request.cls.competitions = get_competitions() + yield + +@pytest.fixture(scope="class") +def client(request): + with patch("server.clubs", get_clubs()): + with patch("server.competitions", get_competitions()): + with server.app.test_client() as client: + request.cls.client = client + yield \ No newline at end of file diff --git a/tests/unit_test/test_server.py b/tests/unit_test/test_server.py index 88cb00ebb..ef2c372fa 100644 --- a/tests/unit_test/test_server.py +++ b/tests/unit_test/test_server.py @@ -1,4 +1,3 @@ -from unittest.mock import patch from unittest import TestCase from pytest import mark @@ -18,3 +17,12 @@ def test_loadCompetitions(self): def test_sould_login_in_out(self): response = self.client.get("/logout") self.assertEqual(response.status_code, 302) + + def test_sould_connect_with_email_exists(self): + for cl in self.clubs: + response = self.client.post("/showSummary", data={'email': cl.get('email')}) + self.assertEqual(response.status_code, 200) + + def test_sould_no_connect_with_email_does_not_exists(self): + response = self.client.post("/showSummary", data={'email': "not_exists_email@test.com"}) + assert response.status_code == 401 From b58a9a81f70564b89ec7b9d45ab67d5791e4eaae Mon Sep 17 00:00:00 2001 From: zionhigt Date: Sun, 27 Mar 2022 13:24:11 +0200 Subject: [PATCH 3/3] Fix for issue #1 --- server.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server.py b/server.py index 4b700ef78..90525ff87 100644 --- a/server.py +++ b/server.py @@ -26,8 +26,10 @@ 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 len(club): + return render_template('welcome.html',club=club[0],competitions=competitions), 200 + return render_template('index.html'), 401 @app.route('/book//')