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.

59 lines
2.0 KiB

  1. # coding: utf-8
  2. from datetime import timedelta as td
  3. import json
  4. from unittest.mock import patch
  5. from django.utils.timezone import now
  6. from hc.api.models import Channel, Check, Notification
  7. from hc.test import BaseTestCase
  8. from django.test.utils import override_settings
  9. class NotifyTestCase(BaseTestCase):
  10. def _setup_data(self, value, status="down", email_verified=True):
  11. self.check = Check(project=self.project)
  12. self.check.status = status
  13. self.check.last_ping = now() - td(minutes=61)
  14. self.check.save()
  15. self.channel = Channel(project=self.project)
  16. self.channel.kind = "pd"
  17. self.channel.value = value
  18. self.channel.email_verified = email_verified
  19. self.channel.save()
  20. self.channel.checks.add(self.check)
  21. @patch("hc.api.transports.requests.request")
  22. def test_pd(self, mock_post):
  23. self._setup_data("123")
  24. mock_post.return_value.status_code = 200
  25. self.channel.notify(self.check)
  26. assert Notification.objects.count() == 1
  27. args, kwargs = mock_post.call_args
  28. payload = kwargs["json"]
  29. self.assertEqual(payload["event_type"], "trigger")
  30. self.assertEqual(payload["service_key"], "123")
  31. @patch("hc.api.transports.requests.request")
  32. def test_pd_complex(self, mock_post):
  33. self._setup_data(json.dumps({"service_key": "456"}))
  34. mock_post.return_value.status_code = 200
  35. self.channel.notify(self.check)
  36. assert Notification.objects.count() == 1
  37. args, kwargs = mock_post.call_args
  38. payload = kwargs["json"]
  39. self.assertEqual(payload["event_type"], "trigger")
  40. self.assertEqual(payload["service_key"], "456")
  41. @override_settings(PD_ENABLED=False)
  42. def test_it_requires_pd_enabled(self):
  43. self._setup_data("123")
  44. self.channel.notify(self.check)
  45. n = Notification.objects.get()
  46. self.assertEqual(n.error, "PagerDuty notifications are not enabled.")