From 1bdcaaf5d8ae087faf02cb61afee11b6bd5fa11e Mon Sep 17 00:00:00 2001 From: Colin Maudry Date: Thu, 2 Jul 2026 13:42:05 +0200 Subject: [PATCH] =?UTF-8?q?R=C3=A9activation=20de=20l'env=20MAIL=5FSUPPRES?= =?UTF-8?q?S=5FSEND?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .template.env | 3 +++ src/auth/mailer.py | 22 +++++++++++++++++++++- tests/auth/test_mailer.py | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.template.env b/.template.env index 1669fef..8172eac 100644 --- a/.template.env +++ b/.template.env @@ -45,6 +45,9 @@ BREVO_TEMPLATE_VERIFY_ID= # ID numérique du template "vérification BREVO_TEMPLATE_RESET_ID= # ID numérique du template "réinitialisation mot de passe" MAIL_FROM=noreply@colibre.fr # expéditeur (doit être un expéditeur vérifié dans Brevo) MAIL_FROM_NAME=colibre # nom d'expéditeur +# Active/désactive le mode sandbox Brevo (X-Sib-Sandbox: drop) indépendamment de DEVELOPMENT. +# Si non défini, suit DEVELOPMENT (sandbox actif par défaut en dev/tests). +MAIL_SUPPRESS_SEND= # Sauvegarde de users.sqlite vers un stockage S3-compatible S3_ENDPOINT_URL=https://s3.exemple.net diff --git a/src/auth/mailer.py b/src/auth/mailer.py index 15fd8a7..52ffbb6 100644 --- a/src/auth/mailer.py +++ b/src/auth/mailer.py @@ -37,9 +37,29 @@ def _template_id(env_var: str) -> int: return int(raw) +def _suppress_send() -> bool: + raw = os.getenv("MAIL_SUPPRESS_SEND") + if raw is not None: + return raw.lower() == "true" + return DEVELOPMENT + + def _send_template(template_id: int, recipient: str, params: dict) -> None: assert _client is not None, "Mailer non initialisé (init_mailer() non appelé)" - sandbox_headers = {"X-Sib-Sandbox": "drop"} if DEVELOPMENT else None + suppress = _suppress_send() + if suppress: + cause = ( + "MAIL_SUPPRESS_SEND=true" + if os.getenv("MAIL_SUPPRESS_SEND") is not None + else "DEVELOPMENT=true" + ) + logger.warning( + "Email en mode sandbox (%s) — non délivré à %s (template %s)", + cause, + recipient, + template_id, + ) + sandbox_headers = {"X-Sib-Sandbox": "drop"} if suppress else None try: _client.transactional_emails.send_transac_email( template_id=template_id, diff --git a/tests/auth/test_mailer.py b/tests/auth/test_mailer.py index 04db4e9..5cc43fc 100644 --- a/tests/auth/test_mailer.py +++ b/tests/auth/test_mailer.py @@ -40,6 +40,26 @@ def test_send_verification_email(fake_client): assert call["headers"] == {"X-Sib-Sandbox": "drop"} # DEVELOPMENT=true en tests +def test_mail_suppress_send_true_forces_sandbox_even_outside_development( + fake_client, monkeypatch +): + monkeypatch.setattr(mailer, "DEVELOPMENT", False) + monkeypatch.setenv("MAIL_SUPPRESS_SEND", "true") + mailer.send_verification_email("a@b.c", "TOKEN123") + call = fake_client.transactional_emails.calls[0] + assert call["headers"] == {"X-Sib-Sandbox": "drop"} + + +def test_mail_suppress_send_false_disables_sandbox_even_in_development( + fake_client, monkeypatch +): + monkeypatch.setattr(mailer, "DEVELOPMENT", True) + monkeypatch.setenv("MAIL_SUPPRESS_SEND", "false") + mailer.send_verification_email("a@b.c", "TOKEN123") + call = fake_client.transactional_emails.calls[0] + assert call["headers"] is None + + def test_send_reset_email(fake_client): mailer.send_reset_email("a@b.c", "RESET456") calls = fake_client.transactional_emails.calls