-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement configurable default values for the subject and preheader
- Loading branch information
Showing
5 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
example_project/example/templates/templated_email/test_no_subject_preheader.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{% block content %} | ||
|
||
# Hello {{ name }}! | ||
|
||
This is a test message. | ||
{% endblock %} |
9 changes: 9 additions & 0 deletions
9
...le_project/example/templates/templated_email/test_subject_preheader_provided.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{% block subject %}Subject from Template{% endblock %} | ||
{% block preheader %}Preheader from Template{% endblock %} | ||
|
||
{% block content %} | ||
|
||
# Hello {{ name }}! | ||
|
||
This is a test message. | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
from django.core import mail | ||
from django.template import TemplateDoesNotExist | ||
from django.utils import translation | ||
from django.utils.translation import gettext as _ | ||
from templated_email import send_templated_mail | ||
|
||
from templated_email_md.backend import MarkdownTemplateBackend | ||
|
@@ -137,7 +138,7 @@ def test_fail_silently(): | |
# Should get a fallback response instead of an exception | ||
assert "Email template rendering failed" in response["html"] | ||
assert "Email template rendering failed" in response["plain"] | ||
assert response["subject"] == "No Subject" | ||
assert response["subject"] == "Hello!" | ||
|
||
|
||
def test_subject_from_context(backend): | ||
|
@@ -379,3 +380,79 @@ def test_remove_comments(): | |
assert "HTML Comment" not in cleaned_html | ||
assert "CSS Comment" not in cleaned_html | ||
assert "JavaScript Comment" not in cleaned_html | ||
|
||
|
||
def test_default_subject_and_preheader(): | ||
""" | ||
Test that the default subject and preheader are used when | ||
not provided in the template or context. | ||
""" | ||
# Send an email without subject or preheader in template or context | ||
send_templated_mail( | ||
template_name="test_no_subject_preheader", | ||
from_email="[email protected]", | ||
recipient_list=["[email protected]"], | ||
context={"name": "Test User"}, | ||
) | ||
|
||
assert len(mail.outbox) == 1 | ||
email = mail.outbox[0] | ||
|
||
# Default subject should be 'Hello!' as per the default in the backend | ||
assert email.subject == _("Hello!") | ||
# Preheader should be empty string by default | ||
# Since preheader may not be directly visible, check in the HTML content | ||
html_content = email.alternatives[0][0] | ||
assert "Hello!" in email.subject | ||
assert '<span class="preheader"' in html_content | ||
# Assuming the preheader is included even if empty, check that it's empty | ||
assert "></span>" in html_content | ||
|
||
|
||
def test_custom_default_subject_and_preheader(): | ||
""" | ||
Test that the custom default subject and preheader from settings | ||
are used when not provided in the template or context. | ||
""" | ||
settings.TEMPLATED_EMAIL_DEFAULT_SUBJECT = "Default Subject from Settings" | ||
settings.TEMPLATED_EMAIL_DEFAULT_PREHEADER = "Default Preheader from Settings" | ||
# Send an email without subject or preheader in template or context | ||
send_templated_mail( | ||
template_name="test_no_subject_preheader", | ||
from_email="[email protected]", | ||
recipient_list=["[email protected]"], | ||
context={"name": "Test User"}, | ||
) | ||
|
||
assert len(mail.outbox) == 1 | ||
email = mail.outbox[0] | ||
|
||
# Subject should be the custom default from settings | ||
assert email.subject == "Default Subject from Settings" | ||
# Preheader should be the custom default from settings | ||
html_content = email.alternatives[0][0] | ||
assert '<span class="preheader"' in html_content | ||
assert ">Default Preheader from Settings</span>" in html_content | ||
|
||
|
||
def test_subject_and_preheader_provided(): | ||
""" | ||
Test that when subject and preheader are provided in the template, | ||
they override the default values. | ||
""" | ||
# Send an email with subject and preheader in the template | ||
send_templated_mail( | ||
template_name="test_subject_preheader_provided", | ||
from_email="[email protected]", | ||
recipient_list=["[email protected]"], | ||
context={"name": "Test User"}, | ||
) | ||
|
||
assert len(mail.outbox) == 1 | ||
email = mail.outbox[0] | ||
|
||
# Subject and preheader should be as provided in the template | ||
assert email.subject == "Subject from Template" | ||
html_content = email.alternatives[0][0] | ||
assert '<span class="preheader"' in html_content | ||
assert ">Preheader from Template</span>" in html_content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters