Files
benevoles/backend/tests/test_position_sheets_api.py
T
AdamThomasandClaude Sonnet 5 f6fbfaf82a Feuilles de poste : ajouter la photo des bénévoles affectés
Vignette carrée (14 mm, proportions préservées) à côté du nom de chaque
bénévole affecté, à côté de son téléphone — case vide si le bénévole n'a
pas de photo. Complète l'affichage déjà en place sur l'écran référent et
l'écran bénévole (étape 22).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 20:06:51 +02:00

121 lines
4.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Feuilles de poste (PDF) — API."""
import base64
from app.models import Assignment, Role
from tests.factories import dt_at, make_position, make_shift, make_volunteer
# PNG 1×1 valide (transparent), le plus petit possible.
_PNG = base64.b64decode(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY"
"42YAAAAASUVORK5CYII="
)
def _url(edition):
return f"/editions/{edition.id}/position-sheets/generate"
def test_is_coordinator_only(edition, make_account, as_account):
lead = make_account(Role.LEAD, email="lead@e.fr")
client = as_account(lead)
resp = client.post(_url(edition), json={})
assert resp.status_code == 403
def test_rejects_when_no_position_matches(client, edition):
resp = client.post(
_url(edition),
json={"position_ids": ["00000000-0000-0000-0000-000000000000"]},
)
assert resp.status_code == 422
assert resp.json()["detail"]["code"] == "position_sheets.no_positions"
def test_rejects_when_no_real_need_to_print(client, edition, session):
# Créneaux générés à 0/0, jamais remplis : rien à imprimer.
make_position(session, edition, name="Bar central")
resp = client.post(_url(edition), json={})
assert resp.status_code == 422
assert resp.json()["detail"]["code"] == "position_sheets.no_shifts"
def test_generates_a_pdf_with_assigned_volunteers(client, edition, session):
position = make_position(session, edition, name="Bar central")
shift = make_shift(
session, edition, position, start=dt_at(9, 19), end=dt_at(9, 22),
min_staff=1, ideal_staff=2,
)
volunteer = make_volunteer(session, edition, first_name="Alex", last_name="Martin")
session.add(
Assignment(edition_id=edition.id, volunteer_id=volunteer.id, shift_id=shift.id)
)
session.commit()
resp = client.post(_url(edition), json={})
assert resp.status_code == 200
assert resp.headers["content-type"] == "application/pdf"
assert resp.content.startswith(b"%PDF")
def test_generates_a_pdf_with_a_referent(client, edition, session, make_account):
position = make_position(session, edition, name="Bar central")
make_shift(
session, edition, position, start=dt_at(9, 19), end=dt_at(9, 22),
min_staff=1, ideal_staff=2,
)
referent_volunteer = make_volunteer(session, edition, first_name="Sam", last_name="Nuit")
referent_account = make_account(
Role.LEAD, volunteer_id=referent_volunteer.id, email="referent@e.fr"
)
client.patch(
f"/editions/{edition.id}/positions/{position.id}",
json={"lead_account_id": str(referent_account.id)},
)
resp = client.post(_url(edition), json={})
assert resp.status_code == 200
assert resp.content.startswith(b"%PDF")
def test_includes_a_volunteer_photo_when_present(client, edition, session):
position = make_position(session, edition, name="Bar central")
shift = make_shift(
session, edition, position, start=dt_at(9, 19), end=dt_at(9, 22),
min_staff=1, ideal_staff=2,
)
with_photo = make_volunteer(session, edition, first_name="Alex", last_name="Martin")
with_photo.photo = _PNG
with_photo.photo_content_type = "image/png"
without_photo = make_volunteer(session, edition, first_name="Sam", last_name="Nuit")
session.add_all(
[
Assignment(edition_id=edition.id, volunteer_id=with_photo.id, shift_id=shift.id),
Assignment(edition_id=edition.id, volunteer_id=without_photo.id, shift_id=shift.id),
]
)
session.commit()
resp = client.post(_url(edition), json={})
assert resp.status_code == 200
assert resp.content.startswith(b"%PDF")
def test_position_ids_filter_scopes_the_sheets(client, edition, session):
bar = make_position(session, edition, name="Bar central")
welcome = make_position(session, edition, name="Accueil")
make_shift(
session, edition, bar, start=dt_at(9, 19), end=dt_at(9, 22),
min_staff=1, ideal_staff=2,
)
make_shift(
session, edition, welcome, start=dt_at(9, 9), end=dt_at(9, 13),
min_staff=1, ideal_staff=2,
)
resp = client.post(_url(edition), json={"position_ids": [str(bar.id)]})
assert resp.status_code == 200
resp = client.post(_url(edition), json={"position_ids": [str(welcome.id)]})
assert resp.status_code == 200