chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
60158935f4
commit
6519682885
15831 files changed
+3063428
No files matched your search
@@ -0,0 +1,55 @@
|
||||
from email.message import Message
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from python_http_client.exceptions import UnauthorizedError
|
||||
|
||||
from libs.sendgrid import SendGridClient
|
||||
|
||||
|
||||
def _mail(to: str = "user@example.com") -> dict[str, object]:
|
||||
return {"to": to, "subject": "Hi", "html": "<b>Hi</b>"}
|
||||
|
||||
|
||||
@patch("libs.sendgrid.sendgrid.SendGridAPIClient")
|
||||
def test_sendgrid_success(mock_client_cls: MagicMock) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_cls.return_value = mock_client
|
||||
# nested attribute access: client.mail.send.post
|
||||
mock_client.client.mail.send.post.return_value = MagicMock(status_code=202, body=b"", headers={})
|
||||
|
||||
sg = SendGridClient(sendgrid_api_key="key", _from="noreply@example.com")
|
||||
sg.send(_mail())
|
||||
|
||||
mock_client_cls.assert_called_once()
|
||||
mock_client.client.mail.send.post.assert_called_once()
|
||||
|
||||
|
||||
@patch("libs.sendgrid.sendgrid.SendGridAPIClient")
|
||||
def test_sendgrid_missing_to_raises(mock_client_cls: MagicMock) -> None:
|
||||
sg = SendGridClient(sendgrid_api_key="key", _from="noreply@example.com")
|
||||
with pytest.raises(ValueError, match="SendGridClient: Cannot send email: recipient address is missing\\."):
|
||||
sg.send(_mail(to=""))
|
||||
mock_client_cls.assert_not_called()
|
||||
|
||||
|
||||
@patch("libs.sendgrid.sendgrid.SendGridAPIClient")
|
||||
def test_sendgrid_auth_errors_reraise(mock_client_cls: MagicMock) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_cls.return_value = mock_client
|
||||
mock_client.client.mail.send.post.side_effect = UnauthorizedError(401, "Unauthorized", b"{}", Message())
|
||||
|
||||
sg = SendGridClient(sendgrid_api_key="key", _from="noreply@example.com")
|
||||
with pytest.raises(UnauthorizedError):
|
||||
sg.send(_mail())
|
||||
|
||||
|
||||
@patch("libs.sendgrid.sendgrid.SendGridAPIClient")
|
||||
def test_sendgrid_timeout_reraise(mock_client_cls: MagicMock) -> None:
|
||||
mock_client = MagicMock()
|
||||
mock_client_cls.return_value = mock_client
|
||||
mock_client.client.mail.send.post.side_effect = TimeoutError("timeout")
|
||||
|
||||
sg = SendGridClient(sendgrid_api_key="key", _from="noreply@example.com")
|
||||
with pytest.raises(TimeoutError):
|
||||
sg.send(_mail())
|
||||
Reference in new issue
Block a user