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.

124 lines
3.9 KiB

  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(SendReportsTestCase, self).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_allowed = False
  19. self.bobs_profile.save()
  20. self.charlies_profile.reports_allowed = False
  21. self.charlies_profile.save()
  22. # And it needs at least one check that has been pinged.
  23. self.check = Check(project=self.project, last_ping=now())
  24. self.check.status = "down"
  25. self.check.save()
  26. def test_it_sends_report(self):
  27. cmd = Command(stdout=Mock())
  28. cmd.pause = Mock() # don't pause for 1s
  29. found = cmd.handle_one_monthly_report()
  30. self.assertTrue(found)
  31. self.profile.refresh_from_db()
  32. self.assertTrue(self.profile.next_report_date > now())
  33. self.assertEqual(self.profile.next_report_date.day, 1)
  34. self.assertEqual(len(mail.outbox), 1)
  35. email = mail.outbox[0]
  36. self.assertTrue("List-Unsubscribe" in email.extra_headers)
  37. self.assertTrue("List-Unsubscribe-Post" in email.extra_headers)
  38. def test_it_obeys_next_report_date(self):
  39. self.profile.next_report_date = now() + td(days=1)
  40. self.profile.save()
  41. found = Command().handle_one_monthly_report()
  42. self.assertFalse(found)
  43. def test_it_fills_blank_next_report_date(self):
  44. self.profile.next_report_date = None
  45. self.profile.save()
  46. found = Command().handle_one_monthly_report()
  47. self.assertTrue(found)
  48. self.profile.refresh_from_db()
  49. self.assertTrue(self.profile.next_report_date)
  50. self.assertEqual(self.profile.next_report_date.day, 1)
  51. self.assertEqual(len(mail.outbox), 0)
  52. def test_it_obeys_reports_allowed_flag(self):
  53. self.profile.reports_allowed = False
  54. self.profile.save()
  55. found = Command().handle_one_monthly_report()
  56. self.assertFalse(found)
  57. def test_it_requires_pinged_checks(self):
  58. self.check.delete()
  59. found = Command().handle_one_monthly_report()
  60. self.assertTrue(found)
  61. # No email should have been sent:
  62. self.assertEqual(len(mail.outbox), 0)
  63. def test_it_sends_nag(self):
  64. cmd = Command(stdout=Mock())
  65. cmd.pause = Mock() # don't pause for 1s
  66. found = cmd.handle_one_nag()
  67. self.assertTrue(found)
  68. self.profile.refresh_from_db()
  69. self.assertTrue(self.profile.next_nag_date > now())
  70. self.assertEqual(len(mail.outbox), 1)
  71. def test_it_obeys_next_nag_date(self):
  72. self.profile.next_nag_date = now() + td(days=1)
  73. self.profile.save()
  74. # If next_nag_date is in future, a nag should not get sent.
  75. found = Command().handle_one_nag()
  76. self.assertFalse(found)
  77. def test_it_obeys_nag_period(self):
  78. self.profile.nag_period = td()
  79. self.profile.save()
  80. # If nag_period is 0 ("disabled"), a nag should not get sent.
  81. found = Command().handle_one_nag()
  82. self.assertFalse(found)
  83. def test_nags_require_down_checks(self):
  84. self.check.status = "up"
  85. self.check.save()
  86. found = Command().handle_one_nag()
  87. self.assertTrue(found)
  88. # No email should have been sent:
  89. self.assertEqual(len(mail.outbox), 0)
  90. # next_nag_date should now be unset
  91. self.profile.refresh_from_db()
  92. self.assertIsNone(self.profile.next_nag_date)