diff --git a/.gitignore b/.gitignore index 2cba99d87..ffe03f368 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ bin include lib .Python -tests/ .envrc +.venv __pycache__ \ No newline at end of file diff --git a/server.py b/server.py index 4084baeac..220f392ab 100644 --- a/server.py +++ b/server.py @@ -1,59 +1,93 @@ import json -from flask import Flask,render_template,request,redirect,flash,url_for +from flask import Flask, render_template, request, redirect, flash, url_for -def loadClubs(): - with open('clubs.json') as c: - listOfClubs = json.load(c)['clubs'] - return listOfClubs +def load_clubs(): + with open("clubs.json") as c: + clubs = json.load(c)["clubs"] + return clubs -def loadCompetitions(): - with open('competitions.json') as comps: - listOfCompetitions = json.load(comps)['competitions'] - return listOfCompetitions +def load_competitions(): + with open("competitions.json") as comps: + competitions = json.load(comps)["competitions"] + return competitions app = Flask(__name__) -app.secret_key = 'something_special' +app.secret_key = "something_special" -competitions = loadCompetitions() -clubs = loadClubs() +competitions = load_competitions() +clubs = load_clubs() -@app.route('/') + +class EmailNotFound(Exception): + def __init__(self, message): + super().__init__(message) + self.message = message + + +class CompetitionNotFound(Exception): + def __init__(self, message): + super().__init__(message) + self.message = message + + +def get_club(email): + clubs = load_clubs() + for club in clubs: + if email == club["email"]: + return club + raise EmailNotFound("Club introuvables. Veuillez réessayer.") + + +def get_competition(name): + competitions = load_competitions() + for competition in competitions: + if name == competition["name"]: + return competition + raise CompetitionNotFound("Compétition introuvables. Veuillez réessayer.") + + +@app.route("/") def index(): - return render_template('index.html') + return render_template("index.html") + -@app.route('/showSummary',methods=['POST']) +@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 = get_club(request.form["email"]) + return render_template("welcome.html", club=club, competitions=competitions) -@app.route('/book//') -def book(competition,club): - foundClub = [c for c in clubs if c['name'] == club][0] - foundCompetition = [c for c in competitions if c['name'] == competition][0] +@app.route("/book//") +def book(competition, club): + foundClub = [c for c in clubs if c["name"] == club][0] + foundCompetition = [c for c in competitions if c["name"] == competition][0] if foundClub and foundCompetition: - return render_template('booking.html',club=foundClub,competition=foundCompetition) + return render_template( + "booking.html", club=foundClub, competition=foundCompetition + ) else: flash("Something went wrong-please try again") - return render_template('welcome.html', club=club, competitions=competitions) + return render_template("welcome.html", club=club, competitions=competitions) -@app.route('/purchasePlaces',methods=['POST']) +@app.route("/purchasePlaces", methods=["POST"]) 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] - placesRequired = int(request.form['places']) - competition['numberOfPlaces'] = int(competition['numberOfPlaces'])-placesRequired - flash('Great-booking complete!') - return render_template('welcome.html', club=club, competitions=competitions) + 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] + placesRequired = int(request.form["places"]) + competition["numberOfPlaces"] = int(competition["numberOfPlaces"]) - placesRequired + flash("Great-booking complete!") + return render_template("welcome.html", club=club, competitions=competitions) # TODO: Add route for points display -@app.route('/logout') +@app.route("/logout") def logout(): - return redirect(url_for('index')) \ No newline at end of file + return redirect(url_for("index")) diff --git a/tests/tests_functionals/__init__.py b/tests/tests_functionals/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/tests_integrations/__init__.py b/tests/tests_integrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/tests_unitaires/__init__.py b/tests/tests_unitaires/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/tests_unitaires/test_get_club.py b/tests/tests_unitaires/test_get_club.py new file mode 100644 index 000000000..433ed1b66 --- /dev/null +++ b/tests/tests_unitaires/test_get_club.py @@ -0,0 +1,29 @@ +import pytest +import sys +import os + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../"))) + +from server import get_club, load_clubs, EmailNotFound + + +def test_get_club_should_returne_club_with_valide_data(mocker): + mock_response = [{ + "name": "test1", + "email": "test@testgetclub.co", + "points": "13", + }] + mocker.patch("server.load_clubs", return_value=mock_response) + result = get_club("test@testgetclub.co") + expected_value = { + "name": "test1", + "email": "test@testgetclub.co", + "points": "13"} + assert result == expected_value + + +def test_get_club_should_raise_error_with_invalide_data(mocker): + + mocker.patch('server.load_clubs', return_value=[]) + with pytest.raises(EmailNotFound, match='Club introuvables. Veuillez réessayer.'): + get_club("test2@testgetclub.co") diff --git a/tests/tests_unitaires/test_get_competition.py b/tests/tests_unitaires/test_get_competition.py new file mode 100644 index 000000000..f4e97a38a --- /dev/null +++ b/tests/tests_unitaires/test_get_competition.py @@ -0,0 +1,32 @@ +import pytest +import sys +import os + +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../'))) + +from server import load_competitions, get_competition, CompetitionNotFound + + +def test_should_return_competition_with_valid_competition(mocker): + mock_response = [ + { + "name": "testcompetition", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "11" + } + ] + mocker.patch('server.load_competitions', return_value=mock_response) + result = get_competition('testcompetition') + expected_value = { + "name": "testcompetition", + "date": "2020-03-27 10:00:00", + "numberOfPlaces": "11" + } + assert result == expected_value + + +def test_should_raise_CompetitionNotFound_error_with_invalid_competition(mocker): + mock_response = [] + mocker.patch('server.load_competitions', return_value=mock_response) + with pytest.raises(CompetitionNotFound, match="Compétition introuvables. Veuillez réessayer."): + get_competition('invalidcompetition')