import uuid from app.models import Role def _url(edition, suffix=""): return f"/editions/{edition.id}/accounts{suffix}" def test_promotes_a_volunteer_account_to_lead(client, edition, make_account): account = make_account(Role.VOLUNTEER, email="v@ex.fr") resp = client.patch(_url(edition, f"/{account.id}"), json={"role": "LEAD"}) assert resp.status_code == 200 assert resp.json()["role"] == "LEAD" assert resp.json()["auth_method"] == "magic_link" def test_demotes_a_lead_account_to_volunteer(client, edition, make_account): account = make_account(Role.LEAD, email="l@ex.fr") resp = client.patch(_url(edition, f"/{account.id}"), json={"role": "VOLUNTEER"}) assert resp.status_code == 200 assert resp.json()["role"] == "VOLUNTEER" def test_cannot_change_role_of_a_coordinator_account(client, edition, make_account): account = make_account(Role.COORDINATOR, email="c@ex.fr", password="password123") resp = client.patch(_url(edition, f"/{account.id}"), json={"role": "LEAD"}) assert resp.status_code == 422 assert resp.json()["detail"]["code"] == "account.role_change_not_allowed" def test_cannot_promote_a_volunteer_to_coordinator(client, edition, make_account): account = make_account(Role.VOLUNTEER, email="v2@ex.fr") resp = client.patch(_url(edition, f"/{account.id}"), json={"role": "COORDINATOR"}) assert resp.status_code == 422 assert resp.json()["detail"]["code"] == "account.role_change_not_allowed" def test_unknown_account_is_404(client, edition): assert ( client.patch(_url(edition, f"/{uuid.uuid4()}"), json={"role": "LEAD"}).status_code == 404 ) def test_volunteer_role_forbidden(edition, make_account, as_account): volunteer_account = make_account(Role.VOLUNTEER, email="v3@ex.fr") other = make_account(Role.VOLUNTEER, email="v4@ex.fr") api = as_account(volunteer_account) resp = api.patch(_url(edition, f"/{other.id}"), json={"role": "LEAD"}) assert resp.status_code == 403