diff --git a/server.py b/server.py index 4084baeac..5d303539d 100644 --- a/server.py +++ b/server.py @@ -1,5 +1,5 @@ import json -from flask import Flask,render_template,request,redirect,flash,url_for +from flask import Flask,render_template,request,redirect,flash,url_for,session def loadClubs(): @@ -24,10 +24,20 @@ def loadCompetitions(): def index(): return render_template('index.html') + @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) + user_email = request.form['email'] + found_clubs = [club for club in clubs if club['email'] == user_email] + + if found_clubs: + club = found_clubs[0] + session['club_email'] = club['email'] + return render_template('welcome.html',club=club,competitions=competitions) + else: + flash("Sorry, that email was not found.") + session.pop('club_email', None) + return redirect(url_for('index')) @app.route('/book//') diff --git a/templates/index.html b/templates/index.html index 926526b7d..cfa154e7b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,6 +3,11 @@ GUDLFT Registration +

Welcome to the GUDLFT Registration Portal!

@@ -12,5 +17,13 @@

Welcome to the GUDLFT Registration Portal!

+ + {% with messages = get_flashed_messages() %} + {% if messages %} + {% for message in messages %} +

{{ message }}

+ {% endfor %} + {% endif %} + {% endwith %} \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_login.py b/tests/test_login.py new file mode 100644 index 000000000..e5b620518 --- /dev/null +++ b/tests/test_login.py @@ -0,0 +1,40 @@ +import pytest +from server import app, clubs, competitions +from unittest.mock import patch, MagicMock + + +@pytest.fixture +def client(): + """Configures the Flask test client and yields it for testing.""" + app.config['TESTING'] = True + with app.test_client() as client: + yield client + +def test_unknown_email_flashes_message(client): + """Test that an unknown email results in a flash message being stored.""" + # Mock the 'clubs' data so that the email is not found + with patch('server.clubs', []): + response = client.post('/showSummary', data={'email': 'unknown@test.com'}, follow_redirects=False) + + # Check the flash messages stored in the session + with client.session_transaction() as sess: + flashed_messages = dict(sess.get('_flashes', [])) + + assert response.status_code == 302 + assert response.location == '/' + assert "Sorry, that email was not found." in flashed_messages.values() + +def test_existing_email_login_is_successful(client): + """Test that a user with an existing email can log in and view the welcome page.""" + # Mock the 'clubs' data to include a known user + with patch('server.clubs', [{'name': 'Test Club', 'email': 'test@test.com', 'points': '10'}]): + response = client.post('/showSummary', data={'email': 'test@test.com'}) + + # Assertions + assert response.status_code == 200 + assert b'Welcome' in response.data + + # Check that the email was stored in the session + with client.session_transaction() as sess: + assert 'club_email' in sess + assert sess['club_email'] == 'test@test.com' \ No newline at end of file