test(smtp): fix patch path

This commit is contained in:
-LAN- 2025-09-24 03:29:56 +08:00
parent f486d1bcee
commit bccd18b838
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF

View File

@ -1,19 +1,20 @@
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
import pytest import pytest
from libs.smtp import SMTPClient
from libs.mail import SMTPClient
def _mail() -> dict: def _mail() -> dict:
return {"to": "user@example.com", "subject": "Hi", "html": "<b>Hi</b>"} return {"to": "user@example.com", "subject": "Hi", "html": "<b>Hi</b>"}
@patch("libs.smtp.smtplib.SMTP") @patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_plain_success(mock_smtp_cls: MagicMock): def test_smtp_plain_success(mock_smtp_cls: MagicMock):
mock_smtp = MagicMock() mock_smtp = MagicMock()
mock_smtp_cls.return_value = mock_smtp mock_smtp_cls.return_value = mock_smtp
client = SMTPClient(server="smtp.example.com", port=25, username="", password="", _from="noreply@example.com") client = SMTPClient(server="smtp.example.com", port=25, username="", password="", from_addr="noreply@example.com")
client.send(_mail()) client.send(_mail())
mock_smtp_cls.assert_called_once_with("smtp.example.com", 25, timeout=10) mock_smtp_cls.assert_called_once_with("smtp.example.com", 25, timeout=10)
@ -21,7 +22,7 @@ def test_smtp_plain_success(mock_smtp_cls: MagicMock):
mock_smtp.quit.assert_called_once() mock_smtp.quit.assert_called_once()
@patch("libs.smtp.smtplib.SMTP") @patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock): def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock):
mock_smtp = MagicMock() mock_smtp = MagicMock()
mock_smtp_cls.return_value = mock_smtp mock_smtp_cls.return_value = mock_smtp
@ -31,7 +32,7 @@ def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock):
port=587, port=587,
username="user", username="user",
password="pass", password="pass",
_from="noreply@example.com", from_addr="noreply@example.com",
use_tls=True, use_tls=True,
opportunistic_tls=True, opportunistic_tls=True,
) )
@ -45,7 +46,7 @@ def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock):
mock_smtp.quit.assert_called_once() mock_smtp.quit.assert_called_once()
@patch("libs.smtp.smtplib.SMTP_SSL") @patch("libs.mail.smtp_connection.smtplib.SMTP_SSL")
def test_smtp_tls_ssl_branch_and_timeout(mock_smtp_ssl_cls: MagicMock): def test_smtp_tls_ssl_branch_and_timeout(mock_smtp_ssl_cls: MagicMock):
# Cover SMTP_SSL branch and TimeoutError handling # Cover SMTP_SSL branch and TimeoutError handling
mock_smtp = MagicMock() mock_smtp = MagicMock()
@ -57,7 +58,7 @@ def test_smtp_tls_ssl_branch_and_timeout(mock_smtp_ssl_cls: MagicMock):
port=465, port=465,
username="", username="",
password="", password="",
_from="noreply@example.com", from_addr="noreply@example.com",
use_tls=True, use_tls=True,
opportunistic_tls=False, opportunistic_tls=False,
) )
@ -66,19 +67,19 @@ def test_smtp_tls_ssl_branch_and_timeout(mock_smtp_ssl_cls: MagicMock):
mock_smtp.quit.assert_called_once() mock_smtp.quit.assert_called_once()
@patch("libs.smtp.smtplib.SMTP") @patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_generic_exception_propagates(mock_smtp_cls: MagicMock): def test_smtp_generic_exception_propagates(mock_smtp_cls: MagicMock):
mock_smtp = MagicMock() mock_smtp = MagicMock()
mock_smtp.sendmail.side_effect = RuntimeError("oops") mock_smtp.sendmail.side_effect = RuntimeError("oops")
mock_smtp_cls.return_value = mock_smtp mock_smtp_cls.return_value = mock_smtp
client = SMTPClient(server="smtp.example.com", port=25, username="", password="", _from="noreply@example.com") client = SMTPClient(server="smtp.example.com", port=25, username="", password="", from_addr="noreply@example.com")
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
client.send(_mail()) client.send(_mail())
mock_smtp.quit.assert_called_once() mock_smtp.quit.assert_called_once()
@patch("libs.smtp.smtplib.SMTP") @patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_smtplib_exception_in_login(mock_smtp_cls: MagicMock): def test_smtp_smtplib_exception_in_login(mock_smtp_cls: MagicMock):
# Ensure we hit the specific SMTPException except branch # Ensure we hit the specific SMTPException except branch
import smtplib import smtplib
@ -92,7 +93,7 @@ def test_smtp_smtplib_exception_in_login(mock_smtp_cls: MagicMock):
port=25, port=25,
username="user", # non-empty to trigger login username="user", # non-empty to trigger login
password="pass", password="pass",
_from="noreply@example.com", from_addr="noreply@example.com",
) )
with pytest.raises(smtplib.SMTPException): with pytest.raises(smtplib.SMTPException):
client.send(_mail()) client.send(_mail())