Skip to content

(fix) handle unknown email in login #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .coverage
Binary file not shown.
6 changes: 6 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[run]
omit =
lib/
bin/
*/__init__.py
tests/*
12 changes: 12 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[flake8]
max-line-length = 80
exclude =
bin/activate.py,
lib,
packages,
migrations,
build,
dist,
*.pyc,
__pycache__,
bin/activate_this.py
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ bin
include
lib
.Python
tests/
.envrc
__pycache__
__pycache__
pyvenv.cfg
.DS_Store
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tool.black]
line-length = 80
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]

testpaths = tests
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ itsdangerous==1.1.0
Jinja2==2.11.2
MarkupSafe==1.1.1
Werkzeug==1.0.1
pytest
black
flake8
81 changes: 53 additions & 28 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,84 @@
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
with open("clubs.json") as c:
listOfClubs = json.load(c)["clubs"]
return listOfClubs


def loadCompetitions():
with open('competitions.json') as comps:
listOfCompetitions = json.load(comps)['competitions']
return listOfCompetitions
with open("competitions.json") as comps:
listOfCompetitions = json.load(comps)["competitions"]
return listOfCompetitions


app = Flask(__name__)
app.secret_key = 'something_special'
app.secret_key = "something_special"

competitions = loadCompetitions()
clubs = loadClubs()

@app.route('/')

@app.route("/")
def index():
return render_template('index.html')
return render_template("index.html")


def get_club_from_email(email):
try:
club = [
club for club in clubs if club["email"] == email
][0]
return club
except IndexError:
return None

@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_from_email(request.form["email"])
if club:
return render_template("welcome.html", club=club,
competitions=competitions)
else:
flash("Sorry, that email wasn't found.")
return redirect(url_for("index"))


@app.route('/book/<competition>/<club>')
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/<competition>/<club>")
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'))
return redirect(url_for("index"))
13 changes: 12 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
Expand All @@ -6,10 +7,20 @@
</head>
<body>
<h1>Welcome to the GUDLFT Registration Portal!</h1>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}

Please enter your secretary email to continue:
<form action="showSummary" method="post">
<label for="email">Email:</label>
<input type="email" name="email" id=""/>
<input type="email" name="email" id="email"/>
<button type="submit">Enter</button>
</form>
</body>
Expand Down
6 changes: 3 additions & 3 deletions templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
<body>
<h2>Welcome, {{club['email']}} </h2><a href="{{url_for('logout')}}">Logout</a>

{% with messages = get_flashed_messages()%}
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{message}}</li>
{% endfor %}
</ul>
{% endif%}

Points available: {{club['points']}}
<h3>Competitions:</h3>
<ul>
Expand All @@ -30,7 +31,6 @@ <h3>Competitions:</h3>
<hr />
{% endfor %}
</ul>
{%endwith%}

{% endwith %}
</body>
</html>
Empty file added tests/__init__.py
Empty file.
Empty file.
48 changes: 48 additions & 0 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
import pytest
from unittest.mock import mock_open, patch

# Mock data for clubs.json and competitions.json
mock_clubs_json = json.dumps(
{
"clubs": [
{"name": "Club 1", "email": "club1@example.com"},
{"name": "Club 2", "email": "club2@example.com"},
]
}
)

mock_competitions_json = json.dumps(
{
"competitions": [
{"name": "Competition 1", "numberOfPlaces": "25"},
{"name": "Competition 2", "numberOfPlaces": "15"},
]
}
)


def mocked_open(file, *args, **kwargs):
"""
Mock open function to return mock data for
clubs.json and competitions.json
"""
if file == "clubs.json":
return mock_open(read_data=mock_clubs_json)()
elif file == "competitions.json":
return mock_open(read_data=mock_competitions_json)()
else:
raise FileNotFoundError(f"File {file} not found")


# Patch open before importing the app to ensure clubs
# and competitions are loaded with mock data
with patch("builtins.open", side_effect=mocked_open):
from server import app # Import app after patching


@pytest.fixture
def client():
app.config["TESTING"] = True
with app.test_client() as client:
yield client
24 changes: 24 additions & 0 deletions tests/integration_tests/test_unknown_email.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

# Integration test for valid email
def test_showSummary_valid_email_integration(client):
# Simulate POST request with a valid email
response = client.post("/showSummary", data={"email": "club1@example.com"})

# Check if the welcome page is rendered with the correct club data
assert response.status_code == 200
assert b"Welcome" in response.data


# Integration test for invalid email
def test_showSummary_invalid_email_integration(client):
# Simulate POST request with an invalid email
response = client.post(
"/showSummary", data={"email": "invalid@example.com"}
)

# Validate that the response redirects to the index page
assert response.status_code == 302
response = client.get(response.headers["Location"]) # Follow the redirect

# Check if the flash message appears in the redirected page
assert b"Sorry, that email wasn&#39;t found." in response.data
Empty file added tests/unit_tests/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/unit_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
from server import app


@pytest.fixture
def client():
with app.test_client() as client:
yield client
Loading