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.

97 lines
3.0 KiB

  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. class SendAlertsTestCase(BaseTestCase):
  8. def setUp(self):
  9. super(SendAlertsTestCase, self).setUp()
  10. # Make alice eligible for reports:
  11. # account needs to be more than one month old
  12. self.alice.date_joined = now() - td(days=365)
  13. self.alice.save()
  14. # Make alice eligible for nags:
  15. self.profile.nag_period = td(hours=1)
  16. self.profile.next_nag_date = now() - td(seconds=10)
  17. self.profile.save()
  18. # And it needs at least one check that has been pinged.
  19. self.check = Check(user=self.alice, last_ping=now())
  20. self.check.status = "down"
  21. self.check.save()
  22. def test_it_sends_report(self):
  23. found = Command().handle_one_monthly_report()
  24. self.assertTrue(found)
  25. self.profile.refresh_from_db()
  26. self.assertTrue(self.profile.next_report_date > now())
  27. self.assertEqual(len(mail.outbox), 1)
  28. def test_it_obeys_next_report_date(self):
  29. self.profile.next_report_date = now() + td(days=1)
  30. self.profile.save()
  31. found = Command().handle_one_monthly_report()
  32. self.assertFalse(found)
  33. def test_it_obeys_reports_allowed_flag(self):
  34. self.profile.reports_allowed = False
  35. self.profile.save()
  36. found = Command().handle_one_monthly_report()
  37. self.assertFalse(found)
  38. def test_it_requires_pinged_checks(self):
  39. self.check.delete()
  40. found = Command().handle_one_monthly_report()
  41. self.assertTrue(found)
  42. # No email should have been sent:
  43. self.assertEqual(len(mail.outbox), 0)
  44. def test_it_sends_nag(self):
  45. found = Command().handle_one_nag()
  46. self.assertTrue(found)
  47. self.profile.refresh_from_db()
  48. self.assertTrue(self.profile.next_nag_date > now())
  49. self.assertEqual(len(mail.outbox), 1)
  50. def test_it_obeys_next_nag_date(self):
  51. self.profile.next_nag_date = now() + td(days=1)
  52. self.profile.save()
  53. # If next_nag_date is in future, a nag should not get sent.
  54. found = Command().handle_one_nag()
  55. self.assertFalse(found)
  56. def test_it_obeys_nag_period(self):
  57. self.profile.nag_period = td()
  58. self.profile.save()
  59. # If nag_period is 0 ("disabled"), a nag should not get sent.
  60. found = Command().handle_one_nag()
  61. self.assertFalse(found)
  62. def test_nags_require_down_checks(self):
  63. self.check.status = "up"
  64. self.check.save()
  65. found = Command().handle_one_nag()
  66. self.assertTrue(found)
  67. # No email should have been sent:
  68. self.assertEqual(len(mail.outbox), 0)
  69. # next_nag_date should now be unset
  70. self.profile.refresh_from_db()
  71. self.assertIsNone(self.profile.next_nag_date)