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_commands.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import datetime
  2. import os
  3. from django.core.files.base import ContentFile
  4. from django.core.management import call_command
  5. from django.test import TestCase
  6. from django.test.utils import override_settings
  7. from django.utils.timezone import now
  8. from ..models import Attachment, Email, STATUS
  9. class CommandTest(TestCase):
  10. def test_cleanup_mail_with_orphaned_attachments(self):
  11. self.assertEqual(Email.objects.count(), 0)
  12. email = Email.objects.create(to=['to@example.com'],
  13. from_email='from@example.com',
  14. subject='Subject')
  15. email.created = now() - datetime.timedelta(31)
  16. email.save()
  17. attachment = Attachment()
  18. attachment.file.save(
  19. 'test.txt', content=ContentFile('test file content'), save=True
  20. )
  21. email.attachments.add(attachment)
  22. attachment_path = attachment.file.name
  23. # We have orphaned attachment now
  24. call_command('cleanup_mail', days=30)
  25. self.assertEqual(Email.objects.count(), 0)
  26. self.assertEqual(Attachment.objects.count(), 1)
  27. # Actually cleanup orphaned attachments
  28. call_command('cleanup_mail', '-da', days=30)
  29. self.assertEqual(Email.objects.count(), 0)
  30. self.assertEqual(Attachment.objects.count(), 0)
  31. # Check that the actual file has been deleted as well
  32. self.assertFalse(os.path.exists(attachment_path))
  33. # Check if the email attachment's actual file have been deleted
  34. Email.objects.all().delete()
  35. email = Email.objects.create(to=['to@example.com'],
  36. from_email='from@example.com',
  37. subject='Subject')
  38. email.created = now() - datetime.timedelta(31)
  39. email.save()
  40. attachment = Attachment()
  41. attachment.file.save(
  42. 'test.txt', content=ContentFile('test file content'), save=True
  43. )
  44. email.attachments.add(attachment)
  45. attachment_path = attachment.file.name
  46. # Simulate that the files have been deleted by accidents
  47. os.remove(attachment_path)
  48. # No exceptions should break the cleanup
  49. call_command('cleanup_mail', '-da', days=30)
  50. self.assertEqual(Email.objects.count(), 0)
  51. self.assertEqual(Attachment.objects.count(), 0)
  52. def test_cleanup_mail(self):
  53. """
  54. The ``cleanup_mail`` command deletes mails older than a specified
  55. amount of days
  56. """
  57. self.assertEqual(Email.objects.count(), 0)
  58. # The command shouldn't delete today's email
  59. email = Email.objects.create(from_email='from@example.com',
  60. to=['to@example.com'])
  61. call_command('cleanup_mail', days=30)
  62. self.assertEqual(Email.objects.count(), 1)
  63. # Email older than 30 days should be deleted
  64. email.created = now() - datetime.timedelta(31)
  65. email.save()
  66. call_command('cleanup_mail', days=30)
  67. self.assertEqual(Email.objects.count(), 0)
  68. TEST_SETTINGS = {
  69. 'BACKENDS': {
  70. 'default': 'django.core.mail.backends.dummy.EmailBackend',
  71. },
  72. 'BATCH_SIZE': 1
  73. }
  74. @override_settings(POST_OFFICE=TEST_SETTINGS)
  75. def test_send_queued_mail(self):
  76. """
  77. Ensure that ``send_queued_mail`` behaves properly and sends all queued
  78. emails in two batches.
  79. """
  80. # Make sure that send_queued_mail with empty queue does not raise error
  81. call_command('send_queued_mail', processes=1)
  82. Email.objects.create(from_email='from@example.com',
  83. to=['to@example.com'], status=STATUS.queued)
  84. Email.objects.create(from_email='from@example.com',
  85. to=['to@example.com'], status=STATUS.queued)
  86. call_command('send_queued_mail', processes=1)
  87. self.assertEqual(Email.objects.filter(status=STATUS.sent).count(), 2)
  88. self.assertEqual(Email.objects.filter(status=STATUS.queued).count(), 0)
  89. def test_successful_deliveries_logging(self):
  90. """
  91. Successful deliveries are only logged when log_level is 2.
  92. """
  93. email = Email.objects.create(from_email='from@example.com',
  94. to=['to@example.com'], status=STATUS.queued)
  95. call_command('send_queued_mail', log_level=0)
  96. self.assertEqual(email.logs.count(), 0)
  97. email = Email.objects.create(from_email='from@example.com',
  98. to=['to@example.com'], status=STATUS.queued)
  99. call_command('send_queued_mail', log_level=1)
  100. self.assertEqual(email.logs.count(), 0)
  101. email = Email.objects.create(from_email='from@example.com',
  102. to=['to@example.com'], status=STATUS.queued)
  103. call_command('send_queued_mail', log_level=2)
  104. self.assertEqual(email.logs.count(), 1)
  105. def test_failed_deliveries_logging(self):
  106. """
  107. Failed deliveries are logged when log_level is 1 and 2.
  108. """
  109. email = Email.objects.create(from_email='from@example.com',
  110. to=['to@example.com'], status=STATUS.queued,
  111. backend_alias='error')
  112. call_command('send_queued_mail', log_level=0)
  113. self.assertEqual(email.logs.count(), 0)
  114. email = Email.objects.create(from_email='from@example.com',
  115. to=['to@example.com'], status=STATUS.queued,
  116. backend_alias='error')
  117. call_command('send_queued_mail', log_level=1)
  118. self.assertEqual(email.logs.count(), 1)
  119. email = Email.objects.create(from_email='from@example.com',
  120. to=['to@example.com'], status=STATUS.queued,
  121. backend_alias='error')
  122. call_command('send_queued_mail', log_level=2)
  123. self.assertEqual(email.logs.count(), 1)