"""Testy SMTP odesílače (STARTTLS, SMTP_SSL, chyby bez úniku tajemství).""" from __future__ import annotations import logging import smtplib from email.message import EmailMessage from typing import Any from unittest.mock import MagicMock, patch import pytest from poznavacky.mailer import MailDeliveryError, SmtpMailer def test_smtp_mailer_starttls_and_auth() -> None: fake_client = MagicMock() fake_cm = MagicMock() fake_cm.__enter__.return_value = fake_client with patch("poznavacky.mailer.smtplib.SMTP", return_value=fake_cm) as cls: mailer = SmtpMailer( host="mail.example.test", port=587, user="noreply@example.test", password="tajne-smtp-heslo", sender="Poznavacky ", use_tls=True, ) mailer.send_email( to="zak@example.test", subject="Obnova hesla", body="Odkaz: https://poznavacky.example.test/obnova-hesla/tok123", ) cls.assert_called_once() assert cls.call_args.args == ("mail.example.test", 587) fake_client.starttls.assert_called_once() fake_client.login.assert_called_once_with( "noreply@example.test", "tajne-smtp-heslo", ) fake_client.send_message.assert_called_once() sent_msg: EmailMessage = fake_client.send_message.call_args.args[0] assert sent_msg["From"] == "Poznavacky " assert sent_msg["To"] == "zak@example.test" assert sent_msg["Subject"] == "Obnova hesla" assert "tok123" in sent_msg.get_content() def test_smtp_mailer_uses_smtp_ssl_on_port_465() -> None: fake_client = MagicMock() fake_cm = MagicMock() fake_cm.__enter__.return_value = fake_client with patch( "poznavacky.mailer.smtplib.SMTP_SSL", return_value=fake_cm, ) as ssl_cls: mailer = SmtpMailer( host="mail.example.test", port=465, user="", password="", sender="noreply@example.test", use_tls=True, ) mailer.send_email( to="zak@example.test", subject="Test SSL", body="Obsah zprávy", ) ssl_cls.assert_called_once() fake_client.starttls.assert_not_called() fake_client.login.assert_not_called() fake_client.send_message.assert_called_once() def test_smtp_mailer_missing_host_raises_without_secrets( caplog: pytest.LogCaptureFixture, ) -> None: mailer = SmtpMailer( host="", port=587, user="u@example.test", password="SUPER-SECRET-SMTP-PASSWORD", sender="noreply@example.test", ) with caplog.at_level(logging.WARNING): with pytest.raises(MailDeliveryError): mailer.send_email( to="zak@example.test", subject="Obnova", body="TAJNY-RESET-TOKEN-XYZ", ) assert "SUPER-SECRET-SMTP-PASSWORD" not in caplog.text assert "TAJNY-RESET-TOKEN-XYZ" not in caplog.text def test_smtp_mailer_failure_logs_safely_without_password_or_token( caplog: pytest.LogCaptureFixture, ) -> None: secret_pass = "VELMI-TAJNE-HESLO-SMTP" secret_token = "SUROVY-TOKEN-123456" def _boom(*args: Any, **kwargs: Any) -> Any: raise smtplib.SMTPAuthenticationError( 535, f"Auth failed for {secret_pass}".encode(), ) with ( patch("poznavacky.mailer.smtplib.SMTP", side_effect=_boom), caplog.at_level(logging.ERROR), ): mailer = SmtpMailer( host="mail.example.test", port=587, user="u@example.test", password=secret_pass, sender="noreply@example.test", ) with pytest.raises(MailDeliveryError) as exc_info: mailer.send_email( to="zak@example.test", subject="Obnova", body=f"https://example.test/obnova-hesla/{secret_token}", ) assert secret_pass not in caplog.text assert secret_token not in caplog.text assert secret_pass not in str(exc_info.value) assert secret_token not in str(exc_info.value)