Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/notification/models/email.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import ClassVar
from typing import Any, ClassVar

from core.external_apis.smtp_email import EmailClient, email_client
from django.db import models
Expand All @@ -17,6 +17,12 @@ class EmailNotificationTemplate(NotificationTemplateBase):
class EmailNotificationHistorySentTo(NotificationHistorySentToBase):
history = models.ForeignKey("EmailNotificationHistory", on_delete=models.PROTECT, related_name="sent_to_list")

@property
def payload(self) -> dict[str, Any]:
rendered = self.render()
rendered["body"] = self.render_as_html()
return rendered


class EmailNotificationHistoryQuerySet(
NotificationHistoryQuerySet["EmailNotificationHistory", EmailNotificationTemplate],
Expand Down
24 changes: 24 additions & 0 deletions app/notification/test/history_send_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,30 @@ def test_history_send_parameters_uses_rendered_payload(system_user):
assert params["template_code"] == "render"


@pytest.mark.django_db
def test_email_payload_body_is_html_rendered(system_user):
# 이메일 발송 시 payload["body"]는 HTML 템플릿으로 렌더링된 결과여야 함.
tpl = EmailNotificationTemplate.objects.create(
code="html-body",
title="t",
sent_from="a@b.c",
data='{"title":"안녕 {{ name }}","body":"본문 {{ name }}"}',
created_by=system_user,
updated_by=system_user,
)
history = _create_history(tpl, context={"name": "길동"})
sent_to = history.sent_to_list.get()
payload = sent_to.payload

# title은 plain text
assert payload["title"] == "안녕 길동"
assert not payload["title"].strip().startswith("<")

# body는 HTML 렌더링 결과
assert payload["body"].strip().startswith("<")
assert "길동" in payload["body"]


@pytest.mark.django_db
def test_history_template_code_property_returns_template_code(email_template):
history = _create_history(email_template)
Expand Down
Loading