Add capacity validation and extra tests#2
Conversation
- Add capacity limit validation to signup endpoint - Add tests for capacity limits and static file serving
There was a problem hiding this comment.
Pull request overview
This PR adds capacity validation to prevent activity signups when maximum capacity is reached, and introduces three new test cases to improve test coverage.
Changes:
- Added capacity check in
signup_for_activityto block signups when an activity is full - Added test to verify capacity limit enforcement
- Added test to verify static file serving
- Added test to check empty email behavior
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/app.py | Added capacity validation before allowing signups |
| tests/test_app.py | Added three new tests for capacity limits, static file serving, and empty email handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Use an activity with low capacity | ||
| activity_name = "Chess Club" # Has max_participants: 12, but already has 2 participants | ||
|
|
||
| # Fill up the remaining spots (12 - 2 = 10 spots left) | ||
| for i in range(10): | ||
| response = client.post( | ||
| f"/activities/{activity_name}/signup", | ||
| params={"email": f"student{i}@test.edu"} | ||
| ) | ||
| assert response.status_code == 200 | ||
|
|
||
| # Try to sign up one more (should fail) | ||
| response = client.post( | ||
| f"/activities/{activity_name}/signup", | ||
| params={"email": "overflow@test.edu"} |
There was a problem hiding this comment.
This test assumes Chess Club has exactly 2 participants at the start, but since tests share state and there's no test isolation mechanism, previous tests may have modified the participants list. This test could fail or behave unpredictably depending on test execution order. Consider using a fresh activity or resetting state in a fixture, or selecting an activity that isn't used by other tests.
| # Use an activity with low capacity | |
| activity_name = "Chess Club" # Has max_participants: 12, but already has 2 participants | |
| # Fill up the remaining spots (12 - 2 = 10 spots left) | |
| for i in range(10): | |
| response = client.post( | |
| f"/activities/{activity_name}/signup", | |
| params={"email": f"student{i}@test.edu"} | |
| ) | |
| assert response.status_code == 200 | |
| # Try to sign up one more (should fail) | |
| response = client.post( | |
| f"/activities/{activity_name}/signup", | |
| params={"email": "overflow@test.edu"} | |
| # Use an activity with known capacity | |
| activity_name = "Chess Club" | |
| # Get current state of the activity to avoid assumptions about initial participants | |
| activities_response = client.get("/activities") | |
| assert activities_response.status_code == 200 | |
| activities_data = activities_response.json() | |
| assert activity_name in activities_data | |
| activity = activities_data[activity_name] | |
| max_participants = activity["max_participants"] | |
| current_participants = len(activity["participants"]) | |
| # Fill up the remaining spots based on current state | |
| remaining_spots = max(0, max_participants - current_participants) | |
| for i in range(remaining_spots): | |
| response = client.post( | |
| f"/activities/{activity_name}/signup", | |
| params={"email": f"capacity_test_student{i}@test.edu"} | |
| ) | |
| assert response.status_code == 200 | |
| # Try to sign up one more (should fail due to capacity limit) | |
| response = client.post( | |
| f"/activities/{activity_name}/signup", | |
| params={"email": "overflow_capacity_test@test.edu"} |
| # This should either fail validation or succeed depending on implementation | ||
| # For now, just check it doesn't crash | ||
| assert response.status_code in [200, 400] No newline at end of file |
There was a problem hiding this comment.
This test is too permissive and doesn't actually validate the expected behavior for empty email addresses. It should either verify that empty emails are rejected with a 400 status (if validation is intended) or succeed with 200 (if empty emails are allowed). The current assertion provides no meaningful coverage.
| assert "DOMContentLoaded" in response.text | ||
|
|
||
| def test_signup_empty_email(): | ||
| """Test signup with empty email""" |
There was a problem hiding this comment.
The docstring doesn't describe what the expected behavior should be when signing up with an empty email. Update the docstring to clarify whether empty emails should be accepted or rejected.
| """Test signup with empty email""" | |
| """Test that signing up with an empty email is handled gracefully. | |
| The application may either reject the request with a 400 error or accept it | |
| with a 200 response, but it must not crash when an empty email is provided. | |
| """ |
This pull request adds a check to prevent users from signing up for an activity that has reached its maximum capacity and introduces new tests to verify this behavior, as well as tests for static file serving and email validation.
Activity capacity enforcement:
signup_for_activity(insrc/app.py) to raise an HTTP 400 error if the activity is at full capacity, preventing further signups.Testing improvements:
test_activity_capacity_limitintests/test_app.pyto verify that signups are blocked when an activity reaches its maximum number of participants.test_static_files_servedto ensure that HTML, CSS, and JS static files are correctly served by the application.test_signup_empty_emailto check application behavior when attempting to sign up with an empty email address.