Skip to content

Commit 4f11436

Browse files
committed
Fix #155 introduce sendtesttemplatedemail command
The command should be similar to the Django's one.
1 parent 14843fe commit 4f11436

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed

templated_email/management/__init__.py

Whitespace-only changes.

templated_email/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from django.conf import settings
2+
from django.core.management.commands.sendtestemail import (
3+
Command as SendTestEmailCommand,
4+
)
5+
from templated_email import send_templated_mail
6+
7+
8+
class Command(SendTestEmailCommand):
9+
help = "Sends a test email to the specified email address(es) using templated email."
10+
11+
def handle(self, *args, **kwargs):
12+
subject = "Test email from sendtesttemplatedemail management command"
13+
message = "If you're reading this, it was successful."
14+
15+
kw = dict(
16+
template_name="test_email",
17+
context={"message": message},
18+
from_email=None,
19+
)
20+
21+
# Get recipient lists from parent command
22+
recipients = kwargs.get("email", [])
23+
24+
if recipients:
25+
send_templated_mail(recipient_list=recipients, **kw)
26+
self.stdout.write(
27+
self.style.SUCCESS(
28+
f"Test email sent to {', '.join(recipients)}."
29+
)
30+
)
31+
32+
# Send to managers if requested
33+
if kwargs.get("managers"):
34+
manager_emails = [email for name, email in settings.MANAGERS]
35+
if manager_emails:
36+
send_templated_mail(recipient_list=manager_emails, **kw)
37+
self.stdout.write(
38+
self.style.SUCCESS(
39+
f"Test email sent to managers: {', '.join(manager_emails)}."
40+
)
41+
)
42+
else:
43+
self.stdout.write(
44+
self.style.WARNING("No managers configured in settings.")
45+
)
46+
47+
# Send to admins if requested
48+
if kwargs.get("admins"):
49+
admin_emails = [email for name, email in settings.ADMINS]
50+
if admin_emails:
51+
send_templated_mail(recipient_list=admin_emails, **kw)
52+
self.stdout.write(
53+
self.style.SUCCESS(
54+
f"Test email sent to admins: {', '.join(admin_emails)}."
55+
)
56+
)
57+
else:
58+
self.stdout.write(
59+
self.style.WARNING("No admins configured in settings.")
60+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{% block subject %}Test email sent by sendtesttemplatedemail management command{% endblock %}
2+
3+
{% block plain %}
4+
Hello,
5+
6+
{{ message }}
7+
8+
This is a test email sent from the Django Templated Email library.
9+
If you can read this message, your email configuration is working correctly.
10+
11+
Best regards,
12+
Django Templated Email
13+
{% endblock %}
14+
15+
{% block html %}
16+
<!DOCTYPE html>
17+
<html>
18+
<head>
19+
<meta charset="utf-8">
20+
<title>Test Email</title>
21+
</head>
22+
<body>
23+
<h1>Hello!</h1>
24+
<p>{{ message }}</p>
25+
<p>This is a test email sent from the Django Templated Email library.</p>
26+
<p>If you can read this message, your email configuration is working correctly.</p>
27+
<p>Best regards,<br>
28+
Django Templated Email</p>
29+
</body>
30+
</html>
31+
{% endblock %}

tests/test_management_commands.py

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
from io import StringIO
2+
from unittest.mock import MagicMock, patch
3+
from django.core.management import call_command
4+
from django.test import TestCase, override_settings
5+
6+
7+
class SendTestTemplatedEmailCommandTest(TestCase):
8+
def test_command_with_email_addresses(self):
9+
"""Test sending to specific email addresses."""
10+
out = StringIO()
11+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
12+
mock_send.return_value = None
13+
14+
call_command(
15+
'sendtesttemplatedemail',
16+
17+
18+
stdout=out
19+
)
20+
21+
mock_send.assert_called_once()
22+
call_args = mock_send.call_args
23+
self.assertEqual(
24+
call_args.kwargs['recipient_list'],
25+
26+
)
27+
self.assertEqual(call_args.kwargs['template_name'], 'test_email')
28+
self.assertIn('message', call_args.kwargs['context'])
29+
self.assertIsNone(call_args.kwargs['from_email'])
30+
31+
output = out.getvalue()
32+
self.assertIn('Test email sent to', output)
33+
self.assertIn('[email protected]', output)
34+
self.assertIn('[email protected]', output)
35+
36+
@override_settings(MANAGERS=[('Manager', '[email protected]')])
37+
def test_command_with_managers(self):
38+
"""Test sending to managers."""
39+
out = StringIO()
40+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
41+
mock_send.return_value = None
42+
43+
call_command(
44+
'sendtesttemplatedemail',
45+
'--managers',
46+
stdout=out
47+
)
48+
49+
mock_send.assert_called_once()
50+
call_args = mock_send.call_args
51+
self.assertEqual(
52+
call_args.kwargs['recipient_list'],
53+
54+
)
55+
self.assertEqual(call_args.kwargs['template_name'], 'test_email')
56+
57+
output = out.getvalue()
58+
self.assertIn('Test email sent to managers', output)
59+
self.assertIn('[email protected]', output)
60+
61+
@override_settings(ADMINS=[('Admin', '[email protected]')])
62+
def test_command_with_admins(self):
63+
"""Test sending to admins."""
64+
out = StringIO()
65+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
66+
mock_send.return_value = None
67+
68+
call_command(
69+
'sendtesttemplatedemail',
70+
'--admins',
71+
stdout=out
72+
)
73+
74+
mock_send.assert_called_once()
75+
call_args = mock_send.call_args
76+
self.assertEqual(
77+
call_args.kwargs['recipient_list'],
78+
79+
)
80+
self.assertEqual(call_args.kwargs['template_name'], 'test_email')
81+
82+
output = out.getvalue()
83+
self.assertIn('Test email sent to admins', output)
84+
self.assertIn('[email protected]', output)
85+
86+
@override_settings(
87+
MANAGERS=[('Manager1', '[email protected]'), ('Manager2', '[email protected]')],
88+
ADMINS=[('Admin1', '[email protected]'), ('Admin2', '[email protected]')]
89+
)
90+
def test_command_with_all_recipients(self):
91+
"""Test sending to email addresses, managers, and admins."""
92+
out = StringIO()
93+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
94+
mock_send.return_value = None
95+
96+
call_command(
97+
'sendtesttemplatedemail',
98+
99+
'--managers',
100+
'--admins',
101+
stdout=out
102+
)
103+
104+
# Should be called 3 times: once for direct email, once for managers, once for admins
105+
self.assertEqual(mock_send.call_count, 3)
106+
107+
# Check each call
108+
calls = mock_send.call_args_list
109+
110+
# First call - direct email
111+
self.assertEqual(
112+
calls[0].kwargs['recipient_list'],
113+
114+
)
115+
116+
# Second call - managers
117+
self.assertEqual(
118+
set(calls[1].kwargs['recipient_list']),
119+
120+
)
121+
122+
# Third call - admins
123+
self.assertEqual(
124+
set(calls[2].kwargs['recipient_list']),
125+
126+
)
127+
128+
output = out.getvalue()
129+
self.assertIn('[email protected]', output)
130+
self.assertIn('managers', output)
131+
self.assertIn('admins', output)
132+
133+
@override_settings(MANAGERS=[], ADMINS=[])
134+
def test_command_with_no_managers_or_admins(self):
135+
"""Test warning messages when no managers or admins are configured."""
136+
out = StringIO()
137+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
138+
mock_send.return_value = None
139+
140+
call_command(
141+
'sendtesttemplatedemail',
142+
'--managers',
143+
'--admins',
144+
stdout=out
145+
)
146+
147+
# Should not be called since no managers or admins are configured
148+
mock_send.assert_not_called()
149+
150+
output = out.getvalue()
151+
self.assertIn('No managers configured', output)
152+
self.assertIn('No admins configured', output)
153+
154+
def test_template_context(self):
155+
"""Test that the correct context is passed to the template."""
156+
out = StringIO()
157+
with patch('templated_email.management.commands.sendtesttemplatedemail.send_templated_mail') as mock_send:
158+
mock_send.return_value = None
159+
160+
call_command(
161+
'sendtesttemplatedemail',
162+
163+
stdout=out
164+
)
165+
166+
call_args = mock_send.call_args
167+
context = call_args.kwargs['context']
168+
self.assertIn('message', context)
169+
self.assertEqual(
170+
context['message'],
171+
"If you're reading this, it was successful."
172+
)

0 commit comments

Comments
 (0)