from unittest import mock import faker import pytest from anydi import Container from email_campaigns.adapters.features import AUDIENCE_ENABLE_STRIPO_PLUGIN_V2 from email_campaigns.automated.handlers import SendAutomatedTestEmailHandler from email_campaigns.automated.models import AutomatedEmail from email_campaigns.emails.exceptions import ( EmailNotFoundError, EmailPrivacyFooterMissingError, ) from email_campaigns.emails.handlers import SendTestEmailRequest from email_campaigns.emails.models import EmailDomain from email_campaigns.emails.services import EmailSendService from tests.unit.types import CreateModel, EnableFeatures class TestSendAutomatedTestEmailHandler: @pytest.mark.db def test_send_automated_test_email_not_found( self, handler: SendAutomatedTestEmailHandler, faker: faker.Faker, identity_id: str, ) -> None: with pytest.raises(EmailNotFoundError): handler.handle( SendTestEmailRequest( email_id=faker.pystr(), emails={faker.email(), faker.email()}, identity_id=identity_id, content=faker.pystr(), ) ) @pytest.mark.db def test_send_automated_test_email_without_privacy_footer( self, handler: SendAutomatedTestEmailHandler, faker: faker.Faker, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail, html_content="") with pytest.raises(EmailPrivacyFooterMissingError): handler.handle( SendTestEmailRequest( email_id=automated_email.id, emails={faker.email(), faker.email()}, identity_id=identity_id, content=faker.pystr(), ) ) @pytest.mark.db def test_send_automated_test_email_success( self, container: Container, handler: SendAutomatedTestEmailHandler, faker: faker.Faker, create_model: CreateModel, identity_id: str, preference_center_encrypter_mock: mock.MagicMock, ) -> None: preference_center_encrypter_mock.encrypt.return_value = b"token" email_domain = create_model(EmailDomain) automated_email = create_model( AutomatedEmail, sender_name=faker.name(), subject=faker.sentence(), email_domain=email_domain, email_username=faker.user_name(), html_content=( "" '' ), ) email_send_service_mock = mock.MagicMock(spec=EmailSendService) with container.override(EmailSendService, email_send_service_mock): handler.handle( SendTestEmailRequest( email_id=automated_email.id, emails={faker.email(), faker.email()}, identity_id=identity_id, content=None, ) ) assert email_send_service_mock.send_test_email.called @pytest.mark.db def test_send_test_with_unapplied_changes_fetches_from_stripo( self, container: Container, handler: SendAutomatedTestEmailHandler, faker: faker.Faker, create_model: CreateModel, identity_id: str, automated_stripo_client_v2_mock: mock.MagicMock, stripo_client_mock: mock.MagicMock, enable_features: EnableFeatures, ) -> None: email_domain = create_model(EmailDomain) original_html = ( "" '' ) original_css = "p{color:red}" automated_email = create_model( AutomatedEmail, sender_name=faker.name(), subject=faker.sentence(), email_domain=email_domain, email_username=faker.user_name(), html_content=original_html, css_content=original_css, has_unapplied_changes=True, ) new_html = ( "" '' ) new_css = "p{color:blue}" compressed = faker.pystr() automated_stripo_client_v2_mock.get_html_css.return_value = mock.Mock( html=new_html, css=new_css ) stripo_client_mock.compress.return_value = compressed email_send_service_mock = mock.MagicMock(spec=EmailSendService) with ( container.override(EmailSendService, email_send_service_mock), enable_features([AUDIENCE_ENABLE_STRIPO_PLUGIN_V2]), ): handler.handle( SendTestEmailRequest( email_id=automated_email.id, emails={faker.email()}, identity_id=identity_id, content=None, with_unapplied_changes=True, ) ) automated_stripo_client_v2_mock.get_html_css.assert_called_once_with( email_id=automated_email.id, user_id=identity_id ) stripo_client_mock.compress.assert_called_once_with(new_html, new_css) email_send_service_mock.send_test_email.assert_called_once() kwargs = email_send_service_mock.send_test_email.call_args.kwargs assert kwargs["content"] == compressed # model not mutated assert automated_email.html_content == original_html assert automated_email.css_content == original_css @pytest.mark.db def test_send_test_with_unapplied_changes_falls_back_when_ff_off( self, container: Container, handler: SendAutomatedTestEmailHandler, faker: faker.Faker, create_model: CreateModel, identity_id: str, automated_stripo_client_v2_mock: mock.MagicMock, preference_center_encrypter_mock: mock.MagicMock, ) -> None: preference_center_encrypter_mock.encrypt.return_value = b"token" email_domain = create_model(EmailDomain) automated_email = create_model( AutomatedEmail, sender_name=faker.name(), subject=faker.sentence(), email_domain=email_domain, email_username=faker.user_name(), html_content=( "" '' ), has_unapplied_changes=True, ) email_send_service_mock = mock.MagicMock(spec=EmailSendService) with container.override(EmailSendService, email_send_service_mock): handler.handle( SendTestEmailRequest( email_id=automated_email.id, emails={faker.email()}, identity_id=identity_id, content=None, with_unapplied_changes=True, ) ) automated_stripo_client_v2_mock.get_html_css.assert_not_called() email_send_service_mock.send_test_email.assert_called_once()