feat: implement Phase 3 participant self-management (stories 4.5, 6.1)
Implemented features: - Story 4.5: View Participant List - participants can see other active participants - Story 6.1: Update Profile - participants can edit name and gift ideas before matching - Utility functions for state management and business logic - Comprehensive unit and integration tests New files: - src/utils/participant.py - Business logic utilities - src/templates/participant/profile_edit.html - Profile edit form - tests/unit/test_participant_utils.py - Unit tests for utilities - tests/integration/test_participant_list.py - Integration tests for participant list - tests/integration/test_profile_update.py - Integration tests for profile updates Modified files: - src/routes/participant.py - Added dashboard participant list and profile edit route - src/templates/participant/dashboard.html - Added participant list section and edit link - src/forms/participant.py - Added ProfileUpdateForm - src/app.py - Added participant.profile_edit to setup check exemptions - tests/conftest.py - Added exchange_factory, participant_factory, auth_participant fixtures All tests passing. Phase 3.3 (reminder preferences) and 3.4 (withdrawal) remain to be implemented. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -75,3 +75,97 @@ def admin(db):
|
||||
db.session.add(admin)
|
||||
db.session.commit()
|
||||
return admin
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def exchange_factory(db):
|
||||
"""Factory for creating test exchanges.
|
||||
|
||||
Args:
|
||||
db: Database instance.
|
||||
|
||||
Returns:
|
||||
Function that creates and returns Exchange instances.
|
||||
"""
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from src.models.exchange import Exchange
|
||||
|
||||
def _create(state="draft", **kwargs):
|
||||
exchange = Exchange(
|
||||
slug=kwargs.get("slug", Exchange.generate_slug()),
|
||||
name=kwargs.get("name", "Test Exchange"),
|
||||
budget=kwargs.get("budget", "$25-50"),
|
||||
max_participants=kwargs.get("max_participants", 50),
|
||||
registration_close_date=kwargs.get(
|
||||
"registration_close_date", datetime.now(UTC) + timedelta(days=7)
|
||||
),
|
||||
exchange_date=kwargs.get(
|
||||
"exchange_date", datetime.now(UTC) + timedelta(days=14)
|
||||
),
|
||||
timezone=kwargs.get("timezone", "UTC"),
|
||||
state=state,
|
||||
)
|
||||
db.session.add(exchange)
|
||||
db.session.commit()
|
||||
return exchange
|
||||
|
||||
return _create
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def participant_factory(db, exchange_factory):
|
||||
"""Factory for creating test participants.
|
||||
|
||||
Args:
|
||||
db: Database instance.
|
||||
exchange_factory: Exchange factory fixture.
|
||||
|
||||
Returns:
|
||||
Function that creates and returns Participant instances.
|
||||
"""
|
||||
from src.models.participant import Participant
|
||||
|
||||
counter = {"value": 0}
|
||||
|
||||
def _create(exchange=None, **kwargs):
|
||||
if not exchange:
|
||||
exchange = exchange_factory()
|
||||
|
||||
counter["value"] += 1
|
||||
participant = Participant(
|
||||
exchange_id=exchange.id,
|
||||
name=kwargs.get("name", f"Test Participant {counter['value']}"),
|
||||
email=kwargs.get("email", f"test{counter['value']}@example.com"),
|
||||
gift_ideas=kwargs.get("gift_ideas", "Test ideas"),
|
||||
reminder_enabled=kwargs.get("reminder_enabled", True),
|
||||
withdrawn_at=kwargs.get("withdrawn_at"),
|
||||
)
|
||||
db.session.add(participant)
|
||||
db.session.commit()
|
||||
return participant
|
||||
|
||||
return _create
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def auth_participant(client, exchange_factory, participant_factory):
|
||||
"""Create an authenticated participant session.
|
||||
|
||||
Args:
|
||||
client: Flask test client.
|
||||
exchange_factory: Exchange factory fixture.
|
||||
participant_factory: Participant factory fixture.
|
||||
|
||||
Returns:
|
||||
Authenticated participant instance.
|
||||
"""
|
||||
exchange = exchange_factory(state="registration_open")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
session["user_id"] = participant.id
|
||||
session["user_type"] = "participant"
|
||||
session["exchange_id"] = exchange.id
|
||||
|
||||
return participant
|
||||
|
||||
135
tests/integration/test_participant_list.py
Normal file
135
tests/integration/test_participant_list.py
Normal file
@@ -0,0 +1,135 @@
|
||||
"""Integration tests for participant list functionality."""
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_participant_list_shows_all_active(
|
||||
client, auth_participant, participant_factory
|
||||
):
|
||||
"""Test participant list shows all active participants."""
|
||||
# auth_participant fixture creates session and one participant
|
||||
exchange = auth_participant.exchange
|
||||
|
||||
# Create 2 more active participants
|
||||
participant_factory(exchange=exchange, name="Bob")
|
||||
participant_factory(exchange=exchange, name="Charlie")
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Bob" in response.data
|
||||
assert b"Charlie" in response.data
|
||||
assert b"Participants (3)" in response.data
|
||||
|
||||
|
||||
def test_participant_list_excludes_withdrawn(
|
||||
client, auth_participant, participant_factory
|
||||
):
|
||||
"""Test withdrawn participants are not shown."""
|
||||
exchange = auth_participant.exchange
|
||||
|
||||
# Create active and withdrawn participants
|
||||
participant_factory(exchange=exchange, name="Active")
|
||||
participant_factory(
|
||||
exchange=exchange, name="Withdrawn", withdrawn_at=datetime.now(UTC)
|
||||
)
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Active" in response.data
|
||||
assert b"Withdrawn" not in response.data
|
||||
# Count should be 2 (auth_participant + Active)
|
||||
assert b"Participants (2)" in response.data
|
||||
|
||||
|
||||
def test_participant_list_shows_self_badge(client, auth_participant):
|
||||
"""Test participant list marks current user with badge."""
|
||||
exchange = auth_participant.exchange
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
# Should show the participant's name
|
||||
assert auth_participant.name.encode() in response.data
|
||||
# Should show "You" badge
|
||||
assert b"You" in response.data
|
||||
|
||||
|
||||
def test_participant_list_ordered_by_name(
|
||||
client, auth_participant, participant_factory
|
||||
):
|
||||
"""Test participants are shown in alphabetical order."""
|
||||
exchange = auth_participant.exchange
|
||||
|
||||
# Create participants with names that should be sorted
|
||||
participant_factory(exchange=exchange, name="Zoe")
|
||||
participant_factory(exchange=exchange, name="Alice")
|
||||
participant_factory(exchange=exchange, name="Bob")
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
# Check order by finding positions in response
|
||||
data = response.data.decode()
|
||||
alice_pos = data.find("Alice")
|
||||
bob_pos = data.find("Bob")
|
||||
zoe_pos = data.find("Zoe")
|
||||
|
||||
# All should be present
|
||||
assert alice_pos > 0
|
||||
assert bob_pos > 0
|
||||
assert zoe_pos > 0
|
||||
|
||||
# Should be in alphabetical order
|
||||
assert alice_pos < bob_pos < zoe_pos
|
||||
|
||||
|
||||
def test_participant_list_empty_state(client, exchange_factory, participant_factory):
|
||||
"""Test message shown when only one participant."""
|
||||
exchange = exchange_factory(state="registration_open")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
|
||||
# Create session for the only participant
|
||||
with client.session_transaction() as session:
|
||||
session["user_id"] = participant.id
|
||||
session["user_type"] = "participant"
|
||||
session["exchange_id"] = exchange.id
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Participants (1)" in response.data
|
||||
# Should show participant's own name
|
||||
assert participant.name.encode() in response.data
|
||||
|
||||
|
||||
def test_participant_list_requires_auth(client, exchange_factory):
|
||||
"""Test participant list requires authentication."""
|
||||
exchange = exchange_factory()
|
||||
|
||||
response = client.get(
|
||||
url_for("participant.dashboard", id=exchange.id), follow_redirects=False
|
||||
)
|
||||
|
||||
# Should redirect to login (or show error)
|
||||
assert response.status_code in [302, 403]
|
||||
|
||||
|
||||
def test_participant_list_different_exchange(
|
||||
client, auth_participant, exchange_factory, participant_factory
|
||||
):
|
||||
"""Test participants filtered by exchange."""
|
||||
exchange1 = auth_participant.exchange
|
||||
exchange2 = exchange_factory(state="registration_open")
|
||||
|
||||
# Create participant in different exchange
|
||||
participant_factory(exchange=exchange2, name="Other Exchange")
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange1.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
# Should not show participant from other exchange
|
||||
assert b"Other Exchange" not in response.data
|
||||
120
tests/integration/test_profile_update.py
Normal file
120
tests/integration/test_profile_update.py
Normal file
@@ -0,0 +1,120 @@
|
||||
"""Integration tests for profile update functionality."""
|
||||
|
||||
from flask import url_for
|
||||
|
||||
|
||||
def test_profile_update_get_shows_form(client, auth_participant):
|
||||
"""GET shows edit form with current values."""
|
||||
response = client.get(url_for("participant.profile_edit"))
|
||||
|
||||
assert response.status_code == 200
|
||||
assert auth_participant.name.encode() in response.data
|
||||
assert b"Edit Your Profile" in response.data
|
||||
|
||||
|
||||
def test_profile_update_post_success(client, auth_participant, db):
|
||||
"""POST updates profile successfully."""
|
||||
response = client.post(
|
||||
url_for("participant.profile_edit"),
|
||||
data={"name": "Updated Name", "gift_ideas": "Updated ideas"},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"profile has been updated" in response.data
|
||||
|
||||
# Verify database
|
||||
db.session.refresh(auth_participant)
|
||||
assert auth_participant.name == "Updated Name"
|
||||
assert auth_participant.gift_ideas == "Updated ideas"
|
||||
|
||||
|
||||
def test_profile_update_name_change(client, auth_participant, db):
|
||||
"""Name updates in database."""
|
||||
original_name = auth_participant.name
|
||||
|
||||
client.post(
|
||||
url_for("participant.profile_edit"),
|
||||
data={"name": "New Name", "gift_ideas": ""},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
db.session.refresh(auth_participant)
|
||||
assert auth_participant.name == "New Name"
|
||||
assert auth_participant.name != original_name
|
||||
|
||||
|
||||
def test_profile_update_locked_after_matching(
|
||||
client, exchange_factory, participant_factory
|
||||
):
|
||||
"""Profile edit blocked after matching."""
|
||||
exchange = exchange_factory(state="matched")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
session["user_id"] = participant.id
|
||||
session["user_type"] = "participant"
|
||||
session["exchange_id"] = exchange.id
|
||||
|
||||
response = client.get(url_for("participant.profile_edit"), follow_redirects=True)
|
||||
|
||||
assert b"profile is locked" in response.data
|
||||
|
||||
|
||||
def test_profile_update_form_validation_empty_name(client):
|
||||
"""Empty name shows validation error."""
|
||||
response = client.post(
|
||||
url_for("participant.profile_edit"), data={"name": "", "gift_ideas": "Test"}
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Name is required" in response.data
|
||||
|
||||
|
||||
def test_profile_update_requires_auth(client):
|
||||
"""Profile edit requires authentication."""
|
||||
response = client.get(url_for("participant.profile_edit"), follow_redirects=False)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_profile_update_strips_whitespace(client, auth_participant, db):
|
||||
"""Whitespace is stripped from name and gift ideas."""
|
||||
client.post(
|
||||
url_for("participant.profile_edit"),
|
||||
data={"name": " Spaces ", "gift_ideas": " Gift "},
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
db.session.refresh(auth_participant)
|
||||
assert auth_participant.name == "Spaces"
|
||||
assert auth_participant.gift_ideas == "Gift"
|
||||
|
||||
|
||||
def test_dashboard_shows_edit_link_when_allowed(client, auth_participant):
|
||||
"""Dashboard shows edit profile link when editing is allowed."""
|
||||
response = client.get(
|
||||
url_for("participant.dashboard", id=auth_participant.exchange_id)
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Edit Profile" in response.data
|
||||
|
||||
|
||||
def test_dashboard_hides_edit_link_after_matching(
|
||||
client, exchange_factory, participant_factory
|
||||
):
|
||||
"""Dashboard hides edit profile link after matching."""
|
||||
exchange = exchange_factory(state="matched")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
|
||||
with client.session_transaction() as session:
|
||||
session["user_id"] = participant.id
|
||||
session["user_type"] = "participant"
|
||||
session["exchange_id"] = exchange.id
|
||||
|
||||
response = client.get(url_for("participant.dashboard", id=exchange.id))
|
||||
|
||||
assert response.status_code == 200
|
||||
# Edit link should not be present
|
||||
assert b"Edit Profile" not in response.data
|
||||
123
tests/unit/test_participant_utils.py
Normal file
123
tests/unit/test_participant_utils.py
Normal file
@@ -0,0 +1,123 @@
|
||||
"""Unit tests for participant utility functions."""
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from src.utils.participant import (
|
||||
can_update_profile,
|
||||
get_active_participants,
|
||||
is_withdrawn,
|
||||
)
|
||||
|
||||
|
||||
def test_get_active_participants_excludes_withdrawn(
|
||||
exchange_factory, participant_factory
|
||||
):
|
||||
"""Test that get_active_participants excludes withdrawn participants."""
|
||||
exchange = exchange_factory()
|
||||
|
||||
# Create 2 active, 1 withdrawn
|
||||
active1 = participant_factory(exchange=exchange, name="Alice")
|
||||
active2 = participant_factory(exchange=exchange, name="Bob")
|
||||
withdrawn = participant_factory(
|
||||
exchange=exchange, name="Charlie", withdrawn_at=datetime.now(UTC)
|
||||
)
|
||||
|
||||
participants = get_active_participants(exchange.id)
|
||||
|
||||
assert len(participants) == 2
|
||||
assert active1 in participants
|
||||
assert active2 in participants
|
||||
assert withdrawn not in participants
|
||||
|
||||
|
||||
def test_get_active_participants_ordered_by_name(exchange_factory, participant_factory):
|
||||
"""Test that participants are ordered alphabetically."""
|
||||
exchange = exchange_factory()
|
||||
|
||||
participant_factory(exchange=exchange, name="Zoe")
|
||||
participant_factory(exchange=exchange, name="Alice")
|
||||
participant_factory(exchange=exchange, name="Bob")
|
||||
|
||||
participants = get_active_participants(exchange.id)
|
||||
|
||||
assert len(participants) == 3
|
||||
assert participants[0].name == "Alice"
|
||||
assert participants[1].name == "Bob"
|
||||
assert participants[2].name == "Zoe"
|
||||
|
||||
|
||||
def test_get_active_participants_empty_when_all_withdrawn(
|
||||
exchange_factory, participant_factory
|
||||
):
|
||||
"""Test that empty list returned when all participants withdrawn."""
|
||||
exchange = exchange_factory()
|
||||
|
||||
participant_factory(exchange=exchange, withdrawn_at=datetime.now(UTC))
|
||||
participant_factory(exchange=exchange, withdrawn_at=datetime.now(UTC))
|
||||
|
||||
participants = get_active_participants(exchange.id)
|
||||
|
||||
assert len(participants) == 0
|
||||
|
||||
|
||||
def test_get_active_participants_different_exchanges(
|
||||
exchange_factory, participant_factory
|
||||
):
|
||||
"""Test that participants are filtered by exchange_id."""
|
||||
exchange1 = exchange_factory()
|
||||
exchange2 = exchange_factory()
|
||||
|
||||
participant_factory(exchange=exchange1, name="Alice")
|
||||
participant_factory(exchange=exchange2, name="Bob")
|
||||
|
||||
participants = get_active_participants(exchange1.id)
|
||||
|
||||
assert len(participants) == 1
|
||||
assert participants[0].name == "Alice"
|
||||
|
||||
|
||||
def test_is_withdrawn_true(participant_factory):
|
||||
"""Test is_withdrawn returns True for withdrawn participant."""
|
||||
participant = participant_factory(withdrawn_at=datetime.now(UTC))
|
||||
assert is_withdrawn(participant) is True
|
||||
|
||||
|
||||
def test_is_withdrawn_false(participant_factory):
|
||||
"""Test is_withdrawn returns False for active participant."""
|
||||
participant = participant_factory()
|
||||
assert is_withdrawn(participant) is False
|
||||
|
||||
|
||||
def test_can_update_profile_draft_state(participant_factory, exchange_factory):
|
||||
"""Profile updates allowed in draft state."""
|
||||
exchange = exchange_factory(state="draft")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
assert can_update_profile(participant) is True
|
||||
|
||||
|
||||
def test_can_update_profile_registration_open(participant_factory, exchange_factory):
|
||||
"""Profile updates allowed when registration open."""
|
||||
exchange = exchange_factory(state="registration_open")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
assert can_update_profile(participant) is True
|
||||
|
||||
|
||||
def test_can_update_profile_registration_closed(participant_factory, exchange_factory):
|
||||
"""Profile updates allowed when registration closed."""
|
||||
exchange = exchange_factory(state="registration_closed")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
assert can_update_profile(participant) is True
|
||||
|
||||
|
||||
def test_can_update_profile_matched_state(participant_factory, exchange_factory):
|
||||
"""Profile updates blocked after matching."""
|
||||
exchange = exchange_factory(state="matched")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
assert can_update_profile(participant) is False
|
||||
|
||||
|
||||
def test_can_update_profile_completed_state(participant_factory, exchange_factory):
|
||||
"""Profile updates blocked when completed."""
|
||||
exchange = exchange_factory(state="completed")
|
||||
participant = participant_factory(exchange=exchange)
|
||||
assert can_update_profile(participant) is False
|
||||
Reference in New Issue
Block a user