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.

125 lines
4.0 KiB

6 years ago
  1. from datetime import timedelta as td
  2. from django.core import mail
  3. from django.utils.timezone import now
  4. from hc.api.management.commands.sendreports import Command
  5. from hc.api.models import Check
  6. from hc.test import BaseTestCase
  7. from mock import Mock
  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()
  28. cmd.stdout = Mock() # silence output to stdout
  29. cmd.pause = Mock() # don't pause for 1s
  30. found = cmd.handle_one_monthly_report()
  31. self.assertTrue(found)
  32. self.profile.refresh_from_db()
  33. self.assertTrue(self.profile.next_report_date > now())
  34. self.assertEqual(self.profile.next_report_date.day, 1)
  35. self.assertEqual(len(mail.outbox), 1)
  36. email = mail.outbox[0]
  37. self.assertTrue("List-Unsubscribe" 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()
  65. cmd.stdout = Mock() # silence output to stdout
  66. cmd.pause = Mock() # don't pause for 1s
  67. found = cmd.handle_one_nag()
  68. self.assertTrue(found)
  69. self.profile.refresh_from_db()
  70. self.assertTrue(self.profile.next_nag_date > now())
  71. self.assertEqual(len(mail.outbox), 1)
  72. def test_it_obeys_next_nag_date(self):
  73. self.profile.next_nag_date = now() + td(days=1)
  74. self.profile.save()
  75. # If next_nag_date is in future, a nag should not get sent.
  76. found = Command().handle_one_nag()
  77. self.assertFalse(found)
  78. def test_it_obeys_nag_period(self):
  79. self.profile.nag_period = td()
  80. self.profile.save()
  81. # If nag_period is 0 ("disabled"), a nag should not get sent.
  82. found = Command().handle_one_nag()
  83. self.assertFalse(found)
  84. def test_nags_require_down_checks(self):
  85. self.check.status = "up"
  86. self.check.save()
  87. found = Command().handle_one_nag()
  88. self.assertTrue(found)
  89. # No email should have been sent:
  90. self.assertEqual(len(mail.outbox), 0)
  91. # next_nag_date should now be unset
  92. self.profile.refresh_from_db()
  93. self.assertIsNone(self.profile.next_nag_date)