from unittest import mock import pytest from fansifter_common.adapters.stripo.exceptions import StripoClientError from email_campaigns.automated.enums import AutomatedEmailType from email_campaigns.automated.handlers import UpdateAutomatedEmailContentHandler from email_campaigns.automated.models import ( AutomatedEmail, AutomatedEmailTrigger, SourceCampaign, ) from email_campaigns.emails.exceptions import ( EmailOptInButtonMissingError, EmailPrivacyFooterMissingError, ) from email_campaigns.emails.handlers import UpdateEmailContentRequest from tests.unit.types import CreateModel class TestUpdateAutomatedEmailContentHandler: @pytest.mark.db def test_update_content_without_source_campaigns_missing_privacy_footer( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail) with pytest.raises(EmailPrivacyFooterMissingError): handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html="
no footer", ) ) @pytest.mark.db def test_update_content_without_source_campaigns_opt_in_missing_button( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: automated_email = create_model(AutomatedEmail, type=AutomatedEmailType.OPT_IN) with pytest.raises(EmailOptInButtonMissingError): handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html='', ) ) @pytest.mark.db def test_update_content_with_source_campaigns_and_privacy_footer( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: source_campaign = create_model(SourceCampaign) automated_email = create_model( AutomatedEmail, source_campaign_connections=[ AutomatedEmailTrigger(source_campaign_id=source_campaign.id) ], ) html = '' handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html=html, ) ) assert automated_email.html_content == html @pytest.mark.db def test_update_content_with_source_campaigns_missing_privacy_footer( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: source_campaign = create_model(SourceCampaign) automated_email = create_model( AutomatedEmail, source_campaign_connections=[ AutomatedEmailTrigger(source_campaign_id=source_campaign.id) ], ) with pytest.raises(EmailPrivacyFooterMissingError): handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html="no footer", ) ) @pytest.mark.db def test_update_content_opt_in_with_source_campaigns_missing_button( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: source_campaign = create_model(SourceCampaign) automated_email = create_model( AutomatedEmail, type=AutomatedEmailType.OPT_IN, html_content=( '' '' ), source_campaign_connections=[ AutomatedEmailTrigger(source_campaign_id=source_campaign.id) ], ) with pytest.raises(EmailOptInButtonMissingError): handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html='', ) ) @pytest.mark.db def test_update_content_opt_in_with_source_campaigns_and_button( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, ) -> None: source_campaign = create_model(SourceCampaign) automated_email = create_model( AutomatedEmail, type=AutomatedEmailType.OPT_IN, source_campaign_connections=[ AutomatedEmailTrigger(source_campaign_id=source_campaign.id) ], ) html = ( '' '' ) handler.handle( UpdateEmailContentRequest( identity_id=identity_id, email_id=automated_email.id, html=html, ) ) assert automated_email.html_content == html class TestUpdateAutomatedEmailContentHandlerCompressedContent: @pytest.mark.db def test_update_content_writes_compressed_content( self, handler: UpdateAutomatedEmailContentHandler, create_model: CreateModel, identity_id: str, stripo_client_mock: mock.MagicMock, ) -> None: stripo_client_mock.compress.return_value = "