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
1 changed files with 12 additions and 11 deletions

View File

@ -1,19 +1,20 @@
from unittest.mock import MagicMock, patch
import pytest
from libs.smtp import SMTPClient
from libs.mail import SMTPClient
def _mail() -> dict:
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):
mock_smtp = MagicMock()
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())
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()
@patch("libs.smtp.smtplib.SMTP")
@patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock):
mock_smtp = MagicMock()
mock_smtp_cls.return_value = mock_smtp
@ -31,7 +32,7 @@ def test_smtp_tls_opportunistic_success(mock_smtp_cls: MagicMock):
port=587,
username="user",
password="pass",
_from="noreply@example.com",
from_addr="noreply@example.com",
use_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()
@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):
# Cover SMTP_SSL branch and TimeoutError handling
mock_smtp = MagicMock()
@ -57,7 +58,7 @@ def test_smtp_tls_ssl_branch_and_timeout(mock_smtp_ssl_cls: MagicMock):
port=465,
username="",
password="",
_from="noreply@example.com",
from_addr="noreply@example.com",
use_tls=True,
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()
@patch("libs.smtp.smtplib.SMTP")
@patch("libs.mail.smtp_connection.smtplib.SMTP")
def test_smtp_generic_exception_propagates(mock_smtp_cls: MagicMock):
mock_smtp = MagicMock()
mock_smtp.sendmail.side_effect = RuntimeError("oops")
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):
client.send(_mail())
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):
# Ensure we hit the specific SMTPException except branch
import smtplib
@ -92,7 +93,7 @@ def test_smtp_smtplib_exception_in_login(mock_smtp_cls: MagicMock):
port=25,
username="user", # non-empty to trigger login
password="pass",
_from="noreply@example.com",
from_addr="noreply@example.com",
)
with pytest.raises(smtplib.SMTPException):
client.send(_mail())