import uuid from app.models import Role def _url(edition, suffix=""): return f"/editions/{edition.id}/positions{suffix}" def test_position_crud_roundtrip(client, edition): created = client.post( _url(edition), json={"name": "Billetterie", "category": "TICKETING", "location": "Entrée A"}, ) assert created.status_code == 201 pid = created.json()["id"] assert created.json()["category"] == "TICKETING" assert client.get(_url(edition, f"/{pid}")).json()["name"] == "Billetterie" assert [p["name"] for p in client.get(_url(edition)).json()] == ["Billetterie"] patched = client.patch( _url(edition, f"/{pid}"), json={"location": "Entrée B"} ) assert patched.json()["location"] == "Entrée B" assert patched.json()["name"] == "Billetterie" assert client.delete(_url(edition, f"/{pid}")).status_code == 204 assert client.get(_url(edition, f"/{pid}")).status_code == 404 def test_unknown_position_is_404(client, edition): assert client.get(_url(edition, f"/{uuid.uuid4()}")).status_code == 404 def test_unknown_edition_is_404(client): assert ( client.get(f"/editions/{uuid.uuid4()}/positions").status_code == 404 ) def test_sets_a_referent(client, edition, make_account): lead = make_account(Role.LEAD, email="referent@ex.fr") created = client.post( _url(edition), json={"name": "Bar", "category": "BAR"}, ) pid = created.json()["id"] resp = client.patch(_url(edition, f"/{pid}"), json={"lead_account_id": str(lead.id)}) assert resp.status_code == 200 assert resp.json()["lead_account_id"] == str(lead.id) def test_referent_must_have_the_lead_role(client, edition, make_account): volunteer_account = make_account(Role.VOLUNTEER, email="v@ex.fr") created = client.post(_url(edition), json={"name": "Bar", "category": "BAR"}) pid = created.json()["id"] resp = client.patch( _url(edition, f"/{pid}"), json={"lead_account_id": str(volunteer_account.id)} ) assert resp.status_code == 422 assert resp.json()["detail"]["code"] == "position.lead_must_be_referent_account" def test_referent_must_belong_to_the_same_edition(client, edition, session, make_account): import datetime as dt from app.models import Edition other_edition = Edition(year=2027, start_date=dt.date(2027, 7, 1), end_date=dt.date(2027, 7, 4)) session.add(other_edition) session.commit() session.refresh(other_edition) other_lead = make_account(Role.LEAD, email="stranger@ex.fr") other_lead.edition_id = other_edition.id session.add(other_lead) session.commit() created = client.post(_url(edition), json={"name": "Bar", "category": "BAR"}) pid = created.json()["id"] resp = client.patch( _url(edition, f"/{pid}"), json={"lead_account_id": str(other_lead.id)} ) assert resp.status_code == 422 assert resp.json()["detail"]["code"] == "position.lead_must_be_referent_account" def test_unknown_referent_is_422(client, edition): created = client.post(_url(edition), json={"name": "Bar", "category": "BAR"}) pid = created.json()["id"] resp = client.patch( _url(edition, f"/{pid}"), json={"lead_account_id": str(uuid.uuid4())} ) assert resp.status_code == 422 assert resp.json()["detail"]["code"] == "position.lead_must_be_referent_account" def test_clearing_the_referent_is_allowed(client, edition, make_account): lead = make_account(Role.LEAD, email="referent2@ex.fr") created = client.post(_url(edition), json={"name": "Bar", "category": "BAR"}) pid = created.json()["id"] client.patch(_url(edition, f"/{pid}"), json={"lead_account_id": str(lead.id)}) resp = client.patch(_url(edition, f"/{pid}"), json={"lead_account_id": None}) assert resp.status_code == 200 assert resp.json()["lead_account_id"] is None