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_data(self, value, status="down", email_verified=True):
  10. self.check = Check(project=self.project)
  11. self.check.status = status
  12. self.check.last_ping = now() - td(minutes=61)
  13. self.check.save()
  14. self.channel = Channel(project=self.project)
  15. self.channel.kind = "pagertree"
  16. self.channel.value = value
  17. self.channel.email_verified = email_verified
  18. self.channel.save()
  19. self.channel.checks.add(self.check)
  20. @patch("hc.api.transports.requests.request")
  21. def test_pagertree(self, mock_post):
  22. self._setup_data("123")
  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["event_type"], "trigger")
  29. @override_settings(PAGERTREE_ENABLED=False)
  30. def test_it_requires_pagertree_enabled(self):
  31. self._setup_data("123")
  32. self.channel.notify(self.check)
  33. n = Notification.objects.get()
  34. self.assertEqual(n.error, "PagerTree notifications are not enabled.")