from unittest import mock import pytest from app.connectors.sendgrid import SendgridClient @pytest.fixture def sendgrid_client() -> SendgridClient: return SendgridClient(api_key="test_api_key") def test_send_email_with_text_only(sendgrid_client: SendgridClient) -> None: with mock.patch("httpx.post") as mock_post: mock_response = mock.MagicMock() mock_post.return_value = mock_response sendgrid_client.send_email( to_email="recipient@example.com", subject="Test Subject", text_content="Test text content", html_content="", from_email="sender@example.com", from_email_display_name="Sender Name", reply_to="reply@example.com", categories=["Fan Reply Forwarding"], custom_args={"send_type": "fan_reply_forwarding"}, ) mock_post.assert_called_once_with( "https://api.sendgrid.com/v3/mail/send", headers={"Authorization": "Bearer test_api_key"}, json={ "from": {"email": "sender@example.com", "name": "Sender Name"}, "personalizations": [{"to": [{"email": "recipient@example.com"}]}], "reply_to": {"email": "reply@example.com"}, "subject": "Test Subject", "content": [{"type": "text/plain", "value": "Test text content"}], "categories": ["Fan Reply Forwarding"], "custom_args": {"send_type": "fan_reply_forwarding"}, }, timeout=10, ) mock_response.raise_for_status.assert_called_once() def test_send_email_with_html_and_text(sendgrid_client: SendgridClient) -> None: with mock.patch("httpx.post") as mock_post: mock_response = mock.MagicMock() mock_post.return_value = mock_response sendgrid_client.send_email( to_email="recipient@example.com", subject="Test Subject", text_content="Test text content", html_content="Test HTML", from_email="sender@example.com", from_email_display_name="Sender Name", reply_to="reply@example.com", categories=["Fan Reply Forwarding"], custom_args={"send_type": "fan_reply_forwarding"}, ) mock_post.assert_called_once_with( "https://api.sendgrid.com/v3/mail/send", headers={"Authorization": "Bearer test_api_key"}, json={ "from": {"email": "sender@example.com", "name": "Sender Name"}, "personalizations": [{"to": [{"email": "recipient@example.com"}]}], "reply_to": {"email": "reply@example.com"}, "subject": "Test Subject", "content": [ {"type": "text/plain", "value": "Test text content"}, { "type": "text/html", "value": "Test HTML", }, ], "categories": ["Fan Reply Forwarding"], "custom_args": {"send_type": "fan_reply_forwarding"}, }, timeout=10, ) mock_response.raise_for_status.assert_called_once() def test_send_email_with_empty_text_and_html_only( sendgrid_client: SendgridClient, ) -> None: with mock.patch("httpx.post") as mock_post: mock_response = mock.MagicMock() mock_post.return_value = mock_response sendgrid_client.send_email( to_email="recipient@example.com", subject="Test Subject", text_content="", html_content="Test HTML only", from_email="sender@example.com", from_email_display_name="Sender Name", reply_to="reply@example.com", categories=["Fan Reply Forwarding"], custom_args={"send_type": "fan_reply_forwarding"}, ) mock_post.assert_called_once_with( "https://api.sendgrid.com/v3/mail/send", headers={"Authorization": "Bearer test_api_key"}, json={ "from": {"email": "sender@example.com", "name": "Sender Name"}, "personalizations": [{"to": [{"email": "recipient@example.com"}]}], "reply_to": {"email": "reply@example.com"}, "subject": "Test Subject", "content": [ { "type": "text/html", "value": "Test HTML only", } ], "categories": ["Fan Reply Forwarding"], "custom_args": {"send_type": "fan_reply_forwarding"}, }, timeout=10, ) mock_response.raise_for_status.assert_called_once() def test_send_email_with_empty_text_and_empty_html_uses_default_content( sendgrid_client: SendgridClient, ) -> None: with mock.patch("httpx.post") as mock_post: mock_response = mock.MagicMock() mock_post.return_value = mock_response sendgrid_client.send_email( to_email="recipient@example.com", subject="Test Subject", text_content="", html_content="", from_email="sender@example.com", from_email_display_name="Sender Name", reply_to="reply@example.com", categories=["Fan Reply Forwarding"], custom_args={"send_type": "fan_reply_forwarding"}, ) mock_post.assert_called_once_with( "https://api.sendgrid.com/v3/mail/send", headers={"Authorization": "Bearer test_api_key"}, json={ "from": {"email": "sender@example.com", "name": "Sender Name"}, "personalizations": [{"to": [{"email": "recipient@example.com"}]}], "reply_to": {"email": "reply@example.com"}, "subject": "Test Subject", "content": [{"type": "text/plain", "value": "No content provided"}], "categories": ["Fan Reply Forwarding"], "custom_args": {"send_type": "fan_reply_forwarding"}, }, timeout=10, ) mock_response.raise_for_status.assert_called_once()