from unittest import mock
import pytest
from email_campaigns.adapters.features import AUDIENCE_ENABLE_STRIPO_PLUGIN_V2
from email_campaigns.automated.handlers import GetAutomatedEmailPreviewHandler
from email_campaigns.automated.models import AutomatedEmail
from email_campaigns.emails.handlers import GetEmailPreviewRequest
from tests.unit.types import CreateModel, EnableFeatures
class TestGetAutomatedEmailPreviewHandler:
@pytest.mark.db
def test_preview_with_unapplied_changes_fetches_from_stripo(
self,
handler: GetAutomatedEmailPreviewHandler,
create_model: CreateModel,
identity_id: str,
automated_stripo_client_v2_mock: mock.MagicMock,
enable_features: EnableFeatures,
) -> None:
automated_email = create_model(
AutomatedEmail,
html_content="",
css_content="old",
has_unapplied_changes=True,
)
new_html = ""
new_css = "new"
automated_stripo_client_v2_mock.get_html_css.return_value = mock.Mock(
html=new_html, css=new_css
)
with enable_features([AUDIENCE_ENABLE_STRIPO_PLUGIN_V2]):
preview = handler.handle(
GetEmailPreviewRequest(
identity_id=identity_id,
email_id=automated_email.id,
with_unapplied_changes=True,
)
)
assert preview.html == new_html
assert preview.css == new_css
automated_stripo_client_v2_mock.get_html_css.assert_called_once_with(
email_id=automated_email.id, user_id=identity_id
)
# model is not mutated
assert automated_email.html_content == ""
assert automated_email.css_content == "old"
@pytest.mark.db
def test_preview_with_unapplied_changes_applies_substitutions(
self,
handler: GetAutomatedEmailPreviewHandler,
create_model: CreateModel,
identity_id: str,
automated_stripo_client_v2_mock: mock.MagicMock,
sendgrid_client_mock: mock.MagicMock,
enable_features: EnableFeatures,
) -> None:
automated_email = create_model(
AutomatedEmail,
html_content="",
css_content="old",
has_unapplied_changes=True,
)
sendgrid_client_mock.get_user_privacy_footer_block.return_value = ""
automated_stripo_client_v2_mock.get_html_css.return_value = mock.Mock(
html="
Hi -firstName-!
", css="new"
)
with enable_features([AUDIENCE_ENABLE_STRIPO_PLUGIN_V2]):
preview = handler.handle(
GetEmailPreviewRequest(
identity_id=identity_id,
email_id=automated_email.id,
with_unapplied_changes=True,
apply_substitutions=True,
)
)
assert preview.html is not None
assert "-firstName-" not in preview.html
assert "Hi Fan!" in preview.html
assert preview.css == "new"
@pytest.mark.db
def test_preview_without_flag_reads_from_model(
self,
handler: GetAutomatedEmailPreviewHandler,
create_model: CreateModel,
identity_id: str,
automated_stripo_client_v2_mock: mock.MagicMock,
) -> None:
automated_email = create_model(
AutomatedEmail, html_content="", css_content="stored"
)
preview = handler.handle(
GetEmailPreviewRequest(
identity_id=identity_id,
email_id=automated_email.id,
)
)
assert preview.html == ""
assert preview.css == "stored"
automated_stripo_client_v2_mock.get_html_css.assert_not_called()
@pytest.mark.db
def test_preview_with_unapplied_changes_falls_back_when_ff_off(
self,
handler: GetAutomatedEmailPreviewHandler,
create_model: CreateModel,
identity_id: str,
automated_stripo_client_v2_mock: mock.MagicMock,
) -> None:
automated_email = create_model(
AutomatedEmail,
html_content="",
css_content="stored",
has_unapplied_changes=True,
)
preview = handler.handle(
GetEmailPreviewRequest(
identity_id=identity_id,
email_id=automated_email.id,
with_unapplied_changes=True,
)
)
assert preview.html == ""
assert preview.css == "stored"
automated_stripo_client_v2_mock.get_html_css.assert_not_called()