def build_email(self, transactional: TransactionalSchema) -> EmailSchema: rendered_template = self.template_service.render_template( Transactional(transactional.transactional_type).template, transactional.transactional_data, ) return EmailSchema( subject=self.template_service.get_title(rendered_template), content=rendered_template, **transactional.dict(by_alias=True))
def test_content_type_field_has_default_value(email_message_dict): assert isinstance(EmailSchema(**email_message_dict).content_type, str)
def test_content_field_is_required(email_message_dict): del email_message_dict["content"] with raises(ValidationError): EmailSchema(**email_message_dict)
def test_subject_field_max_length_is_78(email_message_dict): email_message_dict["subject"] = 79 * "a" with raises(ValidationError): EmailSchema(**email_message_dict)
def test_from_field_must_be_email_address(email_message_dict): for address in invalid_email_addresses: email_message_dict["from"] = address with raises(ValidationError): EmailSchema(**email_message_dict)
def test_from_field_is_optional(email_message_dict): del email_message_dict["from"] assert EmailSchema(**email_message_dict).from_email is None
def send_email(message: EmailSchema, producer=Depends(tasks_producer)): producer.send_task(Task.SEND_EMAIL, args=[message.dict(by_alias=True)])
from pytest import raises from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import Mail, Email, To, Content from app.core.adapters import SendgridAdapter from app.core.schemas import EmailSchema from app.settings import Settings settings = Settings( DEFAULT_EMAIL_ADDRESS="*****@*****.**", SENDGRID_API_KEY="some_api_key", ) message = EmailSchema( **{ "from": "*****@*****.**", "to": "*****@*****.**", "content": "content", "subject": "subject", } ) @patch("sendgrid.SendGridAPIClient.__init__", return_value=None) def test_sendgrid_client_gets_initialized_with_api_key(mock_client_init): SendgridAdapter(settings=settings) mock_client_init.assert_called_once_with(api_key=settings.SENDGRID_API_KEY) @patch("sendgrid.SendGridAPIClient.send") def test_adapter_send_calls_client_send(mock_client_send): adapter = SendgridAdapter(settings=settings) adapter.send(message)
def test_content_type_field_has_default_value(email): assert EmailSchema(**email).content_type == "text/plain"
def test_subject_field_is_required(email): del email["subject"] with raises(ValidationError): EmailSchema(**email)
def test_from_field_has_default_value(email, monkeypatch): del email["from"] assert EmailSchema(**email).from_ == settings.DEFAULT_EMAIL_ADDRESS
def test_to_field_must_be_email_address(email): for address in invalid_email_addresses: email["to"] = address with raises(ValidationError): EmailSchema(**email)
def test_to_field_is_required(email): del email["to"] with raises(ValidationError): EmailSchema(**email)
def email_message(email_message_dict): return EmailSchema(**email_message_dict)
HTTP_422_UNPROCESSABLE_ENTITY, ) from app.api import api from app.core.adapters import SendgridAdapter from app.core.schemas import EmailSchema message_dict = { "from": "*****@*****.**", "to": "*****@*****.**", "subject": "subject", "content": "content", "content_type": "text/html", } message = EmailSchema(**message_dict) @fixture(scope="function") def send_response(client, settings): SendgridAdapter.send = MagicMock() yield client.post("/emails", json=message_dict, headers={"x-api-key": settings.API_KEY}) SendgridAdapter.send.assert_called_once_with(message) def test_send_email_endpoint_should_accept_post(send_response): assert send_response.status_code != HTTP_405_METHOD_NOT_ALLOWED