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.

138 lines
4.6 KiB

  1. from datetime import timedelta
  2. from mock import Mock, patch
  3. from django.core.management import call_command
  4. from django.utils.timezone import now
  5. from hc.api.management.commands.sendalerts import Command, notify
  6. from hc.api.models import Check
  7. from hc.test import BaseTestCase
  8. class SendAlertsTestCase(BaseTestCase):
  9. def test_it_handles_grace_period(self):
  10. check = Check(user=self.alice, status="up")
  11. # 1 day 30 minutes after ping the check is in grace period:
  12. check.last_ping = now() - timedelta(days=1, minutes=30)
  13. check.alert_after = check.get_alert_after()
  14. check.save()
  15. # Expect no exceptions--
  16. Command().handle_one()
  17. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  18. def test_it_notifies_when_check_goes_down(self, mock_notify):
  19. check = Check(user=self.alice, status="up")
  20. check.last_ping = now() - timedelta(days=2)
  21. check.alert_after = check.get_alert_after()
  22. check.save()
  23. result = Command().handle_one()
  24. # If it finds work, it should return True
  25. self.assertTrue(result)
  26. # It should change stored status to "down"
  27. check.refresh_from_db()
  28. self.assertEqual(check.status, "down")
  29. # It should call `notify_on_thread`
  30. self.assertTrue(mock_notify.called)
  31. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  32. def test_it_notifies_when_check_goes_up(self, mock_notify):
  33. check = Check(user=self.alice, status="down")
  34. check.last_ping = now()
  35. check.alert_after = check.get_alert_after()
  36. check.save()
  37. result = Command().handle_one()
  38. # If it finds work, it should return True
  39. self.assertTrue(result)
  40. # It should change stored status to "up"
  41. check.refresh_from_db()
  42. self.assertEqual(check.status, "up")
  43. # It should call `notify_on_thread`
  44. self.assertTrue(mock_notify.called)
  45. # alert_after now should be set
  46. self.assertTrue(check.alert_after)
  47. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  48. def test_it_updates_alert_after(self, mock_notify):
  49. check = Check(user=self.alice, status="up")
  50. check.last_ping = now() - timedelta(hours=1)
  51. check.alert_after = check.last_ping
  52. check.save()
  53. result = Command().handle_one()
  54. # If it finds work, it should return True
  55. self.assertTrue(result)
  56. # It should change stored status to "down"
  57. check.refresh_from_db()
  58. # alert_after should have been increased
  59. self.assertTrue(check.alert_after > check.last_ping)
  60. # notify_on_thread should *not* have been called
  61. self.assertFalse(mock_notify.called)
  62. @patch("hc.api.management.commands.sendalerts.notify")
  63. def test_it_works_synchronously(self, mock_notify):
  64. check = Check(user=self.alice, status="up")
  65. check.last_ping = now() - timedelta(days=2)
  66. check.alert_after = check.get_alert_after()
  67. check.save()
  68. call_command("sendalerts", loop=False, use_threads=False)
  69. # It should call `notify` instead of `notify_on_thread`
  70. self.assertTrue(mock_notify.called)
  71. def test_it_updates_owners_next_nag_date(self):
  72. self.profile.nag_period = timedelta(hours=1)
  73. self.profile.save()
  74. check = Check(user=self.alice, status="down")
  75. check.last_ping = now() - timedelta(days=2)
  76. check.alert_after = check.get_alert_after()
  77. check.save()
  78. notify(check.id, Mock())
  79. self.profile.refresh_from_db()
  80. self.assertIsNotNone(self.profile.next_nag_date)
  81. def test_it_updates_members_next_nag_date(self):
  82. self.bobs_profile.nag_period = timedelta(hours=1)
  83. self.bobs_profile.save()
  84. check = Check(user=self.alice, status="down")
  85. check.last_ping = now() - timedelta(days=2)
  86. check.alert_after = check.get_alert_after()
  87. check.save()
  88. notify(check.id, Mock())
  89. self.bobs_profile.refresh_from_db()
  90. self.assertIsNotNone(self.bobs_profile.next_nag_date)
  91. def test_it_does_not_touch_already_set_next_nag_dates(self):
  92. original_nag_date = now() - timedelta(minutes=30)
  93. self.profile.nag_period = timedelta(hours=1)
  94. self.profile.next_nag_date = original_nag_date
  95. self.profile.save()
  96. check = Check(user=self.alice, status="down")
  97. check.last_ping = now() - timedelta(days=2)
  98. check.alert_after = check.get_alert_after()
  99. check.save()
  100. notify(check.id, Mock())
  101. self.profile.refresh_from_db()
  102. self.assertEqual(self.profile.next_nag_date, original_nag_date)