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.

82 lines
2.7 KiB

  1. from datetime import timedelta
  2. from mock import patch
  3. from django.utils import timezone
  4. from hc.api.management.commands.sendalerts import Command
  5. from hc.api.models import Check
  6. from hc.test import BaseTestCase
  7. class SendAlertsTestCase(BaseTestCase):
  8. def test_it_handles_grace_period(self):
  9. check = Check(user=self.alice, status="up")
  10. # 1 day 30 minutes after ping the check is in grace period:
  11. check.last_ping = timezone.now() - timedelta(days=1, minutes=30)
  12. check.alert_after = check.get_alert_after()
  13. check.save()
  14. # Expect no exceptions--
  15. Command().handle_one()
  16. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  17. def test_it_notifies_when_check_goes_down(self, mock_notify):
  18. check = Check(user=self.alice, status="up")
  19. check.last_ping = timezone.now() - timedelta(days=2)
  20. check.alert_after = check.get_alert_after()
  21. check.save()
  22. result = Command().handle_one()
  23. # If it finds work, it should return True
  24. self.assertTrue(result)
  25. # It should change stored status to "down"
  26. check.refresh_from_db()
  27. self.assertEqual(check.status, "down")
  28. # It should call `notify`
  29. self.assertTrue(mock_notify.called)
  30. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  31. def test_it_notifies_when_check_goes_up(self, mock_notify):
  32. check = Check(user=self.alice, status="down")
  33. check.last_ping = timezone.now()
  34. check.alert_after = check.get_alert_after()
  35. check.save()
  36. result = Command().handle_one()
  37. # If it finds work, it should return True
  38. self.assertTrue(result)
  39. # It should change stored status to "up"
  40. check.refresh_from_db()
  41. self.assertEqual(check.status, "up")
  42. # It should call `notify`
  43. self.assertTrue(mock_notify.called)
  44. # alert_after now should be set
  45. self.assertTrue(check.alert_after)
  46. @patch("hc.api.management.commands.sendalerts.notify_on_thread")
  47. def test_it_updates_alert_after(self, mock_notify):
  48. check = Check(user=self.alice, status="up")
  49. check.last_ping = timezone.now() - timedelta(hours=1)
  50. check.alert_after = check.last_ping
  51. check.save()
  52. result = Command().handle_one()
  53. # If it finds work, it should return True
  54. self.assertTrue(result)
  55. # It should change stored status to "down"
  56. check.refresh_from_db()
  57. # alert_after should have been increased
  58. self.assertTrue(check.alert_after > check.last_ping)
  59. # notify should *not* have been called
  60. self.assertFalse(mock_notify.called)