diff --git a/.gitignore b/.gitignore index 2cba99d87..8b4d1421a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ bin include lib .Python -tests/ .envrc -__pycache__ \ No newline at end of file +__pycache__ +launch_flask.bat +launch_test.bat \ No newline at end of file diff --git a/branchName.txt b/branchName.txt new file mode 100644 index 000000000..8475737c2 --- /dev/null +++ b/branchName.txt @@ -0,0 +1,12 @@ +#6 +FEATURE/Implement-Points-Display-Board +#5 +BUG/Point-updates-are-not-reflected +#4 +BUG/Booking-places-in-past-competitions +#3 +BUG/Clubs-shouldn-t-be-able-to-book-more-than-12-places-per-competition +#2 +BUG/Clubs-should-not-be-able-to-use-more-than-their-points-allowed +#1 +ERROR/Entering-a-unknown-email-crashes-the-app \ No newline at end of file diff --git a/server.py b/server.py index 4084baeac..f3c87274b 100644 --- a/server.py +++ b/server.py @@ -1,15 +1,16 @@ import json from flask import Flask,render_template,request,redirect,flash,url_for +from datetime import datetime -def loadClubs(): - with open('clubs.json') as c: +def loadClubs(path='clubs.json'): + with open(path) as c: listOfClubs = json.load(c)['clubs'] return listOfClubs -def loadCompetitions(): - with open('competitions.json') as comps: +def loadCompetitions(path='competitions.json'): + with open(path) as comps: listOfCompetitions = json.load(comps)['competitions'] return listOfCompetitions @@ -26,8 +27,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//') @@ -45,10 +48,25 @@ def book(competition,club): 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] + + competition_date = datetime.fromisoformat(competition.get("date")); + if competition_date < datetime.now(): + flash('Unable to require places for already been passed competion !') + return render_template('welcome.html', club=club, competitions=competitions), 403 + placesRequired = int(request.form['places']) - competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired + if placesRequired > 12: + flash('You cannot required more than 12 places!') + return render_template('welcome.html', club=club, competitions=competitions), 403 + + if placesRequired > int(club.get("points")): + flash('You haven\'t enough of points to purshase this!') + return render_template('welcome.html', club=club, competitions=competitions), 403 + + competition['numberOfPlaces'] = str(int(competition['numberOfPlaces']) - placesRequired) + club['points'] = str(int(club['points']) - placesRequired) flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + return render_template('welcome.html', club=club, competitions=competitions), 200 # TODO: Add route for points display 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..468cc2a6f --- /dev/null +++ b/tests/unit_test/conftest.py @@ -0,0 +1,44 @@ +import pytest +from unittest.mock import patch + +import server + +clubs_obj = [ + { + "name":"TEST_CLUB", + "email":"TEST_CLUB_EMAIL", + "points":"10" + } + ] + +competitions_obj = [ + { + "name": "TEST_COMPETITION_1", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + }, + { + "name": "TEST_COMPETITION_2", + "date": "2025-03-27 10:00:00", + "numberOfPlaces": "25" + } + ] + + +@pytest.fixture(scope="class") +def clubs(request): + request.cls.clubs = clubs_obj + yield + +@pytest.fixture(scope="class") +def competitions(request): + request.cls.competitions = competitions_obj + yield + +@pytest.fixture(scope="class") +def client(request): + with patch("server.clubs", clubs_obj): + with patch("server.competitions", competitions_obj): + 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..d915ac3d6 --- /dev/null +++ b/tests/unit_test/mocks_files/fixture_load.json @@ -0,0 +1,21 @@ +{ + "clubs":[ + { + "name":"TEST_CLUB", + "email":"TEST_CLUB_EMAIL", + "points":"9" + } + ], + "competitions": [ + { + "name": "TEST_COMPETITION_1", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "25" + }, + { + "name": "TEST_COMPETITION_2", + "date": "2025-03-27 10:00:00", + "numberOfPlaces": "24" + } + ] +} \ 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..05d55f326 --- /dev/null +++ b/tests/unit_test/test_server.py @@ -0,0 +1,71 @@ +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"}) + self.assertEqual(response.status_code, 401) + + def test_sould_not_purshase_more_than_I_own(self): + for com in self.competitions: + for cl in self.clubs: + mock = { + "club": cl.get('name'), + "competition": "TEST_COMPETITION_2", + "places": int(cl.get('points')) + 1 + } + response = self.client.post('/purchasePlaces', data=mock) + self.assertIn("You haven't enough of points to purshase this!", response.data.decode()) + self.assertEqual(response.status_code, 403) + + def test_sould_not_purshase_more_than_12_places(self): + response = self.client.post('/purchasePlaces', data={ + "club": "TEST_CLUB", + "competition": "TEST_COMPETITION_2", + "places": 13 + }) + self.assertIn("You cannot required more than 12 places!", response.data.decode()) + self.assertEqual(response.status_code, 403) + + def test_sould_not_purshase_booking_if_date_passed(self): + response = self.client.post('/purchasePlaces', data={ + "club": "TEST_CLUB", + "competition": "TEST_COMPETITION_1", + "places": 1 + }) + self.assertIn("Unable to require places for already been passed competion !", response.data.decode()) + self.assertEqual(response.status_code, 403) + + def test_club_points_should_be_reflected_after_booking(self): + places = 1 + club = self.clubs[0] + points = int(club.get("points")) + competition = self.competitions[1] + number_of_places = int(competition.get("numberOfPlaces")) + response = self.client.post('/purchasePlaces', data={ + "club": club.get("name"), + "competition": competition.get("name"), + "places": places + }) + self.assertEqual(int(club.get('points')), points - places) + self.assertEqual(int(competition.get('numberOfPlaces')), number_of_places - places)