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.

85 lines
3.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.test.utils import override_settings
  6. from django.utils.timezone import now
  7. from hc.api.models import Channel, Check, Notification
  8. from hc.test import BaseTestCase
  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 = "opsgenie"
  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_opsgenie_with_legacy_value(self, mock_post):
  23. self._setup_data("123")
  24. mock_post.return_value.status_code = 202
  25. self.channel.notify(self.check)
  26. n = Notification.objects.first()
  27. self.assertEqual(n.error, "")
  28. self.assertEqual(mock_post.call_count, 1)
  29. args, kwargs = mock_post.call_args
  30. self.assertIn("api.opsgenie.com", args[1])
  31. payload = kwargs["json"]
  32. self.assertIn("DOWN", payload["message"])
  33. @patch("hc.api.transports.requests.request")
  34. def test_opsgenie_up(self, mock_post):
  35. self._setup_data("123", status="up")
  36. mock_post.return_value.status_code = 202
  37. self.channel.notify(self.check)
  38. n = Notification.objects.first()
  39. self.assertEqual(n.error, "")
  40. self.assertEqual(mock_post.call_count, 1)
  41. args, kwargs = mock_post.call_args
  42. method, url = args
  43. self.assertTrue(str(self.check.code) in url)
  44. @patch("hc.api.transports.requests.request")
  45. def test_opsgenie_with_json_value(self, mock_post):
  46. self._setup_data(json.dumps({"key": "456", "region": "eu"}))
  47. mock_post.return_value.status_code = 202
  48. self.channel.notify(self.check)
  49. n = Notification.objects.first()
  50. self.assertEqual(n.error, "")
  51. self.assertEqual(mock_post.call_count, 1)
  52. args, kwargs = mock_post.call_args
  53. self.assertIn("api.eu.opsgenie.com", args[1])
  54. @patch("hc.api.transports.requests.request")
  55. def test_opsgenie_returns_error(self, mock_post):
  56. self._setup_data("123")
  57. mock_post.return_value.status_code = 403
  58. mock_post.return_value.json.return_value = {"message": "Nice try"}
  59. self.channel.notify(self.check)
  60. n = Notification.objects.first()
  61. self.assertEqual(n.error, 'Received status code 403 with a message: "Nice try"')
  62. @override_settings(OPSGENIE_ENABLED=False)
  63. def test_it_requires_opsgenie_enabled(self):
  64. self._setup_data("123")
  65. self.channel.notify(self.check)
  66. n = Notification.objects.get()
  67. self.assertEqual(n.error, "Opsgenie notifications are not enabled.")