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.

95 lines
3.2 KiB

  1. from datetime import timedelta
  2. from mock import patch
  3. from django.core.management import call_command
  4. from django.utils import timezone
  5. from hc.api.management.commands.sendalerts import Command
  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 = timezone.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 = timezone.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 = timezone.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 = timezone.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 = timezone.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)