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.

44 lines
1.4 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. from unittest.mock import patch
  4. from django.utils.timezone import now
  5. from hc.api.models import Channel, Check, Notification
  6. from hc.test import BaseTestCase
  7. from django.test.utils import override_settings
  8. class NotifyTestCase(BaseTestCase):
  9. def setUp(self):
  10. super().setUp()
  11. self.check = Check(project=self.project)
  12. self.check.name = "Foo"
  13. self.check.status = "down"
  14. self.check.last_ping = now() - td(minutes=61)
  15. self.check.save()
  16. self.channel = Channel(project=self.project)
  17. self.channel.kind = "spike"
  18. self.channel.value = "https://spike.example.org"
  19. self.channel.save()
  20. self.channel.checks.add(self.check)
  21. @patch("hc.api.transports.requests.request")
  22. def test_it_works(self, mock_post):
  23. mock_post.return_value.status_code = 200
  24. self.channel.notify(self.check)
  25. assert Notification.objects.count() == 1
  26. args, kwargs = mock_post.call_args
  27. payload = kwargs["json"]
  28. self.assertEqual(payload["check_id"], str(self.check.code))
  29. self.assertEqual(payload["title"], "Foo is DOWN")
  30. @override_settings(SPIKE_ENABLED=False)
  31. def test_it_requires_spike_enabled(self):
  32. self.channel.notify(self.check)
  33. n = Notification.objects.get()
  34. self.assertEqual(n.error, "Spike notifications are not enabled.")