Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_backends.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from django.conf import settings
  2. from django.core.mail import EmailMultiAlternatives, send_mail, EmailMessage
  3. from django.core.mail.backends.base import BaseEmailBackend
  4. from django.test import TestCase
  5. from django.test.utils import override_settings
  6. from ..models import Email, STATUS, PRIORITY
  7. from ..settings import get_backend
  8. class ErrorRaisingBackend(BaseEmailBackend):
  9. """
  10. An EmailBackend that always raises an error during sending
  11. to test if django_mailer handles sending error correctly
  12. """
  13. def send_messages(self, email_messages):
  14. raise Exception('Fake Error')
  15. class BackendTest(TestCase):
  16. @override_settings(EMAIL_BACKEND='post_office.EmailBackend')
  17. def test_email_backend(self):
  18. """
  19. Ensure that email backend properly queue email messages.
  20. """
  21. send_mail('Test', 'Message', 'from@example.com', ['to@example.com'])
  22. email = Email.objects.latest('id')
  23. self.assertEqual(email.subject, 'Test')
  24. self.assertEqual(email.status, STATUS.queued)
  25. self.assertEqual(email.priority, PRIORITY.medium)
  26. def test_email_backend_setting(self):
  27. """
  28. """
  29. old_email_backend = getattr(settings, 'EMAIL_BACKEND', None)
  30. old_post_office_backend = getattr(settings, 'POST_OFFICE_BACKEND', None)
  31. if hasattr(settings, 'EMAIL_BACKEND'):
  32. delattr(settings, 'EMAIL_BACKEND')
  33. if hasattr(settings, 'POST_OFFICE_BACKEND'):
  34. delattr(settings, 'POST_OFFICE_BACKEND')
  35. previous_settings = settings.POST_OFFICE
  36. delattr(settings, 'POST_OFFICE')
  37. # If no email backend is set, backend should default to SMTP
  38. self.assertEqual(get_backend(), 'django.core.mail.backends.smtp.EmailBackend')
  39. # If EMAIL_BACKEND is set to PostOfficeBackend, use SMTP to send by default
  40. setattr(settings, 'EMAIL_BACKEND', 'post_office.EmailBackend')
  41. self.assertEqual(get_backend(), 'django.core.mail.backends.smtp.EmailBackend')
  42. # If EMAIL_BACKEND is set on new dictionary-styled settings, use that
  43. setattr(settings, 'POST_OFFICE', {'EMAIL_BACKEND': 'test'})
  44. self.assertEqual(get_backend(), 'test')
  45. delattr(settings, 'POST_OFFICE')
  46. if old_email_backend:
  47. setattr(settings, 'EMAIL_BACKEND', old_email_backend)
  48. else:
  49. delattr(settings, 'EMAIL_BACKEND')
  50. setattr(settings, 'POST_OFFICE', previous_settings)
  51. @override_settings(EMAIL_BACKEND='post_office.EmailBackend')
  52. def test_sending_html_email(self):
  53. """
  54. "text/html" attachments to Email should be persisted into the database
  55. """
  56. message = EmailMultiAlternatives('subject', 'body', 'from@example.com',
  57. ['recipient@example.com'])
  58. message.attach_alternative('html', "text/html")
  59. message.send()
  60. email = Email.objects.latest('id')
  61. self.assertEqual(email.html_message, 'html')
  62. @override_settings(EMAIL_BACKEND='post_office.EmailBackend')
  63. def test_headers_sent(self):
  64. """
  65. Test that headers are correctly set on the outgoing emails.
  66. """
  67. message = EmailMessage('subject', 'body', 'from@example.com',
  68. ['recipient@example.com'],
  69. headers={'Reply-To': 'reply@example.com'})
  70. message.send()
  71. email = Email.objects.latest('id')
  72. self.assertEqual(email.headers, {'Reply-To': 'reply@example.com'})
  73. @override_settings(EMAIL_BACKEND='post_office.EmailBackend')
  74. def test_backend_attachments(self):
  75. message = EmailMessage('subject', 'body', 'from@example.com',
  76. ['recipient@example.com'])
  77. message.attach('attachment.txt', 'attachment content')
  78. message.send()
  79. email = Email.objects.latest('id')
  80. self.assertEqual(email.attachments.count(), 1)
  81. self.assertEqual(email.attachments.all()[0].name, 'attachment.txt')
  82. self.assertEqual(email.attachments.all()[0].file.read(), b'attachment content')
  83. @override_settings(
  84. EMAIL_BACKEND='post_office.EmailBackend',
  85. POST_OFFICE={
  86. 'DEFAULT_PRIORITY': 'now',
  87. 'BACKENDS': {'default': 'django.core.mail.backends.dummy.EmailBackend'}
  88. }
  89. )
  90. def test_default_priority_now(self):
  91. # If DEFAULT_PRIORITY is "now", mails should be sent right away
  92. send_mail('Test', 'Message', 'from1@example.com', ['to@example.com'])
  93. email = Email.objects.latest('id')
  94. self.assertEqual(email.status, STATUS.sent)