-
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.
- Loading branch information
Showing
3 changed files
with
158 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""Pytest fixtures for templated_email_md tests.""" | ||
|
||
import pytest | ||
|
||
from templated_email_md.backend import MarkdownTemplateBackend | ||
|
||
|
||
@pytest.fixture | ||
def backend(): | ||
"""Create a MarkdownTemplateBackend instance.""" | ||
return MarkdownTemplateBackend() |
10 changes: 10 additions & 0 deletions
10
example_project/example/templates/templated_email/test_message.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,10 @@ | ||
{% block subject %}Test Email{% endblock %} | ||
|
||
# Hello {{ name }}! | ||
|
||
This is a **bold** test. | ||
|
||
1. Item one | ||
2. Item two | ||
|
||
[A link](http://example.com) |
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 |
---|---|---|
@@ -1,7 +1,13 @@ | ||
"""Test cases for the django-templated-email-md package.""" | ||
|
||
from django.apps import apps | ||
import pytest | ||
from django.conf import settings | ||
from django.core import mail | ||
from django.template import TemplateDoesNotExist | ||
from django.test import override_settings | ||
from templated_email import send_templated_mail | ||
|
||
from templated_email_md.backend import MarkdownTemplateBackend | ||
|
||
|
||
def test_succeeds() -> None: | ||
|
@@ -14,6 +20,133 @@ def test_settings() -> None: | |
assert settings.USE_TZ is True | ||
|
||
|
||
def test_apps() -> None: | ||
"""Test that the app is configured in the Django project.""" | ||
assert "templated_email_md" in apps.get_app_config("templated_email_md").name | ||
@pytest.fixture | ||
def backend(): | ||
"""Create a MarkdownTemplateBackend instance.""" | ||
return MarkdownTemplateBackend() | ||
|
||
|
||
def test_markdown_conversion(backend): | ||
"""Test conversion of Markdown to HTML.""" | ||
response = backend._render_email("test_message", {"name": "Test User"}) | ||
|
||
assert "Hello Test User!" in response["html"] | ||
assert "<h1" in response["html"] | ||
assert "<strong" in response["html"] | ||
assert "<ol" in response["html"] | ||
assert 'href="http://example.com"' in response["html"] | ||
|
||
def test_plain_text_generation(backend): | ||
"""Test generation of plain text version.""" | ||
response = backend._render_email("test_message", {"name": "Test User"}) | ||
|
||
assert "Hello Test User!" in response["plain"] | ||
assert "**" not in response["plain"] # Markdown syntax should be converted | ||
# assert "[A link](http://example.com)" not in response["plain"] # May need to further adjust html2text configs | ||
assert "http://example.com" in response["plain"] | ||
|
||
|
||
def test_css_inlining(backend): | ||
"""Test that CSS styles are properly inlined.""" | ||
response = backend._render_email("test_message", {"name": "Test User"}) | ||
# Check that any CSS from markdown_base.html has been inlined | ||
assert 'style="' in response["html"] | ||
|
||
|
||
def test_template_inheritance(backend): | ||
"""Test that template inheritance works correctly.""" | ||
with pytest.raises(TemplateDoesNotExist): | ||
# Should raise if referenced template doesn't exist | ||
backend._render_email("non_existent_template", {}) | ||
|
||
|
||
@override_settings(TEMPLATED_EMAIL_BACKEND='templated_email_md.backend.MarkdownTemplateBackend') | ||
def test_send_templated_mail(): | ||
"""Test sending an email using the backend.""" | ||
send_templated_mail( | ||
template_name="test_message", | ||
from_email="[email protected]", | ||
recipient_list=["[email protected]"], | ||
context={"name": "Test User"}, | ||
) | ||
|
||
# Check that the email was sent | ||
assert len(mail.outbox) == 1 | ||
assert mail.outbox[0].subject == "Test Email" | ||
assert "Hello Test User!" in mail.outbox[0].body | ||
|
||
|
||
def test_missing_subject_block(backend): | ||
"""Test handling of missing subject block.""" | ||
with pytest.raises(TemplateDoesNotExist): | ||
# This will now raise TemplateDoesNotExist since we don't support inline templates | ||
backend._render_email("non_existent", {}) | ||
|
||
|
||
@pytest.mark.parametrize("context,expected", [ | ||
({"name": "Test"}, "Test"), | ||
({"name": ""}, ""), | ||
({}, ""), # Missing context variable | ||
]) | ||
def test_context_handling(backend, context, expected): | ||
"""Test handling of different context scenarios.""" | ||
response = backend._render_email("test_message", context) | ||
if expected: | ||
assert f"Hello {expected}!" in response["html"] | ||
else: | ||
assert "Hello !" in response["html"] # Template will render empty name | ||
|
||
|
||
def test_template_not_found(backend): | ||
"""Test handling of non-existent templates.""" | ||
with pytest.raises(TemplateDoesNotExist): | ||
backend._render_email("non_existent_template", {}) | ||
|
||
|
||
@override_settings(TEMPLATED_EMAIL_BACKEND='templated_email_md.backend.MarkdownTemplateBackend') | ||
def test_full_email_rendering(): | ||
"""Test the complete email rendering process.""" | ||
send_templated_mail( | ||
template_name="test_message", | ||
from_email="[email protected]", | ||
recipient_list=["[email protected]"], | ||
context={"name": "Test User"}, | ||
) | ||
|
||
assert len(mail.outbox) == 1 | ||
email = mail.outbox[0] | ||
|
||
# Check all parts of the email | ||
assert email.subject == "Test Email" | ||
assert email.from_email == "[email protected]" | ||
assert email.to == ["[email protected]"] | ||
|
||
# Check both HTML and plain text content | ||
assert "Hello Test User!" in email.body # Plain text version | ||
assert len(email.alternatives) == 1 # Should have HTML alternative | ||
html_content = email.alternatives[0][0] | ||
assert "<h1" in html_content | ||
assert "<strong>bold</strong>" in html_content | ||
|
||
|
||
@override_settings(TEMPLATED_EMAIL_BACKEND='templated_email_md.backend.MarkdownTemplateBackend') | ||
def test_fail_silently(): | ||
"""Test that fail_silently works as expected.""" | ||
backend = MarkdownTemplateBackend(fail_silently=True) | ||
|
||
# Try to render a non-existent template | ||
response = backend._render_email("non_existent_template", {}) | ||
|
||
# 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" | ||
|
||
|
||
def test_subject_from_context(backend): | ||
"""Test that subject can be overridden from context.""" | ||
response = backend._render_email( | ||
"test_message", | ||
{"name": "Test User", "subject": "Override Subject"} | ||
) | ||
assert response["subject"] == "Override Subject" |