From e31cb544d05d2029224a1445f86407de7f3b2576 Mon Sep 17 00:00:00 2001 From: Kevin Sebastin <89242806+KevinSebastin@users.noreply.github.com> Date: Wed, 27 May 2026 08:40:25 +0000 Subject: [PATCH 1/4] step 2 --- src/app.py | 76 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/src/app.py b/src/app.py index 4ebb1d9..0a10867 100644 --- a/src/app.py +++ b/src/app.py @@ -21,24 +21,60 @@ # In-memory activity database activities = { - "Chess Club": { - "description": "Learn strategies and compete in chess tournaments", - "schedule": "Fridays, 3:30 PM - 5:00 PM", - "max_participants": 12, - "participants": ["michael@mergington.edu", "daniel@mergington.edu"] - }, - "Programming Class": { - "description": "Learn programming fundamentals and build software projects", - "schedule": "Tuesdays and Thursdays, 3:30 PM - 4:30 PM", - "max_participants": 20, - "participants": ["emma@mergington.edu", "sophia@mergington.edu"] - }, - "Gym Class": { - "description": "Physical education and sports activities", - "schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM", - "max_participants": 30, - "participants": ["john@mergington.edu", "olivia@mergington.edu"] - } + "Chess Club": { + "description": "Learn strategies and compete in chess tournaments", + "schedule": "Fridays, 3:30 PM - 5:00 PM", + "max_participants": 12, + "participants": ["michael@mergington.edu", "daniel@mergington.edu"] + }, + "Programming Class": { + "description": "Learn programming fundamentals and build software projects", + "schedule": "Tuesdays and Thursdays, 3:30 PM - 4:30 PM", + "max_participants": 20, + "participants": ["emma@mergington.edu", "sophia@mergington.edu"] + }, + "Gym Class": { + "description": "Physical education and sports activities", + "schedule": "Mondays, Wednesdays, Fridays, 2:00 PM - 3:00 PM", + "max_participants": 30, + "participants": ["john@mergington.edu", "olivia@mergington.edu"] + }, + "Basketball Team": { + "description": "Competitive basketball league and training", + "schedule": "Mondays and Thursdays, 4:00 PM - 5:30 PM", + "max_participants": 15, + "participants": ["alex@mergington.edu"] + }, + "Tennis Club": { + "description": "Learn and compete in tennis matches", + "schedule": "Wednesdays and Saturdays, 3:00 PM - 4:30 PM", + "max_participants": 10, + "participants": ["jordan@mergington.edu", "taylor@mergington.edu"] + }, + "Drama Club": { + "description": "Perform in theatrical productions and develop acting skills", + "schedule": "Tuesdays and Thursdays, 5:00 PM - 6:30 PM", + "max_participants": 25, + "participants": ["casey@mergington.edu"] + }, + "Art Studio": { + "description": "Explore painting, drawing, and sculpture techniques", + "schedule": "Mondays, 4:00 PM - 5:30 PM", + "max_participants": 18, + "participants": ["morgan@mergington.edu", "ryan@mergington.edu"] + }, + "Debate Team": { + "description": "Develop argumentation and public speaking skills", + "schedule": "Wednesdays, 4:00 PM - 5:30 PM", + "max_participants": 16, + "participants": ["avery@mergington.edu"] + }, + "Robotics Club": { + "description": "Design and build robots for competitions", + "schedule": "Fridays, 4:00 PM - 6:00 PM", + "max_participants": 14, + "participants": ["chris@mergington.edu", "blake@mergington.edu"] + } } @@ -62,6 +98,10 @@ def signup_for_activity(activity_name: str, email: str): # Get the specific activity activity = activities[activity_name] + # Validate student is not already signed up + if email in activity["participants"]: + raise HTTPException(status_code=400, detail="Student already signed up") + # Add student activity["participants"].append(email) return {"message": f"Signed up {email} for {activity_name}"} From b4f1150ab742f06a11e875f2429ac03b0c2004c6 Mon Sep 17 00:00:00 2001 From: Kevin Sebastin <89242806+KevinSebastin@users.noreply.github.com> Date: Wed, 27 May 2026 08:56:19 +0000 Subject: [PATCH 2/4] various fixes --- src/app.py | 15 ++++++ src/static/app.js | 102 ++++++++++++++++++++++++++++++++++------ src/static/styles.css | 106 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 209 insertions(+), 14 deletions(-) diff --git a/src/app.py b/src/app.py index 0a10867..1ff629f 100644 --- a/src/app.py +++ b/src/app.py @@ -105,3 +105,18 @@ def signup_for_activity(activity_name: str, email: str): # Add student activity["participants"].append(email) return {"message": f"Signed up {email} for {activity_name}"} + + +@app.post("/activities/{activity_name}/unregister") +def unregister_from_activity(activity_name: str, email: str): + """Unregister a student from an activity""" + if activity_name not in activities: + raise HTTPException(status_code=404, detail="Activity not found") + + activity = activities[activity_name] + + if email not in activity["participants"]: + raise HTTPException(status_code=400, detail="Student is not signed up for this activity") + + activity["participants"].remove(email) + return {"message": f"Unregistered {email} from {activity_name}"} diff --git a/src/static/app.js b/src/static/app.js index dcc1e38..9b4889d 100644 --- a/src/static/app.js +++ b/src/static/app.js @@ -4,14 +4,35 @@ document.addEventListener("DOMContentLoaded", () => { const signupForm = document.getElementById("signup-form"); const messageDiv = document.getElementById("message"); + function showMessage(text, type) { + messageDiv.textContent = text; + messageDiv.className = type; + messageDiv.classList.remove("hidden"); + + setTimeout(() => { + messageDiv.classList.add("hidden"); + }, 5000); + } + // Function to fetch activities from API async function fetchActivities() { + activitiesList.innerHTML = ` +
+
+

Loading activities...

+

Fetching the latest participant updates

+
+ `; + try { - const response = await fetch("/activities"); + const response = await fetch(`/activities?t=${Date.now()}`, { + cache: "no-store", + }); const activities = await response.json(); // Clear loading message activitiesList.innerHTML = ""; + activitySelect.innerHTML = ''; // Populate activities list Object.entries(activities).forEach(([name, details]) => { @@ -19,12 +40,39 @@ document.addEventListener("DOMContentLoaded", () => { activityCard.className = "activity-card"; const spotsLeft = details.max_participants - details.participants.length; + const participantsList = details.participants.length + ? details.participants + .map( + (participant) => ` +
  • + ${participant} + +
  • + ` + ) + .join("") + : "
  • No participants yet
  • "; activityCard.innerHTML = `

    ${name}

    ${details.description}

    Schedule: ${details.schedule}

    Availability: ${spotsLeft} spots left

    +
    +

    Participants (${details.participants.length}):

    + +
    `; activitiesList.appendChild(activityCard); @@ -59,25 +107,51 @@ document.addEventListener("DOMContentLoaded", () => { const result = await response.json(); if (response.ok) { - messageDiv.textContent = result.message; - messageDiv.className = "success"; + showMessage(result.message, "success"); signupForm.reset(); + await fetchActivities(); } else { - messageDiv.textContent = result.detail || "An error occurred"; - messageDiv.className = "error"; + showMessage(result.detail || "An error occurred", "error"); } + } catch (error) { + showMessage("Failed to sign up. Please try again.", "error"); + console.error("Error signing up:", error); + } + }); + + activitiesList.addEventListener("click", async (event) => { + const removeButton = event.target.closest(".participant-remove-btn"); + if (!removeButton) { + return; + } + + const activity = decodeURIComponent(removeButton.dataset.activity || ""); + const email = decodeURIComponent(removeButton.dataset.email || ""); + + if (!activity || !email) { + showMessage("Unable to remove participant. Please try again.", "error"); + return; + } - messageDiv.classList.remove("hidden"); + try { + const response = await fetch( + `/activities/${encodeURIComponent(activity)}/unregister?email=${encodeURIComponent(email)}`, + { + method: "POST", + } + ); - // Hide message after 5 seconds - setTimeout(() => { - messageDiv.classList.add("hidden"); - }, 5000); + const result = await response.json(); + + if (response.ok) { + showMessage(result.message, "success"); + await fetchActivities(); + } else { + showMessage(result.detail || "Failed to remove participant.", "error"); + } } catch (error) { - messageDiv.textContent = "Failed to sign up. Please try again."; - messageDiv.className = "error"; - messageDiv.classList.remove("hidden"); - console.error("Error signing up:", error); + showMessage("Failed to remove participant. Please try again.", "error"); + console.error("Error unregistering participant:", error); } }); diff --git a/src/static/styles.css b/src/static/styles.css index a533b32..9d4ff64 100644 --- a/src/static/styles.css +++ b/src/static/styles.css @@ -57,6 +57,47 @@ section h3 { color: #1a237e; } +.activities-loading { + min-height: 280px; + border: 2px dashed #c7d2fe; + border-radius: 10px; + background: linear-gradient(135deg, #eef2ff, #f8fafc); + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + text-align: center; + padding: 24px; + margin-bottom: 15px; +} + +.activities-loading h4 { + font-size: 1.4rem; + color: #1a237e; + margin-bottom: 6px; +} + +.activities-loading p { + font-size: 1rem; + color: #475569; +} + +.loading-spinner { + width: 54px; + height: 54px; + border: 5px solid #c7d2fe; + border-top-color: #3949ab; + border-radius: 50%; + margin-bottom: 14px; + animation: spin 0.9s linear infinite; +} + +@keyframes spin { + to { + transform: rotate(360deg); + } +} + .activity-card { margin-bottom: 15px; padding: 15px; @@ -74,6 +115,71 @@ section h3 { margin-bottom: 8px; } +.participants-section { + margin-top: 12px; + padding: 10px 12px; + background-color: #ffffff; + border: 1px solid #e3e6ef; + border-radius: 6px; +} + +.participants-heading { + margin-bottom: 8px; + color: #1a237e; +} + +.participants-list { + list-style: none; + margin: 0; + padding: 0; + display: grid; + gap: 8px; +} + +.participant-item { + display: flex; + align-items: center; + justify-content: space-between; + gap: 10px; + padding: 8px 10px; + background-color: #f8fafc; + border: 1px solid #e2e8f0; + border-radius: 6px; +} + +.participant-email { + color: #334155; + overflow-wrap: anywhere; +} + +.participants-empty { + list-style: none; + margin-left: 0; + color: #64748b; + font-style: italic; +} + +.participant-remove-btn { + border: none; + background-color: #fee2e2; + color: #b91c1c; + width: 30px; + height: 30px; + border-radius: 999px; + cursor: pointer; + font-size: 14px; + line-height: 1; + display: inline-flex; + align-items: center; + justify-content: center; + transition: background-color 0.2s ease, transform 0.2s ease; +} + +.participant-remove-btn:hover { + background-color: #fecaca; + transform: scale(1.05); +} + .form-group { margin-bottom: 15px; } From c4d442f4c19d7bbc62d99b7bf8e0722c8be6be57 Mon Sep 17 00:00:00 2001 From: Kevin Sebastin <89242806+KevinSebastin@users.noreply.github.com> Date: Wed, 27 May 2026 09:19:09 +0000 Subject: [PATCH 3/4] added some tests --- .vscode/settings.json | 5 ++ requirements.txt | 3 +- tests/conftest.py | 21 +++++++++ tests/test_activities.py | 19 ++++++++ tests/test_root.py | 10 ++++ tests/test_signup_unregister.py | 82 +++++++++++++++++++++++++++++++++ 6 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json create mode 100644 tests/conftest.py create mode 100644 tests/test_activities.py create mode 100644 tests/test_root.py create mode 100644 tests/test_signup_unregister.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..94a9dba --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "chat.tools.terminal.autoApprove": { + "pytest": true + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 5d9efb5..f2821b2 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/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..3692c39 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,21 @@ +import copy + +import pytest +from fastapi.testclient import TestClient + +from src.app import activities, app + + +@pytest.fixture +def client(): + return TestClient(app) + + +@pytest.fixture(autouse=True) +def reset_activities_state(): + baseline = copy.deepcopy(activities) + + yield + + activities.clear() + activities.update(copy.deepcopy(baseline)) diff --git a/tests/test_activities.py b/tests/test_activities.py new file mode 100644 index 0000000..600e245 --- /dev/null +++ b/tests/test_activities.py @@ -0,0 +1,19 @@ +def test_get_activities_returns_seeded_activity_data(client): + # Arrange + endpoint = "/activities" + + # Act + response = client.get(endpoint) + payload = response.json() + + # Assert + assert response.status_code == 200 + assert isinstance(payload, dict) + assert "Chess Club" in payload + + chess_club = payload["Chess Club"] + assert "description" in chess_club + assert "schedule" in chess_club + assert "max_participants" in chess_club + assert "participants" in chess_club + assert isinstance(chess_club["participants"], list) diff --git a/tests/test_root.py b/tests/test_root.py new file mode 100644 index 0000000..1ed1431 --- /dev/null +++ b/tests/test_root.py @@ -0,0 +1,10 @@ +def test_root_redirects_to_static_index(client): + # Arrange + endpoint = "/" + + # Act + response = client.get(endpoint, follow_redirects=False) + + # Assert + assert response.status_code == 307 + assert response.headers["location"] == "/static/index.html" diff --git a/tests/test_signup_unregister.py b/tests/test_signup_unregister.py new file mode 100644 index 0000000..0e4f359 --- /dev/null +++ b/tests/test_signup_unregister.py @@ -0,0 +1,82 @@ +def test_signup_for_activity_success(client): + # Arrange + activity_name = "Chess Club" + email = "new.student@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/signup", params={"email": email}) + + # Assert + assert response.status_code == 200 + assert response.json() == {"message": f"Signed up {email} for {activity_name}"} + + updated_activities = client.get("/activities").json() + assert email in updated_activities[activity_name]["participants"] + + +def test_signup_for_activity_rejects_duplicate_registration(client): + # Arrange + activity_name = "Chess Club" + existing_email = "michael@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/signup", params={"email": existing_email}) + + # Assert + assert response.status_code == 400 + assert response.json() == {"detail": "Student already signed up"} + + +def test_signup_for_activity_returns_not_found_for_unknown_activity(client): + # Arrange + activity_name = "Unknown Club" + email = "student@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/signup", params={"email": email}) + + # Assert + assert response.status_code == 404 + assert response.json() == {"detail": "Activity not found"} + + +def test_unregister_from_activity_success(client): + # Arrange + activity_name = "Programming Class" + email = "emma@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/unregister", params={"email": email}) + + # Assert + assert response.status_code == 200 + assert response.json() == {"message": f"Unregistered {email} from {activity_name}"} + + updated_activities = client.get("/activities").json() + assert email not in updated_activities[activity_name]["participants"] + + +def test_unregister_from_activity_returns_not_found_for_unknown_activity(client): + # Arrange + activity_name = "Unknown Club" + email = "student@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/unregister", params={"email": email}) + + # Assert + assert response.status_code == 404 + assert response.json() == {"detail": "Activity not found"} + + +def test_unregister_from_activity_rejects_non_member(client): + # Arrange + activity_name = "Chess Club" + email = "not.registered@mergington.edu" + + # Act + response = client.post(f"/activities/{activity_name}/unregister", params={"email": email}) + + # Assert + assert response.status_code == 400 + assert response.json() == {"detail": "Student is not signed up for this activity"} From c48737988965ded4f3e3ea419acfd1fe37896d7f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 27 May 2026 09:28:17 +0000 Subject: [PATCH 4/4] Escape participant values in activities list rendering --- src/static/app.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/static/app.js b/src/static/app.js index 9b4889d..afbb68d 100644 --- a/src/static/app.js +++ b/src/static/app.js @@ -4,6 +4,15 @@ document.addEventListener("DOMContentLoaded", () => { const signupForm = document.getElementById("signup-form"); const messageDiv = document.getElementById("message"); + function escapeHtml(value) { + return String(value) + .replaceAll("&", "&") + .replaceAll("<", "<") + .replaceAll(">", ">") + .replaceAll('"', """) + .replaceAll("'", "'"); + } + function showMessage(text, type) { messageDiv.textContent = text; messageDiv.className = type; @@ -45,13 +54,13 @@ document.addEventListener("DOMContentLoaded", () => { .map( (participant) => `
  • - ${participant} + ${escapeHtml(participant)}