Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeWkqZYNP6Wn3mGQsrL
70 lines
3.0 KiB
Python
70 lines
3.0 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app.main import app, mirror_client, MAX_CORPS_LECTURE
|
|
|
|
STATIC = Path(__file__).resolve().parent.parent / "app" / "static"
|
|
|
|
DONNEES = {
|
|
"message_id": "m-1", "dossier": "Boîte de réception",
|
|
"en_tetes": {"From": "Ali Ben Salah <ali@exemple.tn>", "To": "Nabil <nabil@exemple.tn>, autre@exemple.tn",
|
|
"cc": "copie@exemple.tn", "Subject": "Demande de prix", "Date": "Tue, 17 Nov 2026 09:41:00 +0100"},
|
|
"corps_texte": "Bonjour,\n\nMerci de confirmer.\n\nCordialement",
|
|
"pieces_jointes": [{"filename": "offre.pdf", "content_type": "application/pdf", "size": 20480}],
|
|
"taille_octets": 4096,
|
|
}
|
|
|
|
|
|
def test_lecture_exige_le_jeton():
|
|
assert TestClient(app).get("/api/v1/mail?id=m-1").status_code == 401
|
|
|
|
|
|
def test_lecture_renvoie_les_champs_utiles(client, monkeypatch):
|
|
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": DONNEES})
|
|
r = client.get("/api/v1/mail?id=m-1")
|
|
assert r.status_code == 200
|
|
j = r.json()
|
|
assert j["de"].startswith("Ali Ben Salah") and "autre@exemple.tn" in j["a"] and j["cc"] == "copie@exemple.tn"
|
|
assert j["objet"] == "Demande de prix" and j["corps"].startswith("Bonjour,") and j["tronque"] is False
|
|
assert j["pieces_jointes"] == [{"nom": "offre.pdf", "type": "application/pdf", "taille": 20480, "bloquee": False}]
|
|
|
|
|
|
def test_lecture_plafonne_un_corps_tres_long(client, monkeypatch):
|
|
gros = dict(DONNEES, corps_texte="x" * (MAX_CORPS_LECTURE + 500))
|
|
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": gros})
|
|
j = client.get("/api/v1/mail?id=m-1").json()
|
|
assert j["tronque"] is True and len(j["corps"]) == MAX_CORPS_LECTURE
|
|
|
|
|
|
def test_lecture_mail_introuvable_et_miroir_indisponible(client, monkeypatch):
|
|
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"erreur": "inconnu"})
|
|
assert client.get("/api/v1/mail?id=zzz").status_code == 404
|
|
|
|
def panne(mid):
|
|
raise ConnectionError("miroir hors service")
|
|
monkeypatch.setattr(mirror_client, "get_mail", panne)
|
|
r = client.get("/api/v1/mail?id=m-1")
|
|
assert r.status_code == 502 and "miroir hors service" not in r.text
|
|
|
|
|
|
def test_lecture_ne_touche_aucun_classement(client, monkeypatch, temp_db):
|
|
from app.db import list_classements
|
|
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": DONNEES})
|
|
avant = list_classements(db_path=temp_db)
|
|
client.get("/api/v1/mail?id=m-1")
|
|
assert list_classements(db_path=temp_db) == avant
|
|
|
|
|
|
def test_panneau_de_lecture_dans_la_page():
|
|
js = (STATIC / "app.js").read_text(encoding="utf-8")
|
|
assert "function lireMail" in js and "'/mail?id='" in js
|
|
assert "noopener noreferrer" in js and "innerHTML" not in js
|
|
assert "Afficher l\\'historique de la conversation" in js
|
|
|
|
|
|
def test_style_de_lecture_colonne_lisible_et_police_locale():
|
|
css = (STATIC / "app.css").read_text(encoding="utf-8")
|
|
assert ".lect-texte" in css and "max-width: 60ch" in css and "font: 400 17px/1.65 var(--ny-police)" in css
|