"""Messages aux bénévoles : celui de l'organisation (`Edition. volunteer_message`, coordination) et celui de chaque poste (`Position. message`, référent du poste ou coordination).""" from app.models import Assignment, Role from tests.factories import dt_at, make_position, make_shift, make_volunteer def test_coordination_sets_the_organisation_message( client, edition, session, make_account, as_account ): resp = client.patch(f"/editions/{edition.id}", json={"volunteer_message": " Repas à 18h "}) assert resp.status_code == 200 assert resp.json()["volunteer_message"] == "Repas à 18h" assert resp.json()["volunteer_message_updated_at"] is not None volunteer = make_volunteer(session, edition) reader = as_account(make_account(Role.VOLUNTEER, volunteer_id=volunteer.id)) resp = reader.get(f"/editions/{edition.id}") assert resp.json()["volunteer_message"] == "Repas à 18h" def test_blank_message_clears_it(client, edition): client.patch(f"/editions/{edition.id}", json={"volunteer_message": "Bonjour"}) resp = client.patch(f"/editions/{edition.id}", json={"volunteer_message": " "}) assert resp.json()["volunteer_message"] is None assert resp.json()["volunteer_message_updated_at"] is None def test_unchanged_message_keeps_its_date(client, edition): first = client.patch(f"/editions/{edition.id}", json={"volunteer_message": "Bonjour"}).json() again = client.patch( f"/editions/{edition.id}", json={"volunteer_message": "Bonjour", "name": "X"} ).json() assert again["volunteer_message_updated_at"] == first["volunteer_message_updated_at"] def test_volunteer_and_lead_cannot_set_the_organisation_message(edition, make_account, as_account): for role in (Role.VOLUNTEER, Role.LEAD): resp = as_account(make_account(role)).patch( f"/editions/{edition.id}", json={"volunteer_message": "Hack"} ) assert resp.status_code == 403 def test_message_too_long_is_rejected(client, edition): resp = client.patch(f"/editions/{edition.id}", json={"volunteer_message": "x" * 2001}) assert resp.status_code == 422 def test_lead_sets_the_message_of_their_position(edition, session, make_account, as_account): lead = make_account(Role.LEAD) position = make_position(session, edition) position.lead_account_id = lead.id session.add(position) session.commit() resp = as_account(lead).patch( f"/editions/{edition.id}/positions/{position.id}", json={"message": "Gilet fourni"} ) assert resp.status_code == 200 assert resp.json()["message"] == "Gilet fourni" assert resp.json()["message_updated_at"] is not None def test_lead_cannot_set_the_message_of_another_position( edition, session, make_account, as_account ): position = make_position(session, edition) resp = as_account(make_account(Role.LEAD)).patch( f"/editions/{edition.id}/positions/{position.id}", json={"message": "Hack"} ) assert resp.status_code == 403 def test_position_message_reaches_the_volunteer_planning(client, edition, session): volunteer = make_volunteer(session, edition) position = make_position(session, edition) shift = make_shift(session, edition, position, start=dt_at(10, 9), end=dt_at(10, 12)) session.add(Assignment(edition_id=edition.id, volunteer_id=volunteer.id, shift_id=shift.id)) session.commit() client.patch( f"/editions/{edition.id}/positions/{position.id}", json={"message": "Gilet fourni"} ) (entry,) = client.get(f"/editions/{edition.id}/volunteers/{volunteer.id}/planning").json() assert entry["position_message"] == "Gilet fourni" assert entry["position_message_updated_at"] is not None