mock server, file and folder management

This commit is contained in:
2026-06-12 02:18:30 +02:00
parent b67c8abdc5
commit f3db5fc5cf
28 changed files with 3049 additions and 6 deletions

View File

@@ -8,6 +8,12 @@ from email.message import EmailMessage
from email.utils import formataddr
from app.mailer.campaign.models import SmtpConfig, TransportSecurity
from app.mailer.dev.mock_mailbox import (
consume_fail_next_smtp,
get_failures,
is_mock_smtp_host,
record_smtp_delivery,
)
class SmtpConfigurationError(ValueError):
@@ -98,6 +104,15 @@ def test_smtp_login(*, smtp_config: SmtpConfig) -> SmtpLoginTestResult:
"""
host, port = _require_smtp_config(smtp_config)
if is_mock_smtp_host(smtp_config.host):
host, port = _require_smtp_config(smtp_config)
return SmtpLoginTestResult(
host=host,
port=port,
security=smtp_config.security.value,
authenticated=bool(smtp_config.username and smtp_config.password),
)
smtp = _open_smtp(smtp_config)
try:
return SmtpLoginTestResult(
@@ -165,6 +180,37 @@ def send_email_message(
if not envelope_recipients:
raise SmtpConfigurationError("at least one SMTP envelope recipient is required")
if is_mock_smtp_host(smtp_config.host):
if consume_fail_next_smtp():
raise SmtpSendError("Mock SMTP configured to fail the next send")
failures = get_failures()
reject_text = str(failures.get("smtp_reject_recipients_containing") or "").strip().lower()
refused: dict[str, tuple[int, bytes]] = {}
accepted = list(envelope_recipients)
if reject_text:
refused = {
recipient: (550, b"mock recipient rejected")
for recipient in envelope_recipients
if reject_text in recipient.lower()
}
accepted = [recipient for recipient in envelope_recipients if recipient not in refused]
if not accepted:
raise SmtpSendError(f"all mock SMTP recipients were refused: {_decode_refused(refused)}")
record_smtp_delivery(
message,
envelope_from=envelope_from,
envelope_recipients=accepted,
smtp_host=smtp_config.host,
)
return SmtpSendResult(
host=host,
port=port,
security=smtp_config.security.value,
envelope_from=envelope_from,
envelope_recipients=list(envelope_recipients),
refused_recipients=_decode_refused(refused),
)
try:
with _open_smtp(smtp_config) as smtp:
refused = smtp.send_message(