fix: embed uploaded logo in emails
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from types import SimpleNamespace
|
||||
|
||||
from bot.services.email_auth_service import EmailAuthService
|
||||
from bot.services.email_templates import EmailInlineImage
|
||||
|
||||
|
||||
def _settings():
|
||||
return SimpleNamespace(
|
||||
SMTP_FROM_NAME="Mini Shop",
|
||||
SMTP_FROM_EMAIL="noreply@example.com",
|
||||
WEBAPP_TITLE="Mini Shop",
|
||||
)
|
||||
|
||||
|
||||
def test_build_email_message_attaches_inline_images_to_html_part():
|
||||
service = EmailAuthService(_settings())
|
||||
|
||||
message = service._build_email_message(
|
||||
email="user@example.com",
|
||||
subject="Login code",
|
||||
body="Your code: 123456",
|
||||
html_body='<img src="cid:webapp-logo" alt="">',
|
||||
inline_images=(
|
||||
EmailInlineImage(
|
||||
content_id="webapp-logo",
|
||||
content_type="image/png",
|
||||
data=b"\x89PNG\r\n\x1a\nlogo",
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
related_part = message.get_body(("related",))
|
||||
assert related_part is not None
|
||||
html_part = related_part.get_body(("html",))
|
||||
assert html_part is not None
|
||||
related_images = [part for part in related_part.iter_attachments()]
|
||||
|
||||
assert len(related_images) == 1
|
||||
assert related_images[0].get_content_type() == "image/png"
|
||||
assert related_images[0]["Content-ID"] == "<webapp-logo>"
|
||||
assert related_images[0].get_content_disposition() == "inline"
|
||||
@@ -6,6 +6,7 @@ from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from bot.middlewares.i18n import JsonI18n
|
||||
from bot.services import email_templates as email_templates_module
|
||||
from bot.services.email_templates import (
|
||||
EmailContent,
|
||||
render_account_merged,
|
||||
@@ -255,6 +256,49 @@ def test_all_email_template_variants_render_without_raw_locale_keys():
|
||||
_assert_content_is_localized(content, language)
|
||||
|
||||
|
||||
def test_uploaded_webapp_logo_is_embedded_inline(tmp_path, monkeypatch):
|
||||
uploads_dir = tmp_path / "uploads"
|
||||
uploads_dir.mkdir()
|
||||
filename = "logo-1111111111111111.png"
|
||||
logo_body = b"\x89PNG\r\n\x1a\nlogo"
|
||||
(uploads_dir / filename).write_bytes(logo_body)
|
||||
monkeypatch.setattr(email_templates_module, "_WEBAPP_UPLOADED_LOGO_DIR", uploads_dir)
|
||||
|
||||
settings = _settings()
|
||||
settings.WEBAPP_LOGO_URL = f"/webapp-uploaded-logo/{filename}"
|
||||
|
||||
content = render_login_code(
|
||||
settings,
|
||||
code="123456",
|
||||
language_code="en",
|
||||
purpose="login",
|
||||
i18n=_i18n("en"),
|
||||
)
|
||||
|
||||
assert 'src="cid:webapp-logo"' in content.html
|
||||
assert len(content.inline_images) == 1
|
||||
inline_logo = content.inline_images[0]
|
||||
assert inline_logo.content_id == "webapp-logo"
|
||||
assert inline_logo.content_type == "image/png"
|
||||
assert inline_logo.data == logo_body
|
||||
|
||||
|
||||
def test_public_https_webapp_logo_remains_external():
|
||||
settings = _settings()
|
||||
settings.WEBAPP_LOGO_URL = "https://cdn.example.com/logo.png"
|
||||
|
||||
content = render_login_code(
|
||||
settings,
|
||||
code="123456",
|
||||
language_code="en",
|
||||
purpose="login",
|
||||
i18n=_i18n("en"),
|
||||
)
|
||||
|
||||
assert 'src="https://cdn.example.com/logo.png"' in content.html
|
||||
assert content.inline_images == ()
|
||||
|
||||
|
||||
def test_support_email_templates_use_russian_copy_for_russian_recipients():
|
||||
subjects = [content.subject for content in _all_rendered_email_variants("ru")[-4:]]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user