Files
courrier/tests/test_courses_069.py
T

93 lines
4.9 KiB
Python

import io
from pathlib import Path
import pytest
from fastapi.testclient import TestClient
from openpyxl import load_workbook
from app.main import app
STATIC = Path(__file__).resolve().parent.parent / "app" / "static"
BASE = {"demande": "Rapporter 20 cartes SIM", "sens": "tunis_sfax", "message_id": "m-9", "objet_mail": "RE: Cartes SIM",
"expediteur": "Ali <ali@exemple.tn>", "date_mail": "2026-11-17T09:41:00+01:00"}
def test_courses_exigent_le_jeton():
c = TestClient(app)
assert c.get("/api/v1/courses").status_code == 401
assert c.get("/api/v1/courses/export").status_code == 401
def test_creation_liste_modification_suppression(client):
r = client.post("/api/v1/courses", json=BASE)
assert r.status_code == 201
k = r.json()
assert k["statut"] == "a_faire" and k["sens"] == "tunis_sfax" and k["fait_le"] is None and k["message_id"] == "m-9"
assert [x["id"] for x in client.get("/api/v1/courses?statut=a_faire").json()["courses"]] == [k["id"]]
assert client.get("/api/v1/courses?statut=faite").json()["courses"] == []
p = client.patch(f"/api/v1/courses/{k['id']}", json={"statut": "faite"}).json()
assert p["statut"] == "faite" and p["fait_le"]
p = client.patch(f"/api/v1/courses/{k['id']}", json={"statut": "a_faire", "demande": "Rapporter 25 cartes SIM", "sens": "sfax_tunis"}).json()
assert p["fait_le"] is None and p["demande"] == "Rapporter 25 cartes SIM" and p["sens"] == "sfax_tunis"
assert client.delete(f"/api/v1/courses/{k['id']}").status_code == 200
assert client.get("/api/v1/courses").json()["courses"] == []
assert client.delete(f"/api/v1/courses/{k['id']}").status_code == 404
assert client.patch("/api/v1/courses/999", json={"statut": "faite"}).status_code == 404
@pytest.mark.parametrize("corps", [{"demande": ""}, {"demande": " "}, {"demande": "x", "sens": "paris"}, {}])
def test_creation_invalide(client, corps):
assert client.post("/api/v1/courses", json=corps).status_code == 422
def test_modification_invalide(client):
k = client.post("/api/v1/courses", json=BASE).json()
assert client.patch(f"/api/v1/courses/{k['id']}", json={"statut": "zzz"}).status_code == 422
assert client.patch(f"/api/v1/courses/{k['id']}", json={"demande": " "}).status_code == 422
def test_les_faites_passent_en_dernier_dans_toutes(client):
a = client.post("/api/v1/courses", json=dict(BASE, demande="A")).json()
b = client.post("/api/v1/courses", json=dict(BASE, demande="B")).json()
client.patch(f"/api/v1/courses/{b['id']}", json={"statut": "faite"})
assert [x["demande"] for x in client.get("/api/v1/courses?statut=toutes").json()["courses"]] == ["A", "B"]
def test_export_xlsx_clair_et_sans_trace_d_outil(client):
a = client.post("/api/v1/courses", json=BASE).json()
client.post("/api/v1/courses", json=dict(BASE, demande="Apporter le cheque", sens="sfax_tunis"))
client.patch(f"/api/v1/courses/{a['id']}", json={"statut": "faite"})
r = client.get("/api/v1/courses/export?statut=toutes")
assert r.status_code == 200 and "attachment" in r.headers["content-disposition"] and r.headers["content-disposition"].endswith('.xlsx"')
wb = load_workbook(io.BytesIO(r.content))
ws = wb["COURSES"]
assert wb.properties.creator == "Nabil.Derouiche" and wb.properties.lastModifiedBy == "Nabil.Derouiche"
assert ws["A1"].value == "COURSES POUR TUNIS" and ws["B4"].value == "DEMANDE" and ws["H4"].value == "REALISEE LE"
titres = [ws.cell(row=4, column=i).value for i in range(1, 9)]
assert all(t == t.upper() and t.isascii() or t == "N°" for t in titres)
assert ws.max_row == 6
ligne = {ws.cell(row=r, column=2).value: [ws.cell(row=r, column=i).value for i in range(1, 9)] for r in (5, 6)}
assert ligne["Rapporter 20 cartes SIM"][2] == "Tunis vers Sfax" and ligne["Rapporter 20 cartes SIM"][3] == "17/11/2026"
assert ligne["Rapporter 20 cartes SIM"][6] == "Faite" and ligne["Rapporter 20 cartes SIM"][7]
assert ws.freeze_panes == "A5" and ws.page_setup.orientation == "landscape"
brut = r.content
import zipfile
z = zipfile.ZipFile(io.BytesIO(brut))
texte = (z.read("docProps/core.xml") + z.read("docProps/app.xml")).decode("utf-8", "ignore").lower()
assert "openpyxl" not in texte
def test_export_suit_le_filtre(client):
client.post("/api/v1/courses", json=dict(BASE, demande="Une"))
k = client.post("/api/v1/courses", json=dict(BASE, demande="Deux")).json()
client.patch(f"/api/v1/courses/{k['id']}", json={"statut": "faite"})
ws = load_workbook(io.BytesIO(client.get("/api/v1/courses/export?statut=a_faire").content))["COURSES"]
assert ws.max_row == 5 and ws["B5"].value == "Une"
assert client.get("/api/v1/courses/export?statut=nimporte").status_code == 422
def test_interface_courses():
js = (STATIC / "app.js").read_text(encoding="utf-8")
assert "vueCourses" in js and "dialogueCourse" in js and "Ajouter aux courses" in js and "innerHTML" not in js
assert "['courses', 'Courses pour Tunis', true]" in js