| 1 |
from django.core import mail |
| 2 |
from django.core.management import CommandError, call_command |
| 3 |
from django.test import SimpleTestCase, override_settings |
| 4 |
|
| 5 |
|
| 6 |
@override_settings( |
| 7 |
ADMINS=( |
| 8 |
("Admin", "[email protected]"), |
| 9 |
("Admin and Manager", "[email protected]"), |
| 10 |
), |
| 11 |
MANAGERS=( |
| 12 |
("Manager", "[email protected]"), |
| 13 |
("Admin and Manager", "[email protected]"), |
| 14 |
), |
| 15 |
) |
| 16 |
class SendTestEmailManagementCommand(SimpleTestCase): |
| 17 |
""" |
| 18 |
Test the sending of a test email using the `sendtestemail` command. |
| 19 |
""" |
| 20 |
|
| 21 |
def test_single_receiver(self): |
| 22 |
""" |
| 23 |
The mail is sent with the correct subject and recipient. |
| 24 |
""" |
| 25 |
recipient = "[email protected]" |
| 26 |
call_command("sendtestemail", recipient) |
| 27 |
self.assertEqual(len(mail.outbox), 1) |
| 28 |
mail_message = mail.outbox[0] |
| 29 |
self.assertEqual(mail_message.subject[0:15], "Test email from") |
| 30 |
self.assertEqual(mail_message.recipients(), [recipient]) |
| 31 |
|
| 32 |
def test_multiple_receivers(self): |
| 33 |
""" |
| 34 |
The mail may be sent with multiple recipients. |
| 35 |
""" |
| 36 |
recipients = ["[email protected]", "[email protected]"] |
| 37 |
call_command("sendtestemail", recipients[0], recipients[1]) |
| 38 |
self.assertEqual(len(mail.outbox), 1) |
| 39 |
mail_message = mail.outbox[0] |
| 40 |
self.assertEqual(mail_message.subject[0:15], "Test email from") |
| 41 |
self.assertEqual( |
| 42 |
sorted(mail_message.recipients()), |
| 43 |
[ |
| 44 |
"[email protected]", |
| 45 |
"[email protected]", |
| 46 |
], |
| 47 |
) |
| 48 |
|
| 49 |
def test_missing_receivers(self): |
| 50 |
""" |
| 51 |
The command should complain if no receivers are given (and --admins or |
| 52 |
--managers are not set). |
| 53 |
""" |
| 54 |
msg = ( |
| 55 |
"You must specify some email recipients, or pass the --managers or " |
| 56 |
"--admin options." |
| 57 |
) |
| 58 |
with self.assertRaisesMessage(CommandError, msg): |
| 59 |
call_command("sendtestemail") |
| 60 |
|
| 61 |
def test_manager_receivers(self): |
| 62 |
""" |
| 63 |
The mail should be sent to the email addresses specified in |
| 64 |
settings.MANAGERS. |
| 65 |
""" |
| 66 |
call_command("sendtestemail", "--managers") |
| 67 |
self.assertEqual(len(mail.outbox), 1) |
| 68 |
mail_message = mail.outbox[0] |
| 69 |
self.assertEqual( |
| 70 |
sorted(mail_message.recipients()), |
| 71 |
[ |
| 72 |
"[email protected]", |
| 73 |
"[email protected]", |
| 74 |
], |
| 75 |
) |
| 76 |
|
| 77 |
def test_admin_receivers(self): |
| 78 |
""" |
| 79 |
The mail should be sent to the email addresses specified in |
| 80 |
settings.ADMIN. |
| 81 |
""" |
| 82 |
call_command("sendtestemail", "--admins") |
| 83 |
self.assertEqual(len(mail.outbox), 1) |
| 84 |
mail_message = mail.outbox[0] |
| 85 |
self.assertEqual( |
| 86 |
sorted(mail_message.recipients()), |
| 87 |
[ |
| 88 |
"[email protected]", |
| 89 |
"[email protected]", |
| 90 |
], |
| 91 |
) |
| 92 |
|
| 93 |
def test_manager_and_admin_receivers(self): |
| 94 |
""" |
| 95 |
The mail should be sent to the email addresses specified in both |
| 96 |
settings.MANAGERS and settings.ADMINS. |
| 97 |
""" |
| 98 |
call_command("sendtestemail", "--managers", "--admins") |
| 99 |
self.assertEqual(len(mail.outbox), 2) |
| 100 |
manager_mail = mail.outbox[0] |
| 101 |
self.assertEqual( |
| 102 |
sorted(manager_mail.recipients()), |
| 103 |
[ |
| 104 |
"[email protected]", |
| 105 |
"[email protected]", |
| 106 |
], |
| 107 |
) |
| 108 |
admin_mail = mail.outbox[1] |
| 109 |
self.assertEqual( |
| 110 |
sorted(admin_mail.recipients()), |
| 111 |
[ |
| 112 |
"[email protected]", |
| 113 |
"[email protected]", |
| 114 |
], |
| 115 |
) |