@@ -123,6 +123,64 @@ def test_login_no_email(client):
123
123
assert "Email is required" in response_text
124
124
125
125
126
+ def test_get_welcome_page_with_email_in_session (client , mocker ):
127
+ """
128
+ Test accessing the welcome page with a valid email in the session.
129
+
130
+ This test verifies that the welcome page is accessible and displays the correct
131
+ information when a valid email is present in the session.
132
+
133
+ :param client: The Flask test client.
134
+ :type client: flask.testing.FlaskClient
135
+ :param mocker: The mock object.
136
+ :type mocker: pytest_mock.MockerFixture
137
+ :return: None
138
+ """
139
+ clubs = mocker .patch ("server.clubs" , sample_clubs ()["clubs" ])
140
+ with client .session_transaction () as sess :
141
+ sess ["email" ] = clubs [0 ]["email" ]
142
+
143
+ response = client .get ("/show_summary" )
144
+ assert response .status_code == 200
145
+ assert b"Welcome" in response .data
146
+ assert b"Points available" in response .data
147
+
148
+
149
+ def test_get_welcome_page_without_email_in_session (client ):
150
+ """
151
+ Test accessing the welcome page without an email in the session.
152
+
153
+ This test verifies that the user is redirected to the home page when no email
154
+ is present in the session.
155
+
156
+ :param client: The Flask test client.
157
+ :type client: flask.testing.FlaskClient
158
+ :return: None
159
+ """
160
+ response = client .get ("/show_summary" )
161
+ assert response .status_code == 302
162
+ assert response .headers ["Location" ] == "http://localhost/"
163
+
164
+
165
+ def test_get_welcome_page_with_invalid_email_in_session (client ):
166
+ """
167
+ Test accessing the welcome page with an invalid email in the session.
168
+
169
+ This test verifies that the user is redirected to the home page when an invalid
170
+ email is present in the session.
171
+
172
+ :param client: The Flask test client.
173
+ :type client: flask.testing.FlaskClient
174
+ :return: None
175
+ """
176
+ with client .session_transaction () as sess :
177
+ sess ["email" ] = "invalid@example.com"
178
+
179
+ response = client .get ("/show_summary" )
180
+ assert response .status_code == 302
181
+ assert response .headers ["Location" ] == "http://localhost/"
182
+
183
+
126
184
def test_purchase_success (client , mocker ):
127
185
"""
128
186
Test the purchase process when it is successful.
@@ -213,6 +271,25 @@ def test_purchase_exceeding_club_points(client, mocker):
213
271
assert "Invalid data provided" in response_text
214
272
215
273
274
+ def test_purchase_places_exceeding_maximum_places (client , mocker ):
275
+ """
276
+ Test that attempting to purchase more than the maximum allowed places (12) results in a BadRequest.
277
+
278
+ :param client: The Flask test client.
279
+ :type client: flask.testing.FlaskClient
280
+ :param mocker: The mock object.
281
+ :type mocker: pytest_mock.MockerFixture
282
+ :return: None
283
+ """
284
+ mocker .patch ("server.clubs" , sample_clubs ()["clubs" ])
285
+ mocker .patch ("server.competitions" , sample_competitions ()["competitions" ])
286
+ with client .session_transaction () as session :
287
+ session ["email" ] = "club1@test.com"
288
+ response = client .post ("/purchase_places" , data = {"competition" : "Comp1" , "club" : "Club1" , "places" : 13 })
289
+ assert response .status_code == 400
290
+ assert b"Invalid data provided" in response .data
291
+
292
+
216
293
def test_deduct_club_points_and_competition_places_after_purchase_process (client , mocker ):
217
294
"""
218
295
Test the deduction of club points and competition places after a purchase.
@@ -246,3 +323,56 @@ def test_deduct_club_points_and_competition_places_after_purchase_process(client
246
323
assert "Great - booking complete!" in response_text
247
324
assert club ["points" ] == int (inital_club_points ) - places_required
248
325
assert competition ["numberOfPlaces" ] == int (initial_competition_places ) - places_required
326
+
327
+
328
+ def test_access_without_email_in_session (client , mocker ):
329
+ """
330
+ Test that accessing a route without an email in the session raises an Unauthorized exception.
331
+
332
+ :param client: The Flask test client.
333
+ :type client: flask.testing.FlaskClient
334
+ :param mocker: The mock object.
335
+ :type mocker: pytest_mock.MockerFixture
336
+ :return: None
337
+ """
338
+ clubs = mocker .patch ("server.clubs" , sample_clubs ()["clubs" ])
339
+ competitions = mocker .patch ("server.competitions" , sample_competitions ()["competitions" ])
340
+ places_required = 2
341
+
342
+ # No email set in session
343
+ with client .session_transaction () as session :
344
+ session ["email" ] = None
345
+
346
+ response = client .post (
347
+ "/purchase_places" , data = {"competition" : competitions [0 ]["name" ], "club" : clubs [0 ]["name" ], "places" : places_required }, follow_redirects = True
348
+ )
349
+
350
+ # Check for Unauthorized exception
351
+ assert response .status_code == 401
352
+ assert b"You must be connected." in response .data
353
+
354
+
355
+ def test_update_booking_insufficient_points (client , mocker ):
356
+ """
357
+ Test the update of a booking when the club has insufficient points.
358
+
359
+ This test verifies that the booking is not updated if the club does not
360
+ have enough points to book the required places.
361
+
362
+ :return: None
363
+ """
364
+ clubs = mocker .patch ("server.clubs" , sample_clubs ()["clubs" ])
365
+ competitions = mocker .patch ("server.competitions" , sample_competitions ()["competitions" ])
366
+ places_required = 14
367
+
368
+ with client .session_transaction () as session :
369
+ session ["email" ] = clubs [0 ]["email" ]
370
+
371
+ response = client .post (
372
+ "/purchase_places" , data = {"competition" : competitions [0 ]["name" ], "club" : clubs [0 ]["name" ], "places" : places_required }, follow_redirects = True
373
+ )
374
+
375
+ assert response .status_code == 400
376
+ assert int (competitions [0 ]["clubBookings" ][clubs [0 ]["name" ]]) == 0
377
+ assert int (clubs [0 ]["points" ]) == 13
378
+ assert int (competitions [0 ]["numberOfPlaces" ]) == 25
0 commit comments