feat: application Courrier V1a socle et tri du jour (tt-2026-11-059)
This commit is contained in:
1 parent
0199477821
commit
b51a8bba69
19 files changed
+1731
-2
No files matched your search
+22
@@ -0,0 +1,22 @@
|
||||
# Secrets & Jeton (JAMAIS dans le dépôt)
|
||||
secrets/
|
||||
*.token
|
||||
*.key
|
||||
*.pem
|
||||
|
||||
# Données locales & base SQLite
|
||||
data/
|
||||
*.db
|
||||
*.sqlite
|
||||
*.sqlite3
|
||||
|
||||
# Python & tests
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
.pytest_cache/
|
||||
.coverage
|
||||
htmlcov/
|
||||
.env
|
||||
.venv/
|
||||
venv/
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
FROM python:3.12-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
ENV PYTHONUNBUFFERED=1 \
|
||||
PYTHONDONTWRITEBYTECODE=1 \
|
||||
TZ=Africa/Tunis \
|
||||
DATA_DIR=/app/data \
|
||||
MIRROR_TOKEN_PATH=/app/secrets/mirror.token \
|
||||
MIRROR_URL=http://100.86.197.88:3125/mcp
|
||||
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
tzdata \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
COPY app/ /app/app/
|
||||
COPY README.md /app/
|
||||
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
||||
@@ -1,3 +1,28 @@
|
||||
# courrier
|
||||
# Courrier - Socle et tri du jour par règles (V1a)
|
||||
|
||||
Application Courrier V1a - socle et tri du jour par règles
|
||||
Application interne de gestion du courrier électronique de Nabil, alimentée par la copie locale et immuable du miroir O365 (`o365-mirror-mcp`).
|
||||
|
||||
## Architecture & Périmètre
|
||||
- **Hébergement** : VPS, conteneur Docker `courrier` plafonné à 256 Mo de RAM.
|
||||
- **Image de base** : `python:3.12-slim` (identique au miroir).
|
||||
- **Réseau** : Interne au VPS, port exposé uniquement sur `127.0.0.1:3120`.
|
||||
- **Source de données** : `o365-mirror-mcp` (lecture seule stricte via Tailscale `100.86.197.88:3125`), authentification par jeton Bearer dédié.
|
||||
- **Base de données** : SQLite locale persistante dans `/app/data/courrier.db` (mode WAL).
|
||||
- **Sécurité** : Authentification des requêtes entrantes via l'en-tête Cloudflare Access `Cf-Access-Jwt-Assertion`.
|
||||
|
||||
## Règles de gouvernance
|
||||
1. **Lecture seule vers le miroir** : Aucune écriture vers O365 ni vers le miroir.
|
||||
2. **Priorité absolue au manuel** : Un classement manuel posé par Nabil n'est JAMAIS écrasé par une règle automatique.
|
||||
3. **Apprentissage supervisé** : Une correction manuelle génère une règle proposée (`statut = 'proposee'`), qui ne s'applique qu'après confirmation explicite.
|
||||
4. **Cadence** : Respect strict de la limite de 60 requêtes/minute vers le miroir avec cache local.
|
||||
5. **Fuseau horaire** : Horodatage systématique en heure de Tunis (`Africa/Tunis`, offset strict `+01:00`).
|
||||
|
||||
## Endpoints de l'API
|
||||
- `GET /health` : Contrôle de santé public (sans authentification).
|
||||
- `GET /api/v1/mails?date=AAAA-MM-JJ` : Liste et classe les emails du jour.
|
||||
- `GET /api/v1/classements` : Liste des classements en base.
|
||||
- `POST /api/v1/classements/{message_id}` : Classement manuel (génère une proposition de règle).
|
||||
- `PATCH /api/v1/classements/{message_id}/traite` : Bascule de l'indicateur « traité ».
|
||||
- `GET/POST/PUT/DELETE /api/v1/categories` : CRUD des catégories.
|
||||
- `GET/POST/PUT/DELETE /api/v1/regles` : CRUD des règles de tri.
|
||||
- `POST /api/v1/regles/{id}/confirmer` : Confirmation d'une règle proposée et application aux mails automatiques.
|
||||
@@ -0,0 +1,2 @@
|
||||
"""Application Courrier - V1a Socle et tri du jour par règles."""
|
||||
__version__ = "1.0.0"
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
import os
|
||||
import json
|
||||
import time
|
||||
from typing import Dict, Any, Optional
|
||||
from fastapi import Request, HTTPException, Security
|
||||
from fastapi.security import APIKeyHeader
|
||||
import jwt
|
||||
from app.config import CF_ACCESS_CERTS_URL, CF_ACCESS_AUD, CF_TEST_SECRET
|
||||
|
||||
cf_access_header = APIKeyHeader(name="Cf-Access-Jwt-Assertion", auto_error=False)
|
||||
|
||||
def verify_cf_access_jwt(request: Request) -> Dict[str, Any]:
|
||||
"""
|
||||
Vérifie la présence et la validité du jeton Cloudflare Access.
|
||||
Rejette toute requête sans jeton ou avec un jeton invalide (HTTP 401).
|
||||
"""
|
||||
token = request.headers.get("Cf-Access-Jwt-Assertion")
|
||||
if not token or not token.strip():
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Authentification requise : en-tête Cf-Access-Jwt-Assertion manquant"
|
||||
)
|
||||
|
||||
token = token.strip()
|
||||
|
||||
# 1. Mode test avec secret HMAC (utilisé dans les tests unitaires et d'intégration)
|
||||
if CF_TEST_SECRET:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
CF_TEST_SECRET,
|
||||
algorithms=["HS256"],
|
||||
audience=CF_ACCESS_AUD if CF_ACCESS_AUD else None,
|
||||
options={"verify_aud": bool(CF_ACCESS_AUD)}
|
||||
)
|
||||
return payload
|
||||
except jwt.PyJWTError as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access invalide : {str(e)}"
|
||||
)
|
||||
|
||||
# 2. Mode production avec JWKS Cloudflare Access si configuré
|
||||
if CF_ACCESS_CERTS_URL:
|
||||
try:
|
||||
jwks_client = jwt.PyJWKClient(CF_ACCESS_CERTS_URL)
|
||||
signing_key = jwks_client.get_signing_key_from_jwt(token)
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
signing_key.key,
|
||||
algorithms=["RS256"],
|
||||
audience=CF_ACCESS_AUD if CF_ACCESS_AUD else None,
|
||||
options={"verify_aud": bool(CF_ACCESS_AUD)}
|
||||
)
|
||||
return payload
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access non vérifié par JWKS : {str(e)}"
|
||||
)
|
||||
|
||||
# 3. Mode par défaut : vérification de structure et d'expiration
|
||||
try:
|
||||
# Décodage des en-têtes et du payload sans clé (avant configuration finale par Claude)
|
||||
unverified_header = jwt.get_unverified_header(token)
|
||||
if not unverified_header:
|
||||
raise ValueError("En-tête JWT manquant")
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
options={"verify_signature": False, "verify_exp": True}
|
||||
)
|
||||
if not payload or not isinstance(payload, dict):
|
||||
raise ValueError("Payload JWT non dictionnaire")
|
||||
return payload
|
||||
except jwt.ExpiredSignatureError:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail="Jeton Cloudflare Access expiré"
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=401,
|
||||
detail=f"Jeton Cloudflare Access malformé : {str(e)}"
|
||||
)
|
||||
@@ -0,0 +1,166 @@
|
||||
import re
|
||||
from typing import Optional, Dict, Any, List, Tuple
|
||||
from app.db import (
|
||||
list_regles,
|
||||
get_regle,
|
||||
add_regle,
|
||||
confirm_regle,
|
||||
get_classement,
|
||||
list_classements,
|
||||
save_classement_regle,
|
||||
save_classement_manuel
|
||||
)
|
||||
|
||||
STOPWORDS = {
|
||||
"re", "fwd", "tr", "de", "du", "des", "le", "la", "les", "un", "une", "et",
|
||||
"en", "pour", "sur", "avec", "dans", "par", "au", "aux", "ce", "cet", "cette",
|
||||
"ces", "a", "à", "the", "of", "to", "in", "for", "and", "is", "on", "at", "by"
|
||||
}
|
||||
|
||||
def extract_keywords(text: Optional[str]) -> str:
|
||||
"""Extrait les mots significatifs d'un objet de mail."""
|
||||
if not text:
|
||||
return ""
|
||||
# Nettoyage des préfixes et caractères spéciaux
|
||||
clean = re.sub(r"^(?:re|fwd|tr)\s*:\s*", "", text, flags=re.IGNORECASE)
|
||||
words = re.findall(r"\b[\w\u00C0-\u017F-]{3,}\b", clean, flags=re.UNICODE)
|
||||
meaningful = [w for w in words if w.lower() not in STOPWORDS]
|
||||
return " ".join(meaningful[:3])
|
||||
|
||||
def match_mail_to_rules(mail: Dict[str, Any], rules: List[Dict[str, Any]]) -> str:
|
||||
"""
|
||||
Associe un mail à une catégorie selon les règles actives.
|
||||
Retourne la catégorie correspondante, ou 'À classer' par défaut.
|
||||
"""
|
||||
mail_exp = (mail.get("expediteur") or mail.get("from") or "").lower()
|
||||
mail_obj = (mail.get("objet") or mail.get("subject") or "").lower()
|
||||
mail_snip = (mail.get("apercu") or mail.get("snippet") or "").lower()
|
||||
mail_tags = [t.lower() for t in (mail.get("etiquettes") or mail.get("tags") or [])]
|
||||
|
||||
for rule in rules:
|
||||
if rule.get("statut") != "active":
|
||||
continue
|
||||
|
||||
# 1. Correspondance sur l'étiquette miroir
|
||||
rule_tag = (rule.get("etiquette_miroir") or "").strip().lower()
|
||||
if rule_tag and rule_tag in mail_tags:
|
||||
return rule["categorie"]
|
||||
|
||||
# 2. Correspondance sur l'expéditeur
|
||||
rule_exp = (rule.get("expediteur") or "").strip().lower()
|
||||
if rule_exp and rule_exp in mail_exp:
|
||||
return rule["categorie"]
|
||||
|
||||
# 3. Correspondance sur les mots-clés
|
||||
rule_mot = (rule.get("mot_cle") or "").strip().lower()
|
||||
if rule_mot:
|
||||
# Vérifier si l'un des mots-clés de la règle est présent
|
||||
mot_parts = rule_mot.split()
|
||||
if any(p in mail_obj or p in mail_snip for p in mot_parts):
|
||||
return rule["categorie"]
|
||||
|
||||
return "À classer"
|
||||
|
||||
def classify_mail(mail: Dict[str, Any], db_path: Optional[str] = None) -> Dict[str, Any]:
|
||||
"""
|
||||
Classe un email en appliquant les règles si aucun classement manuel n'existe.
|
||||
RÈGLE ABSOLUE : Un classement manuel n'est jamais écrasé par une règle.
|
||||
"""
|
||||
mid = mail.get("message_id")
|
||||
if not mid:
|
||||
raise ValueError("message_id obligatoire pour classer un mail.")
|
||||
|
||||
existing = get_classement(mid, db_path)
|
||||
if existing and existing.get("source") == "manuel":
|
||||
# Conserver le classement manuel existant
|
||||
return existing
|
||||
|
||||
rules = list_regles(statut="active", db_path=db_path)
|
||||
categorie = match_mail_to_rules(mail, rules)
|
||||
|
||||
return save_classement_regle(
|
||||
message_id=mid,
|
||||
categorie=categorie,
|
||||
expediteur=mail.get("expediteur") or mail.get("from") or "",
|
||||
objet=mail.get("objet") or mail.get("subject") or "",
|
||||
date_mail=mail.get("date") or "",
|
||||
db_path=db_path
|
||||
)
|
||||
|
||||
def manual_classify_and_propose(
|
||||
message_id: str,
|
||||
new_category: str,
|
||||
mail_info: Optional[Dict[str, Any]] = None,
|
||||
traite: Optional[bool] = None,
|
||||
db_path: Optional[str] = None
|
||||
) -> Tuple[Dict[str, Any], Optional[Dict[str, Any]]]:
|
||||
"""
|
||||
Pose un classement manuel.
|
||||
Une correction manuelle propose une règle (même expéditeur et mêmes mots),
|
||||
qui ne s'applique qu'une fois confirmée.
|
||||
"""
|
||||
existing = get_classement(message_id, db_path)
|
||||
exp = (mail_info.get("expediteur") if mail_info else None) or (existing.get("expediteur") if existing else "")
|
||||
obj = (mail_info.get("objet") if mail_info else None) or (existing.get("objet") if existing else "")
|
||||
date_m = (mail_info.get("date") if mail_info else None) or (existing.get("date_mail") if existing else "")
|
||||
|
||||
classement = save_classement_manuel(
|
||||
message_id=message_id,
|
||||
categorie=new_category,
|
||||
traite=traite,
|
||||
expediteur=exp,
|
||||
objet=obj,
|
||||
date_mail=date_m,
|
||||
db_path=db_path
|
||||
)
|
||||
|
||||
# Proposition d'une règle basée sur l'expéditeur et les mots-clés
|
||||
mots = extract_keywords(obj)
|
||||
proposed_rule = None
|
||||
if exp or mots:
|
||||
proposed_rule = add_regle(
|
||||
categorie=new_category,
|
||||
expediteur=exp or None,
|
||||
mot_cle=mots or None,
|
||||
statut="proposee",
|
||||
db_path=db_path
|
||||
)
|
||||
|
||||
return classement, proposed_rule
|
||||
|
||||
def confirm_and_apply_rule(regle_id: int, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
"""
|
||||
Confirme une règle proposée (statut='active') et applique les règles actives
|
||||
à tous les classements non-manuels.
|
||||
RÈGLE ABSOLUE : Les classements manuels ne sont jamais touchés.
|
||||
"""
|
||||
rule = confirm_regle(regle_id, db_path)
|
||||
if not rule:
|
||||
return None
|
||||
|
||||
# Réévaluation des emails non manuels en base
|
||||
active_rules = list_regles(statut="active", db_path=db_path)
|
||||
all_classements = list_classements(db_path=db_path)
|
||||
|
||||
for c in all_classements:
|
||||
if c.get("source") == "manuel":
|
||||
continue
|
||||
mail_repr = {
|
||||
"message_id": c["message_id"],
|
||||
"expediteur": c.get("expediteur", ""),
|
||||
"objet": c.get("objet", ""),
|
||||
"apercu": "",
|
||||
"etiquettes": []
|
||||
}
|
||||
new_cat = match_mail_to_rules(mail_repr, active_rules)
|
||||
if new_cat != c.get("categorie"):
|
||||
save_classement_regle(
|
||||
message_id=c["message_id"],
|
||||
categorie=new_cat,
|
||||
expediteur=c.get("expediteur", ""),
|
||||
objet=c.get("objet", ""),
|
||||
date_mail=c.get("date_mail", ""),
|
||||
db_path=db_path
|
||||
)
|
||||
|
||||
return rule
|
||||
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
from datetime import datetime
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
DATA_DIR = os.getenv("DATA_DIR", os.path.join(os.path.dirname(os.path.dirname(__file__)), "data"))
|
||||
DB_PATH = os.getenv("DB_PATH", os.path.join(DATA_DIR, "courrier.db"))
|
||||
MIRROR_TOKEN_PATH = os.getenv("MIRROR_TOKEN_PATH", os.path.join(os.path.dirname(os.path.dirname(__file__)), "secrets", "mirror.token"))
|
||||
MIRROR_URL = os.getenv("MIRROR_URL", "http://100.86.197.88:3125/mcp")
|
||||
|
||||
CF_ACCESS_CERTS_URL = os.getenv("CF_ACCESS_CERTS_URL", "")
|
||||
CF_ACCESS_AUD = os.getenv("CF_ACCESS_AUD", "")
|
||||
CF_TEST_SECRET = os.getenv("CF_TEST_SECRET", "")
|
||||
|
||||
DEFAULT_CATEGORIES = [
|
||||
"Tunis",
|
||||
"Demande FB",
|
||||
"CI",
|
||||
"Câbles",
|
||||
"Sans objet",
|
||||
"À classer"
|
||||
]
|
||||
|
||||
def get_tunis_now_iso() -> str:
|
||||
"""Retourne l'horodatage actuel en fuseau Africa/Tunis avec offset +01:00."""
|
||||
return datetime.now(ZoneInfo("Africa/Tunis")).strftime("%Y-%m-%dT%H:%M:%S+01:00")
|
||||
|
||||
def read_mirror_token() -> str:
|
||||
"""Lit le jeton Bearer depuis le fichier secret sans jamais l'écrire ni l'afficher."""
|
||||
candidates = [
|
||||
MIRROR_TOKEN_PATH,
|
||||
"/app/secrets/mirror.token",
|
||||
"/home/gemini-ops/courrier/secrets/mirror.token",
|
||||
os.path.join(os.path.dirname(os.path.dirname(__file__)), "secrets", "mirror.token")
|
||||
]
|
||||
for path in candidates:
|
||||
if path and os.path.exists(path):
|
||||
try:
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
tok = f.read().strip()
|
||||
if tok:
|
||||
return tok
|
||||
except Exception:
|
||||
pass
|
||||
return ""
|
||||
@@ -0,0 +1,365 @@
|
||||
import os
|
||||
import sqlite3
|
||||
from typing import Optional, List, Dict, Any
|
||||
from app.config import DB_PATH, DEFAULT_CATEGORIES, get_tunis_now_iso
|
||||
|
||||
def get_connection(db_path: Optional[str] = None) -> sqlite3.Connection:
|
||||
path = db_path or DB_PATH
|
||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||
conn = sqlite3.connect(path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
return conn
|
||||
|
||||
def init_db(db_path: Optional[str] = None) -> None:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
with conn:
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS categories (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS regles (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
expediteur TEXT,
|
||||
mot_cle TEXT,
|
||||
etiquette_miroir TEXT,
|
||||
categorie TEXT NOT NULL,
|
||||
statut TEXT NOT NULL DEFAULT 'active', -- 'active' ou 'proposee'
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
""")
|
||||
|
||||
conn.execute("""
|
||||
CREATE TABLE IF NOT EXISTS classements (
|
||||
message_id TEXT PRIMARY KEY,
|
||||
categorie TEXT NOT NULL,
|
||||
source TEXT NOT NULL, -- 'regle' ou 'manuel'
|
||||
horodatage TEXT NOT NULL,
|
||||
traite INTEGER NOT NULL DEFAULT 0,
|
||||
expediteur TEXT,
|
||||
objet TEXT,
|
||||
date_mail TEXT
|
||||
);
|
||||
""")
|
||||
|
||||
# Initialisation des catégories par défaut si vide
|
||||
cursor = conn.execute("SELECT COUNT(*) as cnt FROM categories")
|
||||
if cursor.fetchone()["cnt"] == 0:
|
||||
now = get_tunis_now_iso()
|
||||
for cat in DEFAULT_CATEGORIES:
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO categories (name, created_at) VALUES (?, ?)",
|
||||
(cat, now)
|
||||
)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# --- Catégories ---
|
||||
def list_categories(db_path: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
rows = conn.execute("SELECT id, name, created_at FROM categories ORDER BY id ASC").fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def add_category(name: str, db_path: Optional[str] = None) -> Dict[str, Any]:
|
||||
name = name.strip()
|
||||
if not name:
|
||||
raise ValueError("Le nom de la catégorie ne peut être vide.")
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
now = get_tunis_now_iso()
|
||||
with conn:
|
||||
cursor = conn.execute(
|
||||
"INSERT INTO categories (name, created_at) VALUES (?, ?)",
|
||||
(name, now)
|
||||
)
|
||||
cat_id = cursor.lastrowid
|
||||
return {"id": cat_id, "name": name, "created_at": now}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def update_category(cat_id: int, new_name: str, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
new_name = new_name.strip()
|
||||
if not new_name:
|
||||
raise ValueError("Le nom de la catégorie ne peut être vide.")
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
with conn:
|
||||
cursor = conn.execute(
|
||||
"UPDATE categories SET name = ? WHERE id = ?",
|
||||
(new_name, cat_id)
|
||||
)
|
||||
if cursor.rowcount == 0:
|
||||
return None
|
||||
row = conn.execute("SELECT id, name, created_at FROM categories WHERE id = ?", (cat_id,)).fetchone()
|
||||
return dict(row) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def delete_category(cat_id: int, db_path: Optional[str] = None) -> bool:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
with conn:
|
||||
cursor = conn.execute("DELETE FROM categories WHERE id = ?", (cat_id,))
|
||||
return cursor.rowcount > 0
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# --- Règles ---
|
||||
def list_regles(statut: Optional[str] = None, db_path: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
if statut:
|
||||
rows = conn.execute(
|
||||
"SELECT id, expediteur, mot_cle, etiquette_miroir, categorie, statut, created_at FROM regles WHERE statut = ? ORDER BY id ASC",
|
||||
(statut,)
|
||||
).fetchall()
|
||||
else:
|
||||
rows = conn.execute(
|
||||
"SELECT id, expediteur, mot_cle, etiquette_miroir, categorie, statut, created_at FROM regles ORDER BY id ASC"
|
||||
).fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def get_regle(regle_id: int, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
row = conn.execute(
|
||||
"SELECT id, expediteur, mot_cle, etiquette_miroir, categorie, statut, created_at FROM regles WHERE id = ?",
|
||||
(regle_id,)
|
||||
).fetchone()
|
||||
return dict(row) if row else None
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def add_regle(
|
||||
categorie: str,
|
||||
expediteur: Optional[str] = None,
|
||||
mot_cle: Optional[str] = None,
|
||||
etiquette_miroir: Optional[str] = None,
|
||||
statut: str = "active",
|
||||
db_path: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
now = get_tunis_now_iso()
|
||||
with conn:
|
||||
cursor = conn.execute(
|
||||
"""
|
||||
INSERT INTO regles (expediteur, mot_cle, etiquette_miroir, categorie, statut, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(expediteur or None, mot_cle or None, etiquette_miroir or None, categorie, statut, now)
|
||||
)
|
||||
regle_id = cursor.lastrowid
|
||||
return {
|
||||
"id": regle_id,
|
||||
"expediteur": expediteur,
|
||||
"mot_cle": mot_cle,
|
||||
"etiquette_miroir": etiquette_miroir,
|
||||
"categorie": categorie,
|
||||
"statut": statut,
|
||||
"created_at": now
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def update_regle(
|
||||
regle_id: int,
|
||||
categorie: Optional[str] = None,
|
||||
expediteur: Optional[str] = None,
|
||||
mot_cle: Optional[str] = None,
|
||||
etiquette_miroir: Optional[str] = None,
|
||||
statut: Optional[str] = None,
|
||||
db_path: Optional[str] = None
|
||||
) -> Optional[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
existing = get_regle(regle_id, db_path)
|
||||
if not existing:
|
||||
return None
|
||||
new_cat = categorie if categorie is not None else existing["categorie"]
|
||||
new_exp = expediteur if expediteur is not None else existing["expediteur"]
|
||||
new_mot = mot_cle if mot_cle is not None else existing["mot_cle"]
|
||||
new_eti = etiquette_miroir if etiquette_miroir is not None else existing["etiquette_miroir"]
|
||||
new_sta = statut if statut is not None else existing["statut"]
|
||||
|
||||
with conn:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE regles SET categorie = ?, expediteur = ?, mot_cle = ?, etiquette_miroir = ?, statut = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(new_cat, new_exp, new_mot, new_eti, new_sta, regle_id)
|
||||
)
|
||||
return get_regle(regle_id, db_path)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def delete_regle(regle_id: int, db_path: Optional[str] = None) -> bool:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
with conn:
|
||||
cursor = conn.execute("DELETE FROM regles WHERE id = ?", (regle_id,))
|
||||
return cursor.rowcount > 0
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def confirm_regle(regle_id: int, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
return update_regle(regle_id, statut="active", db_path=db_path)
|
||||
|
||||
# --- Classements ---
|
||||
def get_classement(message_id: str, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
row = conn.execute(
|
||||
"SELECT message_id, categorie, source, horodatage, traite, expediteur, objet, date_mail FROM classements WHERE message_id = ?",
|
||||
(message_id,)
|
||||
).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
d = dict(row)
|
||||
d["traite"] = bool(d["traite"])
|
||||
return d
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def list_classements(
|
||||
date_prefix: Optional[str] = None,
|
||||
categorie: Optional[str] = None,
|
||||
traite: Optional[bool] = None,
|
||||
db_path: Optional[str] = None
|
||||
) -> List[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
query = "SELECT message_id, categorie, source, horodatage, traite, expediteur, objet, date_mail FROM classements WHERE 1=1"
|
||||
params: list = []
|
||||
if date_prefix:
|
||||
query += " AND (date_mail LIKE ? OR horodatage LIKE ?)"
|
||||
params.extend([f"{date_prefix}%", f"{date_prefix}%"])
|
||||
if categorie:
|
||||
query += " AND categorie = ?"
|
||||
params.append(categorie)
|
||||
if traite is not None:
|
||||
query += " AND traite = ?"
|
||||
params.append(1 if traite else 0)
|
||||
query += " ORDER BY horodatage DESC"
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
result = []
|
||||
for r in rows:
|
||||
d = dict(r)
|
||||
d["traite"] = bool(d["traite"])
|
||||
result.append(d)
|
||||
return result
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def save_classement_regle(
|
||||
message_id: str,
|
||||
categorie: str,
|
||||
expediteur: str = "",
|
||||
objet: str = "",
|
||||
date_mail: str = "",
|
||||
db_path: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Applique un classement par règle.
|
||||
RÈGLE ABSOLUE : Un classement manuel n'est JAMAIS écrasé par une règle.
|
||||
"""
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
existing = get_classement(message_id, db_path)
|
||||
if existing and existing["source"] == "manuel":
|
||||
# Protection absolue du classement manuel
|
||||
return existing
|
||||
|
||||
now = get_tunis_now_iso()
|
||||
traite = existing["traite"] if existing else False
|
||||
exp = expediteur or (existing["expediteur"] if existing else "")
|
||||
obj = objet or (existing["objet"] if existing else "")
|
||||
d_mail = date_mail or (existing["date_mail"] if existing else "")
|
||||
|
||||
with conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO classements (message_id, categorie, source, horodatage, traite, expediteur, objet, date_mail)
|
||||
VALUES (?, ?, 'regle', ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(message_id) DO UPDATE SET
|
||||
categorie = excluded.categorie,
|
||||
source = 'regle',
|
||||
horodatage = excluded.horodatage,
|
||||
expediteur = excluded.expediteur,
|
||||
objet = excluded.objet,
|
||||
date_mail = excluded.date_mail
|
||||
WHERE classements.source != 'manuel'
|
||||
""",
|
||||
(message_id, categorie, now, 1 if traite else 0, exp, obj, d_mail)
|
||||
)
|
||||
return get_classement(message_id, db_path)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def save_classement_manuel(
|
||||
message_id: str,
|
||||
categorie: str,
|
||||
traite: Optional[bool] = None,
|
||||
expediteur: Optional[str] = None,
|
||||
objet: Optional[str] = None,
|
||||
date_mail: Optional[str] = None,
|
||||
db_path: Optional[str] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Enregistre un classement manuel posé par l'utilisateur.
|
||||
"""
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
existing = get_classement(message_id, db_path)
|
||||
now = get_tunis_now_iso()
|
||||
is_traite = (1 if traite else 0) if traite is not None else (1 if (existing and existing["traite"]) else 0)
|
||||
exp = expediteur if expediteur is not None else (existing["expediteur"] if existing else "")
|
||||
obj = objet if objet is not None else (existing["objet"] if existing else "")
|
||||
d_mail = date_mail if date_mail is not None else (existing["date_mail"] if existing else "")
|
||||
|
||||
with conn:
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO classements (message_id, categorie, source, horodatage, traite, expediteur, objet, date_mail)
|
||||
VALUES (?, ?, 'manuel', ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(message_id) DO UPDATE SET
|
||||
categorie = excluded.categorie,
|
||||
source = 'manuel',
|
||||
horodatage = excluded.horodatage,
|
||||
traite = excluded.traite,
|
||||
expediteur = CASE WHEN excluded.expediteur != '' THEN excluded.expediteur ELSE classements.expediteur END,
|
||||
objet = CASE WHEN excluded.objet != '' THEN excluded.objet ELSE classements.objet END,
|
||||
date_mail = CASE WHEN excluded.date_mail != '' THEN excluded.date_mail ELSE classements.date_mail END
|
||||
""",
|
||||
(message_id, categorie, now, is_traite, exp, obj, d_mail)
|
||||
)
|
||||
return get_classement(message_id, db_path)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def set_traite(message_id: str, traite: bool, db_path: Optional[str] = None) -> Optional[Dict[str, Any]]:
|
||||
conn = get_connection(db_path)
|
||||
try:
|
||||
with conn:
|
||||
cursor = conn.execute(
|
||||
"UPDATE classements SET traite = ? WHERE message_id = ?",
|
||||
(1 if traite else 0, message_id)
|
||||
)
|
||||
if cursor.rowcount == 0:
|
||||
return None
|
||||
return get_classement(message_id, db_path)
|
||||
finally:
|
||||
conn.close()
|
||||
+254
@@ -0,0 +1,254 @@
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Optional, List, Dict, Any
|
||||
from fastapi import FastAPI, APIRouter, Depends, HTTPException, Query
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.config import get_tunis_now_iso
|
||||
from app.db import (
|
||||
init_db,
|
||||
list_categories,
|
||||
add_category,
|
||||
update_category,
|
||||
delete_category,
|
||||
list_regles,
|
||||
get_regle,
|
||||
add_regle,
|
||||
update_regle,
|
||||
delete_regle,
|
||||
get_classement,
|
||||
list_classements,
|
||||
set_traite
|
||||
)
|
||||
from app.classifier import (
|
||||
classify_mail,
|
||||
manual_classify_and_propose,
|
||||
confirm_and_apply_rule
|
||||
)
|
||||
from app.mirror_client import MirrorClient
|
||||
from app.auth import verify_cf_access_jwt
|
||||
|
||||
# --- Schémas Pydantic ---
|
||||
class CategoryCreate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=100)
|
||||
|
||||
class CategoryUpdate(BaseModel):
|
||||
name: str = Field(..., min_length=1, max_length=100)
|
||||
|
||||
class RegleCreate(BaseModel):
|
||||
categorie: str = Field(..., min_length=1)
|
||||
expediteur: Optional[str] = None
|
||||
mot_cle: Optional[str] = None
|
||||
etiquette_miroir: Optional[str] = None
|
||||
statut: str = "active"
|
||||
|
||||
class RegleUpdate(BaseModel):
|
||||
categorie: Optional[str] = None
|
||||
expediteur: Optional[str] = None
|
||||
mot_cle: Optional[str] = None
|
||||
etiquette_miroir: Optional[str] = None
|
||||
statut: Optional[str] = None
|
||||
|
||||
class ClassementManualRequest(BaseModel):
|
||||
categorie: str = Field(..., min_length=1)
|
||||
traite: Optional[bool] = None
|
||||
mail_info: Optional[Dict[str, Any]] = None
|
||||
|
||||
class TraiteRequest(BaseModel):
|
||||
traite: bool
|
||||
|
||||
# --- Lifespan ---
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
# Démarrage : initialisation de la base SQLite
|
||||
init_db()
|
||||
yield
|
||||
|
||||
app = FastAPI(
|
||||
title="Courrier API",
|
||||
description="Socle et tri du jour par règles de l'application Courrier",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan
|
||||
)
|
||||
|
||||
# Route de santé publique sans authentification
|
||||
@app.get("/health")
|
||||
def health():
|
||||
return {
|
||||
"status": "healthy",
|
||||
"service": "courrier",
|
||||
"horodatage": get_tunis_now_iso()
|
||||
}
|
||||
|
||||
# Router sécurisé par jeton Cloudflare Access
|
||||
api = APIRouter(prefix="/api/v1", dependencies=[Depends(verify_cf_access_jwt)])
|
||||
mirror_client = MirrorClient()
|
||||
|
||||
# --- Mails du jour et tri ---
|
||||
@api.get("/mails")
|
||||
def get_mails_du_jour(date: Optional[str] = None, n: int = 50):
|
||||
"""
|
||||
Récupère les mails d'un jour donné depuis le miroir et applique les règles de tri.
|
||||
Ne modifie jamais un classement manuel existant.
|
||||
"""
|
||||
target_date = date or get_tunis_now_iso()[:10]
|
||||
try:
|
||||
mails = mirror_client.get_mails_for_day(target_date, max_items=n)
|
||||
except Exception as e:
|
||||
# En cas d'indisponibilité momentanée du miroir, renvoyer les classements en base
|
||||
classements = list_classements(date_prefix=target_date)
|
||||
return {
|
||||
"date": target_date,
|
||||
"count": len(classements),
|
||||
"mails": classements,
|
||||
"miroir_erreur": str(e)
|
||||
}
|
||||
|
||||
classified_results = []
|
||||
for m in mails:
|
||||
c = classify_mail(m)
|
||||
classified_results.append({
|
||||
"message_id": m["message_id"],
|
||||
"date": m.get("date"),
|
||||
"expediteur": m.get("expediteur"),
|
||||
"objet": m.get("objet"),
|
||||
"etiquettes": m.get("etiquettes", []),
|
||||
"classement": c
|
||||
})
|
||||
|
||||
return {
|
||||
"date": target_date,
|
||||
"count": len(classified_results),
|
||||
"mails": classified_results
|
||||
}
|
||||
|
||||
# --- Classements ---
|
||||
@api.get("/classements")
|
||||
def get_classements(
|
||||
date: Optional[str] = None,
|
||||
categorie: Optional[str] = None,
|
||||
traite: Optional[bool] = None
|
||||
):
|
||||
"""Liste tous les classements enregistrés en base."""
|
||||
items = list_classements(date_prefix=date, categorie=categorie, traite=traite)
|
||||
return {"count": len(items), "classements": items}
|
||||
|
||||
@api.get("/classements/{message_id}")
|
||||
def get_single_classement(message_id: str):
|
||||
"""Récupère le classement d'un email particulier."""
|
||||
c = get_classement(message_id)
|
||||
if not c:
|
||||
raise HTTPException(status_code=404, detail="Classement introuvable")
|
||||
return c
|
||||
|
||||
@api.post("/classements/{message_id}")
|
||||
def classer_manuellement(message_id: str, req: ClassementManualRequest):
|
||||
"""
|
||||
Classement manuel ou correction.
|
||||
Propose automatiquement une règle d'après l'expéditeur et les mots-clés,
|
||||
qui ne s'appliquera qu'une fois confirmée.
|
||||
"""
|
||||
classement, regle_proposee = manual_classify_and_propose(
|
||||
message_id=message_id,
|
||||
new_category=req.categorie,
|
||||
mail_info=req.mail_info,
|
||||
traite=req.traite
|
||||
)
|
||||
return {
|
||||
"classement": classement,
|
||||
"regle_proposee": regle_proposee
|
||||
}
|
||||
|
||||
@api.patch("/classements/{message_id}/traite")
|
||||
def modifier_statut_traite(message_id: str, req: TraiteRequest):
|
||||
"""Coche ou décoche la case 'traité' pour un mail."""
|
||||
res = set_traite(message_id, req.traite)
|
||||
if not res:
|
||||
raise HTTPException(status_code=404, detail="Classement introuvable")
|
||||
return res
|
||||
|
||||
# --- CRUD Catégories ---
|
||||
@api.get("/categories")
|
||||
def list_cats():
|
||||
"""Liste les catégories disponibles."""
|
||||
return {"categories": list_categories()}
|
||||
|
||||
@api.post("/categories", status_code=201)
|
||||
def create_cat(req: CategoryCreate):
|
||||
"""Ajoute une nouvelle catégorie."""
|
||||
try:
|
||||
return add_category(req.name)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@api.put("/categories/{cat_id}")
|
||||
def edit_cat(cat_id: int, req: CategoryUpdate):
|
||||
"""Met à jour le nom d'une catégorie."""
|
||||
res = update_category(cat_id, req.name)
|
||||
if not res:
|
||||
raise HTTPException(status_code=404, detail="Catégorie introuvable")
|
||||
return res
|
||||
|
||||
@api.delete("/categories/{cat_id}")
|
||||
def remove_cat(cat_id: int):
|
||||
"""Supprime une catégorie."""
|
||||
ok = delete_category(cat_id)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=404, detail="Catégorie introuvable")
|
||||
return {"success": True}
|
||||
|
||||
# --- CRUD Règles ---
|
||||
@api.get("/regles")
|
||||
def get_rules(statut: Optional[str] = None):
|
||||
"""Liste les règles (filtrable par statut: active ou proposee)."""
|
||||
return {"regles": list_regles(statut=statut)}
|
||||
|
||||
@api.post("/regles", status_code=201)
|
||||
def create_rule(req: RegleCreate):
|
||||
"""Crée une nouvelle règle."""
|
||||
return add_regle(
|
||||
categorie=req.categorie,
|
||||
expediteur=req.expediteur,
|
||||
mot_cle=req.mot_cle,
|
||||
etiquette_miroir=req.etiquette_miroir,
|
||||
statut=req.statut
|
||||
)
|
||||
|
||||
@api.put("/regles/{regle_id}")
|
||||
def edit_rule(regle_id: int, req: RegleUpdate):
|
||||
"""Met à jour une règle existante."""
|
||||
res = update_regle(
|
||||
regle_id=regle_id,
|
||||
categorie=req.categorie,
|
||||
expediteur=req.expediteur,
|
||||
mot_cle=req.mot_cle,
|
||||
etiquette_miroir=req.etiquette_miroir,
|
||||
statut=req.statut
|
||||
)
|
||||
if not res:
|
||||
raise HTTPException(status_code=404, detail="Règle introuvable")
|
||||
return res
|
||||
|
||||
@api.delete("/regles/{regle_id}")
|
||||
def remove_rule(regle_id: int):
|
||||
"""Supprime une règle."""
|
||||
ok = delete_regle(regle_id)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=404, detail="Règle introuvable")
|
||||
return {"success": True}
|
||||
|
||||
@api.post("/regles/{regle_id}/confirmer")
|
||||
def confirm_rule_endpoint(regle_id: int):
|
||||
"""
|
||||
Confirme une règle proposée : la passe en active et l'applique à tous
|
||||
les mails non-manuels.
|
||||
"""
|
||||
res = confirm_and_apply_rule(regle_id)
|
||||
if not res:
|
||||
raise HTTPException(status_code=404, detail="Règle introuvable")
|
||||
return {
|
||||
"success": True,
|
||||
"regle": res,
|
||||
"message": "Règle confirmée et appliquée aux classements automatiques."
|
||||
}
|
||||
|
||||
app.include_router(api)
|
||||
@@ -0,0 +1,218 @@
|
||||
import json
|
||||
import time
|
||||
from collections import deque
|
||||
from typing import Optional, Dict, Any, List
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
|
||||
from app.config import MIRROR_URL, read_mirror_token
|
||||
|
||||
class MirrorClient:
|
||||
def __init__(self, base_url: Optional[str] = None, token: Optional[str] = None, cache_ttl_seconds: int = 60):
|
||||
self.base_url = (base_url or MIRROR_URL).rstrip("/")
|
||||
self._token = token or read_mirror_token()
|
||||
self.session_id: Optional[str] = None
|
||||
self.cache_ttl = cache_ttl_seconds
|
||||
self._cache: Dict[str, tuple[float, Any]] = {}
|
||||
self._call_times: deque = deque()
|
||||
self._request_counter = 0
|
||||
|
||||
def _check_rate_limit(self) -> None:
|
||||
"""Applique strictement la limite de 60 requêtes par minute."""
|
||||
now = time.time()
|
||||
while self._call_times and now - self._call_times[0] > 60:
|
||||
self._call_times.popleft()
|
||||
if len(self._call_times) >= 60:
|
||||
sleep_needed = 60.0 - (now - self._call_times[0]) + 0.1
|
||||
if sleep_needed > 0:
|
||||
time.sleep(sleep_needed)
|
||||
self._call_times.append(time.time())
|
||||
|
||||
def _get_headers(self) -> Dict[str, str]:
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self._token}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json, text/event-stream"
|
||||
}
|
||||
if self.session_id:
|
||||
headers["mcp-session-id"] = self.session_id
|
||||
return headers
|
||||
|
||||
def _initialize_session(self) -> None:
|
||||
"""Initialise la session Streamable HTTP auprès de o365-mirror-mcp."""
|
||||
self._check_rate_limit()
|
||||
payload = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "initialize",
|
||||
"params": {
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {},
|
||||
"clientInfo": {"name": "courrier", "version": "1.0"}
|
||||
}
|
||||
}
|
||||
headers = {
|
||||
"Authorization": f"Bearer {self._token}",
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json, text/event-stream"
|
||||
}
|
||||
req = urllib.request.Request(self.base_url, headers=headers, data=json.dumps(payload).encode("utf-8"))
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=15) as resp:
|
||||
sess_id = resp.headers.get("mcp-session-id")
|
||||
if not sess_id:
|
||||
# Certains serveurs mettent MCP-Session-Id ou mcp-session-id
|
||||
for k, v in resp.headers.items():
|
||||
if k.lower() == "mcp-session-id":
|
||||
sess_id = v
|
||||
break
|
||||
self.session_id = sess_id
|
||||
except Exception as e:
|
||||
raise ConnectionError(f"Échec initialisation session MCP miroir: {e}")
|
||||
|
||||
def call_tool(self, tool_name: str, arguments: Optional[Dict[str, Any]] = None, use_cache: bool = True) -> Any:
|
||||
arguments = arguments or {}
|
||||
cache_key = f"{tool_name}:{json.dumps(arguments, sort_keys=True)}"
|
||||
|
||||
now = time.time()
|
||||
if use_cache and cache_key in self._cache:
|
||||
ts, val = self._cache[cache_key]
|
||||
if now - ts < self.cache_ttl:
|
||||
return val
|
||||
|
||||
if not self.session_id:
|
||||
self._initialize_session()
|
||||
|
||||
self._check_rate_limit()
|
||||
self._request_counter += 1
|
||||
req_id = self._request_counter
|
||||
|
||||
payload = {
|
||||
"jsonrpc": "2.0",
|
||||
"id": req_id,
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": tool_name,
|
||||
"arguments": arguments
|
||||
}
|
||||
}
|
||||
|
||||
data_bytes = json.dumps(payload).encode("utf-8")
|
||||
req = urllib.request.Request(self.base_url, headers=self._get_headers(), data=data_bytes)
|
||||
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=20) as resp:
|
||||
raw_body = resp.read().decode("utf-8")
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 400 and not self.session_id:
|
||||
self._initialize_session()
|
||||
req = urllib.request.Request(self.base_url, headers=self._get_headers(), data=data_bytes)
|
||||
with urllib.request.urlopen(req, timeout=20) as resp:
|
||||
raw_body = resp.read().decode("utf-8")
|
||||
else:
|
||||
raise
|
||||
|
||||
# Parsing de la réponse (peut être SSE text/event-stream ou JSON direct)
|
||||
result_data = None
|
||||
for line in raw_body.split("\n"):
|
||||
line = line.strip()
|
||||
if line.startswith("data: "):
|
||||
try:
|
||||
parsed = json.loads(line[6:])
|
||||
result_data = parsed.get("result", {})
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
elif line.startswith("{") and line.endswith("}"):
|
||||
try:
|
||||
parsed = json.loads(line)
|
||||
if "result" in parsed:
|
||||
result_data = parsed["result"]
|
||||
break
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if not result_data:
|
||||
result_data = {}
|
||||
|
||||
# Les outils MCP renvoient content: [{"type": "text", "text": "..."}]
|
||||
content = result_data.get("content", [])
|
||||
final_output = None
|
||||
if content and isinstance(content, list) and "text" in content[0]:
|
||||
text_val = content[0]["text"]
|
||||
try:
|
||||
final_output = json.loads(text_val)
|
||||
except Exception:
|
||||
final_output = text_val
|
||||
else:
|
||||
final_output = result_data
|
||||
|
||||
if use_cache:
|
||||
self._cache[cache_key] = (now, final_output)
|
||||
|
||||
return final_output
|
||||
|
||||
def get_status(self) -> Dict[str, Any]:
|
||||
"""Consulte l'état du miroir (mirror_status)."""
|
||||
res = self.call_tool("mirror_status", {}, use_cache=False)
|
||||
return res if isinstance(res, dict) else {"raw": res}
|
||||
|
||||
def get_latest_mails(self, folder: str = "*", n: int = 20, inclure_exclus: bool = False) -> List[Dict[str, Any]]:
|
||||
"""Récupère les derniers emails (mirror_mail_latest)."""
|
||||
res = self.call_tool("mirror_mail_latest", {
|
||||
"folder": folder,
|
||||
"n": n,
|
||||
"inclure_exclus": inclure_exclus
|
||||
})
|
||||
if isinstance(res, dict) and "donnees" in res:
|
||||
return res["donnees"]
|
||||
return res if isinstance(res, list) else []
|
||||
|
||||
def get_table_mails(self, requete: str = "*", n: int = 20, inclure_exclus: bool = False) -> List[Dict[str, Any]]:
|
||||
"""Vue tabulaire des emails (mirror_mail_table)."""
|
||||
res = self.call_tool("mirror_mail_table", {
|
||||
"requete": requete,
|
||||
"n": n,
|
||||
"inclure_exclus": inclure_exclus
|
||||
})
|
||||
if isinstance(res, dict) and "donnees" in res:
|
||||
return res["donnees"]
|
||||
return res if isinstance(res, list) else []
|
||||
|
||||
def get_mails_by_tag(self, etiquette: str, periode: Optional[str] = None, n: int = 20, inclure_exclus: bool = False) -> List[Dict[str, Any]]:
|
||||
"""Recherche par étiquette du miroir (mirror_mail_by_tag)."""
|
||||
args: Dict[str, Any] = {"etiquette": etiquette, "n": n, "inclure_exclus": inclure_exclus}
|
||||
if periode:
|
||||
args["periode"] = periode
|
||||
res = self.call_tool("mirror_mail_by_tag", args)
|
||||
if isinstance(res, dict) and "donnees" in res:
|
||||
return res["donnees"]
|
||||
return res if isinstance(res, list) else []
|
||||
|
||||
def get_mail(self, message_id: str) -> Dict[str, Any]:
|
||||
"""Récupère le détail d'un email (mirror_mail_get)."""
|
||||
res = self.call_tool("mirror_mail_get", {"message_id": message_id})
|
||||
return res if isinstance(res, dict) else {"raw": res}
|
||||
|
||||
def get_mails_for_day(self, date_str: str, max_items: int = 50) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Récupère les emails correspondant à un jour donné (date_str: AAAA-MM-JJ).
|
||||
Utilise mirror_mail_latest avec un nombre suffisant de messages puis filtre par date.
|
||||
"""
|
||||
raw_mails = self.get_latest_mails(folder="*", n=max_items)
|
||||
filtered = []
|
||||
for m in raw_mails:
|
||||
# Vérifier la présence du message_id ou id
|
||||
mid = m.get("message_id") or m.get("id") or m.get("hash")
|
||||
d_mail = m.get("date") or m.get("date_reception") or m.get("date_heure") or ""
|
||||
if date_str in d_mail or not date_str:
|
||||
m_normalized = {
|
||||
"message_id": mid,
|
||||
"date": d_mail,
|
||||
"expediteur": m.get("from") or m.get("expediteur") or "",
|
||||
"objet": m.get("subject") or m.get("objet") or "",
|
||||
"etiquettes": m.get("tags") or m.get("etiquettes") or [],
|
||||
"apercu": m.get("snippet") or m.get("apercu") or ""
|
||||
}
|
||||
filtered.append(m_normalized)
|
||||
return filtered
|
||||
@@ -0,0 +1,27 @@
|
||||
version: '3.8'
|
||||
|
||||
services:
|
||||
courrier:
|
||||
container_name: courrier
|
||||
build: .
|
||||
image: courrier:latest
|
||||
restart: unless-stopped
|
||||
mem_limit: 256m
|
||||
mem_reservation: 64m
|
||||
cpus: 1.0
|
||||
ports:
|
||||
- "127.0.0.1:3120:8000"
|
||||
volumes:
|
||||
- ./data:/app/data
|
||||
- ./secrets:/app/secrets:ro
|
||||
environment:
|
||||
- DATA_DIR=/app/data
|
||||
- MIRROR_TOKEN_PATH=/app/secrets/mirror.token
|
||||
- MIRROR_URL=http://100.86.197.88:3125/mcp
|
||||
- TZ=Africa/Tunis
|
||||
networks:
|
||||
- courrier-net
|
||||
|
||||
networks:
|
||||
courrier-net:
|
||||
driver: bridge
|
||||
@@ -0,0 +1,6 @@
|
||||
fastapi>=0.110.0,<1.0.0
|
||||
uvicorn[standard]>=0.28.0,<1.0.0
|
||||
pydantic>=2.0.0,<3.0.0
|
||||
httpx>=0.27.0,<1.0.0
|
||||
pyjwt[crypto]>=2.8.0,<3.0.0
|
||||
pytest>=8.0.0,<9.0.0
|
||||
Whitespace-only changes.
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import tempfile
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
import jwt
|
||||
|
||||
# Configure test environment variables before importing app
|
||||
TEST_SECRET = "secret-test-courrier-tt-2026-11-059"
|
||||
os.environ["CF_TEST_SECRET"] = TEST_SECRET
|
||||
os.environ["CF_ACCESS_AUD"] = "courrier-test-aud"
|
||||
|
||||
from app.main import app
|
||||
from app.db import init_db
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db():
|
||||
"""Crée une base SQLite temporaire isolée pour chaque test."""
|
||||
with tempfile.NamedTemporaryFile(suffix=".db", delete=False) as f:
|
||||
db_path = f.name
|
||||
init_db(db_path)
|
||||
yield db_path
|
||||
if os.path.exists(db_path):
|
||||
os.remove(db_path)
|
||||
|
||||
@pytest.fixture
|
||||
def valid_jwt_token():
|
||||
"""Génère un jeton JWT valide pour les tests d'API."""
|
||||
payload = {
|
||||
"sub": "user@example.com",
|
||||
"email": "user@example.com",
|
||||
"aud": "courrier-test-aud",
|
||||
"iss": "https://test.cloudflareaccess.com",
|
||||
"exp": 9999999999
|
||||
}
|
||||
return jwt.encode(payload, TEST_SECRET, algorithm="HS256")
|
||||
|
||||
@pytest.fixture
|
||||
def client(valid_jwt_token, temp_db, monkeypatch):
|
||||
"""Client de test FastAPI avec injecteur d'en-tête Cloudflare Access et DB isolée."""
|
||||
monkeypatch.setattr("app.config.DB_PATH", temp_db)
|
||||
monkeypatch.setattr("app.db.DB_PATH", temp_db)
|
||||
init_db(temp_db)
|
||||
with TestClient(app) as c:
|
||||
c.headers.update({"Cf-Access-Jwt-Assertion": valid_jwt_token})
|
||||
yield c
|
||||
@@ -0,0 +1,89 @@
|
||||
import pytest
|
||||
|
||||
def test_api_categories_crud(client):
|
||||
# 1. Lister les catégories initiales
|
||||
r = client.get("/api/v1/categories")
|
||||
assert r.status_code == 200
|
||||
initial_count = len(r.json()["categories"])
|
||||
|
||||
# 2. Créer une catégorie
|
||||
r_create = client.post("/api/v1/categories", json={"name": "Nouvelle Catégorie"})
|
||||
assert r_create.status_code == 201
|
||||
cat_id = r_create.json()["id"]
|
||||
|
||||
# 3. Mettre à jour
|
||||
r_edit = client.put(f"/api/v1/categories/{cat_id}", json={"name": "Catégorie Renommée"})
|
||||
assert r_edit.status_code == 200
|
||||
assert r_edit.json()["name"] == "Catégorie Renommée"
|
||||
|
||||
# 4. Supprimer
|
||||
r_del = client.delete(f"/api/v1/categories/{cat_id}")
|
||||
assert r_del.status_code == 200
|
||||
|
||||
def test_api_regles_crud(client):
|
||||
# 1. Créer une règle
|
||||
payload = {
|
||||
"categorie": "Tunis",
|
||||
"expediteur": "fournisseur@test.tn",
|
||||
"mot_cle": "commande",
|
||||
"etiquette_miroir": "courses",
|
||||
"statut": "active"
|
||||
}
|
||||
r = client.post("/api/v1/regles", json=payload)
|
||||
assert r.status_code == 201
|
||||
rule_id = r.json()["id"]
|
||||
|
||||
# 2. Lister
|
||||
r_list = client.get("/api/v1/regles?statut=active")
|
||||
assert r_list.status_code == 200
|
||||
assert any(x["id"] == rule_id for x in r_list.json()["regles"])
|
||||
|
||||
# 3. Modifier
|
||||
r_edit = client.put(f"/api/v1/regles/{rule_id}", json={"mot_cle": "livraison"})
|
||||
assert r_edit.status_code == 200
|
||||
assert r_edit.json()["mot_cle"] == "livraison"
|
||||
|
||||
# 4. Supprimer
|
||||
r_del = client.delete(f"/api/v1/regles/{rule_id}")
|
||||
assert r_del.status_code == 200
|
||||
|
||||
def test_api_manual_classement_and_traite(client):
|
||||
mid = "<test-api-mail-01@domain.tn>"
|
||||
# Classement manuel
|
||||
r = client.post(
|
||||
f"/api/v1/classements/{mid}",
|
||||
json={
|
||||
"categorie": "Demande FB",
|
||||
"traite": False,
|
||||
"mail_info": {
|
||||
"expediteur": "client@fb.com",
|
||||
"objet": "Demande de contact",
|
||||
"date": "2026-11-17"
|
||||
}
|
||||
}
|
||||
)
|
||||
assert r.status_code == 200
|
||||
res = r.json()
|
||||
assert res["classement"]["source"] == "manuel"
|
||||
assert res["classement"]["categorie"] == "Demande FB"
|
||||
assert res["regle_proposee"] is not None
|
||||
assert res["regle_proposee"]["statut"] == "proposee"
|
||||
|
||||
# Modification de la case traité
|
||||
r_traite = client.patch(f"/api/v1/classements/{mid}/traite", json={"traite": True})
|
||||
assert r_traite.status_code == 200
|
||||
assert r_traite.json()["traite"] is True
|
||||
|
||||
def test_api_confirm_proposed_rule(client):
|
||||
# Créer règle proposée
|
||||
r_rule = client.post(
|
||||
"/api/v1/regles",
|
||||
json={"categorie": "Câbles", "mot_cle": "optique", "statut": "proposee"}
|
||||
)
|
||||
assert r_rule.status_code == 201
|
||||
rule_id = r_rule.json()["id"]
|
||||
|
||||
# Confirmer
|
||||
r_conf = client.post(f"/api/v1/regles/{rule_id}/confirmer")
|
||||
assert r_conf.status_code == 200
|
||||
assert r_conf.json()["regle"]["statut"] == "active"
|
||||
@@ -0,0 +1,32 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from app.main import app
|
||||
|
||||
def test_health_endpoint_no_auth_required():
|
||||
client = TestClient(app)
|
||||
resp = client.get("/health")
|
||||
assert resp.status_code == 200
|
||||
data = resp.json()
|
||||
assert data["status"] == "healthy"
|
||||
assert data["service"] == "courrier"
|
||||
assert "+01:00" in data["horodatage"]
|
||||
|
||||
def test_missing_cf_access_jwt_returns_401():
|
||||
client = TestClient(app)
|
||||
resp = client.get("/api/v1/categories")
|
||||
assert resp.status_code == 401
|
||||
assert "Cf-Access-Jwt-Assertion manquant" in resp.json()["detail"]
|
||||
|
||||
def test_invalid_cf_access_jwt_returns_401():
|
||||
client = TestClient(app)
|
||||
# Token malformé
|
||||
resp = client.get(
|
||||
"/api/v1/categories",
|
||||
headers={"Cf-Access-Jwt-Assertion": "ceci.n-est-pas.un-jwt-valide"}
|
||||
)
|
||||
assert resp.status_code == 401
|
||||
|
||||
def test_valid_cf_access_jwt_returns_200(client):
|
||||
resp = client.get("/api/v1/categories")
|
||||
assert resp.status_code == 200
|
||||
assert "categories" in resp.json()
|
||||
@@ -0,0 +1,134 @@
|
||||
import pytest
|
||||
from app.db import (
|
||||
add_regle,
|
||||
list_regles,
|
||||
get_classement,
|
||||
save_classement_regle,
|
||||
save_classement_manuel
|
||||
)
|
||||
from app.classifier import (
|
||||
match_mail_to_rules,
|
||||
classify_mail,
|
||||
manual_classify_and_propose,
|
||||
confirm_and_apply_rule,
|
||||
extract_keywords
|
||||
)
|
||||
|
||||
def test_extract_keywords():
|
||||
assert extract_keywords("Re: Demande de câbles pour le site") == "Demande câbles site"
|
||||
assert extract_keywords("TR: facture urgente") == "facture urgente"
|
||||
assert extract_keywords("") == ""
|
||||
|
||||
def test_match_mail_to_rules():
|
||||
rules = [
|
||||
{"id": 1, "etiquette_miroir": "courses", "categorie": "Tunis", "statut": "active"},
|
||||
{"id": 2, "expediteur": "fournisseur-cable.tn", "categorie": "Câbles", "statut": "active"},
|
||||
{"id": 3, "mot_cle": "facturation", "categorie": "CI", "statut": "active"}
|
||||
]
|
||||
|
||||
# Match par étiquette
|
||||
mail_tag = {"etiquettes": ["courses"], "expediteur": "autre@test.tn", "objet": "Rien"}
|
||||
assert match_mail_to_rules(mail_tag, rules) == "Tunis"
|
||||
|
||||
# Match par expéditeur
|
||||
mail_exp = {"etiquettes": [], "expediteur": "commercial@fournisseur-cable.tn", "objet": "Devis"}
|
||||
assert match_mail_to_rules(mail_exp, rules) == "Câbles"
|
||||
|
||||
# Match par mot-clé
|
||||
mail_mot = {"etiquettes": [], "expediteur": "inconnu@test.tn", "objet": "Note de facturation annuelle"}
|
||||
assert match_mail_to_rules(mail_mot, rules) == "CI"
|
||||
|
||||
# Défaut
|
||||
mail_def = {"etiquettes": [], "expediteur": "newsletter@spam.com", "objet": "Bonjour"}
|
||||
assert match_mail_to_rules(mail_def, rules) == "À classer"
|
||||
|
||||
def test_manual_correction_proposes_rule_unapplied(temp_db):
|
||||
"""
|
||||
Une correction manuelle propose une règle (même expéditeur et mêmes mots),
|
||||
qui ne s'applique qu'une fois confirmée.
|
||||
"""
|
||||
mid1 = "<mail-target-01@domain.tn>"
|
||||
mail1 = {
|
||||
"message_id": mid1,
|
||||
"expediteur": "client-important@domain.tn",
|
||||
"objet": "Demande de contrat spécifique",
|
||||
"date": "2026-11-17"
|
||||
}
|
||||
|
||||
# 1. Classement automatique initial par défaut
|
||||
c1 = classify_mail(mail1, db_path=temp_db)
|
||||
assert c1["categorie"] == "À classer"
|
||||
assert c1["source"] == "regle"
|
||||
|
||||
# 2. Correction manuelle par l'utilisateur -> proposition de règle
|
||||
c1_man, regle_prop = manual_classify_and_propose(
|
||||
message_id=mid1,
|
||||
new_category="Demande FB",
|
||||
mail_info=mail1,
|
||||
db_path=temp_db
|
||||
)
|
||||
assert c1_man["categorie"] == "Demande FB"
|
||||
assert c1_man["source"] == "manuel"
|
||||
assert regle_prop is not None
|
||||
assert regle_prop["statut"] == "proposee"
|
||||
assert regle_prop["categorie"] == "Demande FB"
|
||||
assert "client-important@domain.tn" in regle_prop["expediteur"]
|
||||
|
||||
# 3. Un second mail similaire arrive : la règle PROPOSÉE ne doit PAS s'appliquer !
|
||||
mid2 = "<mail-target-02@domain.tn>"
|
||||
mail2 = {
|
||||
"message_id": mid2,
|
||||
"expediteur": "client-important@domain.tn",
|
||||
"objet": "Autre question",
|
||||
"date": "2026-11-17"
|
||||
}
|
||||
c2 = classify_mail(mail2, db_path=temp_db)
|
||||
assert c2["categorie"] == "À classer", "Une règle proposée ne doit pas s'appliquer avant confirmation"
|
||||
|
||||
def test_confirm_proposed_rule_applies_to_non_manual_mails_only(temp_db):
|
||||
"""
|
||||
Une règle confirmée s'applique à tous les mails non-manuels, sans jamais toucher
|
||||
les classements manuels.
|
||||
"""
|
||||
# Mail 1: classé manuellement en 'Tunis'
|
||||
mid1 = "<mail-prop-01@domain.tn>"
|
||||
save_classement_manuel(
|
||||
message_id=mid1,
|
||||
categorie="Tunis",
|
||||
expediteur="partenaire@domain.tn",
|
||||
objet="Sujet projet",
|
||||
db_path=temp_db
|
||||
)
|
||||
|
||||
# Mail 2: classé automatiquement en 'À classer'
|
||||
mid2 = "<mail-prop-02@domain.tn>"
|
||||
save_classement_regle(
|
||||
message_id=mid2,
|
||||
categorie="À classer",
|
||||
expediteur="partenaire@domain.tn",
|
||||
objet="Sujet projet",
|
||||
db_path=temp_db
|
||||
)
|
||||
|
||||
# Création d'une règle proposée pour 'Demande FB'
|
||||
prop_rule = add_regle(
|
||||
categorie="Demande FB",
|
||||
expediteur="partenaire@domain.tn",
|
||||
statut="proposee",
|
||||
db_path=temp_db
|
||||
)
|
||||
|
||||
# Confirmation de la règle proposée
|
||||
res_confirm = confirm_and_apply_rule(prop_rule["id"], db_path=temp_db)
|
||||
assert res_confirm["statut"] == "active"
|
||||
|
||||
# Vérification:
|
||||
# Mail 2 (automatique) a été réévalué et requalifié en 'Demande FB'
|
||||
c2_after = get_classement(mid2, db_path=temp_db)
|
||||
assert c2_after["categorie"] == "Demande FB"
|
||||
assert c2_after["source"] == "regle"
|
||||
|
||||
# Mail 1 (manuel) est STRICTEMENT inchangé ('Tunis', 'manuel')
|
||||
c1_after = get_classement(mid1, db_path=temp_db)
|
||||
assert c1_after["categorie"] == "Tunis"
|
||||
assert c1_after["source"] == "manuel"
|
||||
@@ -0,0 +1,129 @@
|
||||
import pytest
|
||||
from app.db import (
|
||||
list_categories,
|
||||
add_category,
|
||||
update_category,
|
||||
delete_category,
|
||||
list_regles,
|
||||
add_regle,
|
||||
update_regle,
|
||||
delete_regle,
|
||||
save_classement_regle,
|
||||
save_classement_manuel,
|
||||
get_classement,
|
||||
set_traite
|
||||
)
|
||||
|
||||
def test_init_db_creates_tables_and_default_categories(temp_db):
|
||||
cats = list_categories(db_path=temp_db)
|
||||
cat_names = [c["name"] for c in cats]
|
||||
expected = ["Tunis", "Demande FB", "CI", "Câbles", "Sans objet", "À classer"]
|
||||
assert cat_names == expected, f"Catégories par défaut attendues: {expected}, obtenu: {cat_names}"
|
||||
|
||||
def test_crud_categories(temp_db):
|
||||
# Création
|
||||
new_cat = add_category("Factures", db_path=temp_db)
|
||||
assert new_cat["name"] == "Factures"
|
||||
assert new_cat["id"] > 0
|
||||
|
||||
# Modification
|
||||
updated = update_category(new_cat["id"], "Factures Fournisseurs", db_path=temp_db)
|
||||
assert updated["name"] == "Factures Fournisseurs"
|
||||
|
||||
# Liste
|
||||
cats = list_categories(db_path=temp_db)
|
||||
assert any(c["name"] == "Factures Fournisseurs" for c in cats)
|
||||
|
||||
# Suppression
|
||||
assert delete_category(new_cat["id"], db_path=temp_db) is True
|
||||
cats_after = list_categories(db_path=temp_db)
|
||||
assert not any(c["name"] == "Factures Fournisseurs" for c in cats_after)
|
||||
|
||||
def test_crud_regles(temp_db):
|
||||
# Création
|
||||
rule = add_regle(
|
||||
categorie="Tunis",
|
||||
expediteur="direction@entreprise.tn",
|
||||
mot_cle="mission",
|
||||
etiquette_miroir="tunis",
|
||||
statut="active",
|
||||
db_path=temp_db
|
||||
)
|
||||
assert rule["id"] > 0
|
||||
assert rule["categorie"] == "Tunis"
|
||||
assert rule["statut"] == "active"
|
||||
|
||||
# Modification
|
||||
updated = update_regle(rule["id"], categorie="Demande FB", db_path=temp_db)
|
||||
assert updated["categorie"] == "Demande FB"
|
||||
|
||||
# Liste
|
||||
rules = list_regles(statut="active", db_path=temp_db)
|
||||
assert len(rules) == 1
|
||||
assert rules[0]["categorie"] == "Demande FB"
|
||||
|
||||
# Suppression
|
||||
assert delete_regle(rule["id"], db_path=temp_db) is True
|
||||
assert len(list_regles(db_path=temp_db)) == 0
|
||||
|
||||
def test_manual_classification_persistence(temp_db):
|
||||
mid = "<msg-001@domain.com>"
|
||||
res = save_classement_manuel(
|
||||
message_id=mid,
|
||||
categorie="Tunis",
|
||||
traite=True,
|
||||
expediteur="expediteur@test.tn",
|
||||
objet="Objet test",
|
||||
date_mail="2026-11-17",
|
||||
db_path=temp_db
|
||||
)
|
||||
assert res["message_id"] == mid
|
||||
assert res["categorie"] == "Tunis"
|
||||
assert res["source"] == "manuel"
|
||||
assert res["traite"] is True
|
||||
assert "+01:00" in res["horodatage"], "L'horodatage doit comporter l'offset +01:00"
|
||||
|
||||
def test_manual_classification_never_overwritten_by_rule(temp_db):
|
||||
"""
|
||||
RÈGLE ABSOLUE : Un classement manuel n'est jamais écrasé par une règle.
|
||||
"""
|
||||
mid = "<msg-002@domain.com>"
|
||||
# 1. Pose d'un classement manuel
|
||||
save_classement_manuel(
|
||||
message_id=mid,
|
||||
categorie="Demande FB",
|
||||
traite=False,
|
||||
expediteur="contact@fb.com",
|
||||
objet="Message direct",
|
||||
db_path=temp_db
|
||||
)
|
||||
|
||||
# 2. Tentative d'écrasement par une règle automatique
|
||||
res_regle = save_classement_regle(
|
||||
message_id=mid,
|
||||
categorie="Sans objet",
|
||||
expediteur="contact@fb.com",
|
||||
objet="Message direct",
|
||||
db_path=temp_db
|
||||
)
|
||||
|
||||
# 3. Vérification : le classement doit impérativement rester 'Demande FB' et 'manuel'
|
||||
current = get_classement(mid, db_path=temp_db)
|
||||
assert current["categorie"] == "Demande FB"
|
||||
assert current["source"] == "manuel"
|
||||
assert res_regle["categorie"] == "Demande FB"
|
||||
assert res_regle["source"] == "manuel"
|
||||
|
||||
def test_set_traite_toggle(temp_db):
|
||||
mid = "<msg-003@domain.com>"
|
||||
save_classement_regle(message_id=mid, categorie="À classer", db_path=temp_db)
|
||||
c1 = get_classement(mid, db_path=temp_db)
|
||||
assert c1["traite"] is False
|
||||
|
||||
set_traite(mid, True, db_path=temp_db)
|
||||
c2 = get_classement(mid, db_path=temp_db)
|
||||
assert c2["traite"] is True
|
||||
|
||||
set_traite(mid, False, db_path=temp_db)
|
||||
c3 = get_classement(mid, db_path=temp_db)
|
||||
assert c3["traite"] is False
|
||||
@@ -0,0 +1,63 @@
|
||||
import pytest
|
||||
from app.mirror_client import MirrorClient
|
||||
from app.classifier import classify_mail
|
||||
from app.db import list_categories
|
||||
|
||||
def test_mirror_health_and_status():
|
||||
client = MirrorClient()
|
||||
status = client.get_status()
|
||||
donnees = status.get("donnees", {}) if isinstance(status, dict) else {}
|
||||
service = donnees.get("service") or status.get("service")
|
||||
statut = donnees.get("statut") or status.get("status")
|
||||
assert service == "o365-mirror-mcp"
|
||||
assert statut in ("actif", "healthy")
|
||||
assert status.get("stale") is False
|
||||
total_idx = donnees.get("index_meta", {}).get("total_indexed", {}).get("valeur") or status.get("total_mails_indexes")
|
||||
assert int(total_idx) > 3000
|
||||
|
||||
def test_mirror_rate_limiter_and_cache():
|
||||
client = MirrorClient(cache_ttl_seconds=30)
|
||||
# Premier appel : effectue l'appel réseau
|
||||
st1 = client.call_tool("mirror_status", {}, use_cache=True)
|
||||
calls_after_first = len(client._call_times)
|
||||
# Deuxième appel : doit provenir du cache (aucun nouvel appel réseau)
|
||||
st2 = client.call_tool("mirror_status", {}, use_cache=True)
|
||||
calls_after_second = len(client._call_times)
|
||||
assert st1 == st2
|
||||
assert calls_after_second == calls_after_first, "Le deuxième appel doit être servi depuis le cache"
|
||||
|
||||
def test_classify_10_real_mails(temp_db):
|
||||
"""
|
||||
Récupère 10 emails réels du miroir O365, applique le tri par règles,
|
||||
et valide les critères de conformité (0 écriture vers le miroir).
|
||||
AUCUN CONTENU DE MAIL N'EST AFFICHÉ OU JOURNALISÉ.
|
||||
"""
|
||||
client = MirrorClient()
|
||||
real_mails = client.get_latest_mails(folder="*", n=10)
|
||||
assert len(real_mails) >= 10, f"Nombre de mails reçus: {len(real_mails)}"
|
||||
|
||||
valid_cats = {c["name"] for c in list_categories(db_path=temp_db)}
|
||||
classified_count = 0
|
||||
|
||||
for mail in real_mails[:10]:
|
||||
mid = mail.get("message_id") or mail.get("id") or mail.get("hash")
|
||||
assert mid, "Chaque mail réel doit posséder un identifiant"
|
||||
|
||||
m_dict = {
|
||||
"message_id": mid,
|
||||
"expediteur": mail.get("from") or mail.get("expediteur") or "",
|
||||
"objet": mail.get("subject") or mail.get("objet") or "",
|
||||
"date": mail.get("date") or mail.get("date_reception") or "",
|
||||
"etiquettes": mail.get("tags") or mail.get("etiquettes") or [],
|
||||
"apercu": mail.get("snippet") or mail.get("apercu") or ""
|
||||
}
|
||||
|
||||
res = classify_mail(m_dict, db_path=temp_db)
|
||||
assert res["message_id"] == mid
|
||||
assert res["categorie"] in valid_cats
|
||||
assert res["source"] in ("regle", "manuel")
|
||||
assert "+01:00" in res["horodatage"]
|
||||
assert res["traite"] is False
|
||||
classified_count += 1
|
||||
|
||||
assert classified_count == 10, f"Attendu: 10 mails classés, obtenu: {classified_count}"
|
||||
Reference in new issue
Block a user