import re from pathlib import Path from fastapi.testclient import TestClient from app.main import app STATIC = Path(__file__).resolve().parent.parent / "app" / "static" def test_page_servie_sans_jeton(): """L'HTML est servi sans jeton (Cloudflare Access protège l'hôte) ; /api reste protégé.""" c = TestClient(app) r = c.get("/") assert r.status_code == 200 assert "Courrier" in r.text assert 'lang="fr"' in r.text assert r.headers["cache-control"] == "no-store" assert "default-src 'self'" in r.headers["content-security-policy"] assert c.get("/api/v1/categories").status_code in (401, 503) def test_statiques_et_polices_locales(): c = TestClient(app) for chemin, type_ in [("/static/app.js", "javascript"), ("/static/app.css", "css"), ("/static/nyora.css", "css"), ("/static/fonts/next-400.woff2", "font")]: r = c.get(chemin) assert r.status_code == 200, chemin assert type_ in r.headers["content-type"] or type_ == "font" assert c.get("/static/../main.py").status_code in (400, 404) def test_aucune_requete_externe_ni_couleur_en_dur(): """Aucune URL externe ; aucune couleur hexadécimale hors du fichier de jetons.""" for nom in ["index.html", "app.css", "app.js", "fonts.css", "nyora.css"]: t = (STATIC / nom).read_text(encoding="utf-8") urls = [u for u in re.findall(r"https?://[^\s\"')]+", t) if "www.w3.org" not in u] assert not urls, (nom, urls) for nom in ["app.css", "app.js", "fonts.css"]: t = (STATIC / nom).read_text(encoding="utf-8") assert not re.search(r"#[0-9A-Fa-f]{3,8}\b", t), nom assert not re.search(r"\brgba?\(", t), nom def test_aucun_innerhtml(): assert "innerHTML" not in (STATIC / "app.js").read_text(encoding="utf-8") def test_a_classer_protegee(client): cats = client.get("/api/v1/categories").json()["categories"] ac = [c for c in cats if c["name"] == "À classer"][0] assert client.put(f"/api/v1/categories/{ac['id']}", json={"name": "Autre"}).status_code == 400 assert client.delete(f"/api/v1/categories/{ac['id']}").status_code == 400 def test_renommage_et_suppression_en_cascade(client): from app.db import get_connection cid = client.post("/api/v1/categories", json={"name": "Projet X"}).json()["id"] client.post("/api/v1/regles", json={"categorie": "Projet X", "expediteur": "a@b.tn", "statut": "active"}) client.post("/api/v1/classements/m1", json={"categorie": "Projet X", "mail_info": {"expediteur": "a@b.tn", "objet": "o", "date": "2026-11-17T09:00:00+01:00"}}) assert client.put(f"/api/v1/categories/{cid}", json={"name": "Projet Y"}).status_code == 200 assert client.get("/api/v1/classements/m1").json()["categorie"] == "Projet Y" assert any(r["categorie"] == "Projet Y" for r in client.get("/api/v1/regles").json()["regles"]) assert client.delete(f"/api/v1/categories/{cid}").status_code == 200 assert client.get("/api/v1/classements/m1").json()["categorie"] == "À classer" assert not any(r["categorie"] == "Projet Y" for r in client.get("/api/v1/regles").json()["regles"]) assert client.put(f"/api/v1/categories/{cid}", json={"name": "Tunis"}).status_code in (400, 404) def test_renommage_doublon_refuse(client): cid = client.post("/api/v1/categories", json={"name": "Zeta"}).json()["id"] assert client.put(f"/api/v1/categories/{cid}", json={"name": "Tunis"}).status_code == 400