diff --git a/requirements.txt b/requirements.txt index 5d9efb5f..f2821b20 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ fastapi uvicorn httpx -watchfiles \ No newline at end of file +watchfiles +pytest \ No newline at end of file diff --git a/src/app.py b/src/app.py index 4ebb1d9b..f0fa0ac6 100644 --- a/src/app.py +++ b/src/app.py @@ -62,6 +62,13 @@ def signup_for_activity(activity_name: str, email: str): # Get the specific activity activity = activities[activity_name] + normalized_email = email.strip().lower() + existing_participants = {participant.strip().lower() for participant in activity["participants"]} + + # Validate student is not already signed up + if normalized_email in existing_participants: + raise HTTPException(status_code=400, detail="Student is already signed up") + # Add student activity["participants"].append(email) return {"message": f"Signed up {email} for {activity_name}"} diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 00000000..f51e7e57 --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,20 @@ +from uuid import uuid4 + +from fastapi.testclient import TestClient + +from src.app import app + +client = TestClient(app) + + +def test_student_cannot_sign_up_twice_for_same_activity(): + activity_name = "Chess Club" + email = f"student-{uuid4()}@mergington.edu" + + first_response = client.post(f"/activities/{activity_name}/signup?email={email}") + assert first_response.status_code == 200 + + second_response = client.post(f"/activities/{activity_name}/signup?email={email}") + + assert second_response.status_code == 400 + assert "already signed up" in second_response.json()["detail"].lower()