email_service.py was 428 lines covering three different concerns: SMTP transport, OTP/welcome rendering (tightly coupled — same brand template + theme), and digest rendering (a totally different shape of email, different layout, different copy cadence). The two halves changed at different cadences and made the file noisy to navigate. Extracted render_digest_email + _DIGEST_HTML_TEMPLATE + _strip_html_to_text to app/services/digest_email.py. SMTP transport and the OTP/welcome surface stay in email_service.py. Import sites updated: email_digest_job and test_email_render now import render_digest_email from digest_email. The OTP/welcome import sites (auth router, branding tests, test_email_service) are untouched. No behaviour change — pure relocation. Templates byte-identical. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
"""Unit tests for render_digest_email."""
|
|
from __future__ import annotations
|
|
|
|
from app.services.digest_email import render_digest_email
|
|
|
|
|
|
def test_daily_subject_and_bodies():
|
|
subj, text, html = render_digest_email(
|
|
kind="daily",
|
|
date_str="2026-05-25",
|
|
content_html="<p>Markets did stuff today.</p>",
|
|
unsubscribe_url="https://read.markets/email/unsubscribe?token=abc",
|
|
settings_url="https://read.markets/settings",
|
|
)
|
|
assert "Daily" in subj
|
|
assert "2026-05-25" in subj
|
|
assert "Markets did stuff today" in html
|
|
assert "abc" in html # unsubscribe link landed
|
|
assert "/settings" in html
|
|
# Plain-text fallback strips HTML.
|
|
assert "<p>" not in text
|
|
assert "Markets did stuff today" in text
|
|
|
|
|
|
def test_weekly_subject_says_recap():
|
|
subj, _, _ = render_digest_email(
|
|
kind="weekly",
|
|
date_str="2026-05-25",
|
|
content_html="<p>x</p>",
|
|
unsubscribe_url="https://x/u",
|
|
settings_url="https://x/s",
|
|
)
|
|
assert "Weekly" in subj
|
|
assert "recap" in subj.lower()
|
|
|
|
|
|
def test_invalid_kind_raises():
|
|
import pytest
|
|
with pytest.raises(ValueError):
|
|
render_digest_email(
|
|
kind="bogus", date_str="2026-05-25",
|
|
content_html="<p>x</p>",
|
|
unsubscribe_url="u", settings_url="s",
|
|
)
|