Files
benevoles/backend/tests/test_api_assignments.py
T
AdamThomasandClaude Sonnet 5 74e35f0195 Étape 5 : API CRUD, matrice et validation branchée
- CRUD complet, scopé par édition, sur Position, Shift, Volunteer, Assignment
  (routers app/api/*, schémas Pydantic, dépendance get_edition partagée)
- Volunteer : gestion des compétences par codes (création à la volée),
  suppression = effacement complet en cascade (RGPD)
- Shift : contrôle fin > début, poste de la même édition, compétences requises
- Assignment : toute écriture active passe par le moteur de validation ;
  violation bloquante -> 422 error.validation_failed, avertissements renvoyés
  dans la réponse. Endpoint POST .../assignments/validate en simulation.
  app/validation/loader.py construit la ValidationInput depuis la base.
- GET /editions/{id}/matrix : postes -> créneaux -> {min, ideal, confirmed,
  proposed}, source de l'écran principal
- 30 tests d'API (CRUD, 404/409/422, cascade, blocage, avertissements, matrice,
  loader) ; 104 tests au total ; alembic check toujours vert
- Clés de traduction shift.* / assignment.* / volunteer.* ajoutées

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uUsRg7XzopkCPWimwg9ZM
2026-09-09 22:54:22 +02:00

196 lines
6.5 KiB
Python

import datetime as dt
from tests.factories import (
declare_available_all_day,
dt_at,
make_position,
make_shift,
make_volunteer,
)
def _url(edition, suffix=""):
return f"/editions/{edition.id}/assignments{suffix}"
def _setup(session, edition, day_parts, *, available=True, skills=()):
position = make_position(session, edition)
shift = make_shift(
session, edition, position, start=dt_at(10, 14), end=dt_at(10, 18)
)
volunteer = make_volunteer(session, edition, email="v@ex.fr", skills=skills)
if available:
declare_available_all_day(
session, edition, volunteer, day_parts, dt.date(2026, 7, 10)
)
return volunteer, shift
def test_create_assignment_clean(client, edition, session, day_parts):
volunteer, shift = _setup(session, edition, day_parts)
resp = client.post(
_url(edition),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
)
assert resp.status_code == 201
body = resp.json()
assert body["warnings"] == []
assert body["assignment"]["status"] == "PROPOSED"
assert body["assignment"]["source"] == "MANUAL"
def test_create_assignment_returns_warnings(client, edition, session, day_parts):
# Bénévole non déclaré disponible + compétence requise absente.
volunteer, shift = _setup(session, edition, day_parts, available=False)
position = make_position(session, edition, name="Régie")
shift_with_skill = make_shift(
session,
edition,
position,
start=dt_at(10, 14),
end=dt_at(10, 18),
skills=["SOUND"],
)
resp = client.post(
_url(edition),
json={
"volunteer_id": str(volunteer.id),
"shift_id": str(shift_with_skill.id),
},
)
assert resp.status_code == 201
codes = {w["code"] for w in resp.json()["warnings"]}
assert "MISSING_REQUIRED_SKILL" in codes
assert "OUTSIDE_AVAILABILITY" in codes
def test_create_assignment_blocked_by_overlap(client, edition, session, day_parts):
volunteer, shift = _setup(session, edition, day_parts)
assert client.post(
_url(edition),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
).status_code == 201
position = make_position(session, edition, name="Parking")
overlapping = make_shift(
session, edition, position, start=dt_at(10, 16), end=dt_at(10, 20)
)
resp = client.post(
_url(edition),
json={
"volunteer_id": str(volunteer.id),
"shift_id": str(overlapping.id),
},
)
assert resp.status_code == 422
detail = resp.json()["detail"]
assert detail["code"] == "error.validation_failed"
assert any(v["code"] == "SHIFT_OVERLAP" for v in detail["violations"])
# Rien n'a été enregistré pour ce créneau.
listed = client.get(
_url(edition), params={"shift_id": str(overlapping.id)}
).json()
assert listed == []
def test_create_duplicate_assignment_conflicts(client, edition, session, day_parts):
volunteer, shift = _setup(session, edition, day_parts)
body = {"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)}
assert client.post(_url(edition), json=body).status_code == 201
resp = client.post(_url(edition), json=body)
assert resp.status_code == 409
assert resp.json()["detail"]["code"] == "assignment.duplicate"
def test_assignment_wrong_edition_is_422(client, edition, session, day_parts):
from app.models import Edition
other = Edition(
year=2027, start_date=dt.date(2027, 7, 1), end_date=dt.date(2027, 7, 4)
)
session.add(other)
session.commit()
session.refresh(other)
stranger = make_volunteer(session, other, email="s@ex.fr")
_, shift = _setup(session, edition, day_parts)
resp = client.post(
_url(edition),
json={"volunteer_id": str(stranger.id), "shift_id": str(shift.id)},
)
assert resp.status_code == 422
assert resp.json()["detail"]["code"] == "assignment.wrong_edition"
def test_validate_endpoint_does_not_persist(client, edition, session, day_parts):
volunteer, shift = _setup(session, edition, day_parts, available=False)
resp = client.post(
_url(edition, "/validate"),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
)
assert resp.status_code == 200
body = resp.json()
assert body["blocking"] is False
assert any(v["code"] == "OUTSIDE_AVAILABILITY" for v in body["violations"])
assert client.get(_url(edition)).json() == []
def test_update_status_revalidates_and_returns_warnings(
client, edition, session, day_parts
):
volunteer, shift = _setup(session, edition, day_parts, available=False)
aid = client.post(
_url(edition),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
).json()["assignment"]["id"]
resp = client.patch(
_url(edition, f"/{aid}"), json={"status": "CONFIRMED"}
)
assert resp.status_code == 200
assert resp.json()["assignment"]["status"] == "CONFIRMED"
assert any(
w["code"] == "OUTSIDE_AVAILABILITY" for w in resp.json()["warnings"]
)
def test_cancelling_an_assignment_skips_validation(
client, edition, session, day_parts
):
volunteer, shift = _setup(session, edition, day_parts)
first = client.post(
_url(edition),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
).json()["assignment"]["id"]
# Un second créneau qui chevauche, mais l'affectation est directement DECLINEE.
position = make_position(session, edition, name="X")
overlapping = make_shift(
session, edition, position, start=dt_at(10, 16), end=dt_at(10, 20)
)
created = client.post(
_url(edition),
json={
"volunteer_id": str(volunteer.id),
"shift_id": str(overlapping.id),
"status": "DECLINED",
},
)
assert created.status_code == 201
patched = client.patch(
_url(edition, f"/{first}"), json={"locked": True}
)
assert patched.status_code == 200
assert patched.json()["assignment"]["locked"] is True
def test_delete_assignment(client, edition, session, day_parts):
volunteer, shift = _setup(session, edition, day_parts)
aid = client.post(
_url(edition),
json={"volunteer_id": str(volunteer.id), "shift_id": str(shift.id)},
).json()["assignment"]["id"]
assert client.delete(_url(edition, f"/{aid}")).status_code == 204
assert client.get(_url(edition, f"/{aid}")).status_code == 404