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/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//') 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..7bd2cb951 --- /dev/null +++ b/tests/unit_test/conftest.py @@ -0,0 +1,41 @@ +import pytest +from unittest.mock import patch + + +import server + +def get_clubs(): + return [ + { + "name":"TEST_CLUB", + "email":"TEST_CLUB_EMAIL", + "points":"13" + } + ] + +def get_competitions(): + return [ + { + "name": "TEST_COMPETITION", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + } + ] + +@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/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..ef2c372fa --- /dev/null +++ b/tests/unit_test/test_server.py @@ -0,0 +1,28 @@ +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) + + 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