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.

147 lines
4.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. from datetime import timedelta as td
  2. from unittest.mock import Mock
  3. from django.core import mail
  4. from django.utils.timezone import now
  5. from hc.api.management.commands.sendreports import Command
  6. from hc.api.models import Check
  7. from hc.test import BaseTestCase
  8. class SendReportsTestCase(BaseTestCase):
  9. def setUp(self):
  10. super().setUp()
  11. # Make alice eligible for a monthly report:
  12. self.profile.next_report_date = now() - td(hours=1)
  13. # and for a nag
  14. self.profile.nag_period = td(hours=1)
  15. self.profile.next_nag_date = now() - td(seconds=10)
  16. self.profile.save()
  17. # Disable bob's and charlie's monthly reports so they don't interfere
  18. self.bobs_profile.reports = "off"
  19. self.bobs_profile.reports_allowed = False
  20. self.bobs_profile.save()
  21. self.charlies_profile.reports = "off"
  22. self.charlies_profile.reports_allowed = False
  23. self.charlies_profile.save()
  24. # And it needs at least one check that has been pinged.
  25. self.check = Check(project=self.project, last_ping=now())
  26. self.check.status = "down"
  27. self.check.save()
  28. def test_it_sends_monthly_report(self):
  29. cmd = Command(stdout=Mock())
  30. cmd.pause = Mock() # don't pause for 1s
  31. found = cmd.handle_one_report()
  32. self.assertTrue(found)
  33. self.profile.refresh_from_db()
  34. self.assertTrue(self.profile.next_report_date > now())
  35. self.assertEqual(self.profile.next_report_date.day, 1)
  36. self.assertEqual(len(mail.outbox), 1)
  37. email = mail.outbox[0]
  38. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  39. self.assertTrue("List-Unsubscribe-Post" in email.extra_headers)
  40. self.assertEqual(email.subject, "Monthly Report")
  41. self.assertIn("This is a monthly report", email.body)
  42. self.assertIn("This is a monthly report", email.alternatives[0][0])
  43. def test_it_sends_weekly_report(self):
  44. self.profile.reports = "weekly"
  45. self.profile.save()
  46. cmd = Command(stdout=Mock())
  47. cmd.pause = Mock() # don't pause for 1s
  48. cmd.handle_one_report()
  49. email = mail.outbox[0]
  50. self.assertEqual(email.subject, "Weekly Report")
  51. self.assertIn("This is a weekly report", email.body)
  52. self.assertIn("This is a weekly report", email.alternatives[0][0])
  53. def test_it_obeys_next_report_date(self):
  54. self.profile.next_report_date = now() + td(days=1)
  55. self.profile.save()
  56. found = Command().handle_one_report()
  57. self.assertFalse(found)
  58. def test_it_fills_blank_next_report_date(self):
  59. self.profile.next_report_date = None
  60. self.profile.save()
  61. found = Command().handle_one_report()
  62. self.assertTrue(found)
  63. self.profile.refresh_from_db()
  64. self.assertEqual(self.profile.next_report_date.day, 1)
  65. self.assertEqual(len(mail.outbox), 0)
  66. def test_it_obeys_reports_off(self):
  67. self.profile.reports = "off"
  68. self.profile.save()
  69. found = Command().handle_one_report()
  70. self.assertFalse(found)
  71. def test_it_requires_pinged_checks(self):
  72. self.check.delete()
  73. found = Command().handle_one_report()
  74. self.assertTrue(found)
  75. # No email should have been sent:
  76. self.assertEqual(len(mail.outbox), 0)
  77. def test_it_sends_nag(self):
  78. cmd = Command(stdout=Mock())
  79. cmd.pause = Mock() # don't pause for 1s
  80. found = cmd.handle_one_nag()
  81. self.assertTrue(found)
  82. self.profile.refresh_from_db()
  83. self.assertTrue(self.profile.next_nag_date > now())
  84. self.assertEqual(len(mail.outbox), 1)
  85. email = mail.outbox[0]
  86. html = email.alternatives[0][0]
  87. self.assertNotIn(str(self.check.code), email.body)
  88. self.assertNotIn(str(self.check.code), html)
  89. def test_it_obeys_next_nag_date(self):
  90. self.profile.next_nag_date = now() + td(days=1)
  91. self.profile.save()
  92. # If next_nag_date is in future, a nag should not get sent.
  93. found = Command().handle_one_nag()
  94. self.assertFalse(found)
  95. def test_it_obeys_nag_period(self):
  96. self.profile.nag_period = td()
  97. self.profile.save()
  98. # If nag_period is 0 ("disabled"), a nag should not get sent.
  99. found = Command().handle_one_nag()
  100. self.assertFalse(found)
  101. def test_nags_require_down_checks(self):
  102. self.check.status = "up"
  103. self.check.save()
  104. found = Command().handle_one_nag()
  105. self.assertTrue(found)
  106. # No email should have been sent:
  107. self.assertEqual(len(mail.outbox), 0)
  108. # next_nag_date should now be unset
  109. self.profile.refresh_from_db()
  110. self.assertIsNone(self.profile.next_nag_date)