Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XGaEeTDpfvpmLSvXNgt3Xe
88 lines
3.3 KiB
Python
88 lines
3.3 KiB
Python
import httpx
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from app import ia
|
|
from app.main import app, mirror_client
|
|
|
|
DONNEES = {"message_id": "m-1", "en_tetes": {"Subject": "Colis pour Tunis"},
|
|
"corps_texte": "Merci de rapporter le colis. Ali Test 12 345,678 DT"}
|
|
|
|
|
|
@pytest.fixture
|
|
def mail(monkeypatch):
|
|
monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": DONNEES})
|
|
|
|
|
|
def test_ia_desactivee_par_defaut(client):
|
|
assert client.get("/api/v1/ia/etat").json()["actif"] is False
|
|
|
|
|
|
def test_suggestion_refusee_si_ia_desactivee(client, mail, monkeypatch):
|
|
appels = []
|
|
monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: appels.append(1) or "Tunis")
|
|
assert client.get("/api/v1/ia/suggestion?id=m-1").status_code == 409
|
|
assert appels == []
|
|
|
|
|
|
def test_interrupteur_se_bascule(client):
|
|
assert client.put("/api/v1/ia/etat", json={"actif": True}).json()["actif"] is True
|
|
assert client.put("/api/v1/ia/etat", json={"actif": False}).json()["actif"] is False
|
|
|
|
|
|
def test_suggestion_proposee_jamais_appliquee(client, mail, monkeypatch):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: "Tunis")
|
|
j = client.get("/api/v1/ia/suggestion?id=m-1").json()
|
|
assert j == {"categorie": "Tunis", "source": "ia", "appliquee": False}
|
|
assert client.get("/api/v1/classements/m-1").status_code == 404
|
|
|
|
|
|
def test_reponse_hors_liste_devient_a_classer(client, mail, monkeypatch):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: "Je pense que c'est urgent")
|
|
assert client.get("/api/v1/ia/suggestion?id=m-1").json()["categorie"] == "À classer"
|
|
|
|
|
|
def test_refus_net_si_privaite_injoignable(client, mail, monkeypatch):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
def boom(*a, **k):
|
|
raise httpx.ConnectError("x")
|
|
monkeypatch.setattr(ia.httpx, "post", boom)
|
|
monkeypatch.setattr(ia, "_cle", lambda: "k")
|
|
r = client.get("/api/v1/ia/suggestion?id=m-1")
|
|
assert r.status_code == 503
|
|
|
|
|
|
def test_aucun_appel_direct_hors_privaite(client, mail, monkeypatch):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
urls = []
|
|
def faux(url, **k):
|
|
urls.append(url)
|
|
raise httpx.ConnectError("x")
|
|
monkeypatch.setattr(ia.httpx, "post", faux)
|
|
monkeypatch.setattr(ia, "_cle", lambda: "k")
|
|
client.get("/api/v1/ia/suggestion?id=m-1")
|
|
assert urls == [ia.PRIVAITE_URL] and "8401" in urls[0]
|
|
|
|
|
|
def test_aucun_contenu_de_mail_dans_les_journaux(client, mail, monkeypatch, caplog):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
class R:
|
|
status_code = 500
|
|
monkeypatch.setattr(ia.httpx, "post", lambda *a, **k: R())
|
|
monkeypatch.setattr(ia, "_cle", lambda: "k")
|
|
with caplog.at_level("DEBUG"):
|
|
client.get("/api/v1/ia/suggestion?id=m-1")
|
|
assert "Ali Test" not in caplog.text and "12 345" not in caplog.text and "Colis" not in caplog.text
|
|
|
|
|
|
def test_suggestion_exige_le_jeton_access():
|
|
assert TestClient(app).get("/api/v1/ia/suggestion?id=m-1").status_code == 401
|
|
|
|
|
|
def test_cle_absente_refus_net(client, mail, monkeypatch):
|
|
client.put("/api/v1/ia/etat", json={"actif": True})
|
|
monkeypatch.setattr(ia, "PRIVAITE_KEY_PATH", "/nonexistent/k")
|
|
assert client.get("/api/v1/ia/suggestion?id=m-1").status_code == 503
|