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.

166 lines
5.6 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. from django.core import mail
  2. from mock import patch
  3. from requests.exceptions import ConnectionError, Timeout
  4. from hc.api.models import Channel, Check, Notification
  5. from hc.test import BaseTestCase
  6. class NotifyTestCase(BaseTestCase):
  7. def _setup_data(self, kind, value, status="down", email_verified=True):
  8. self.check = Check()
  9. self.check.status = status
  10. self.check.save()
  11. self.channel = Channel(user=self.alice)
  12. self.channel.kind = kind
  13. self.channel.value = value
  14. self.channel.email_verified = email_verified
  15. self.channel.save()
  16. self.channel.checks.add(self.check)
  17. @patch("hc.api.transports.requests.request")
  18. def test_webhook(self, mock_get):
  19. self._setup_data("webhook", "http://example")
  20. mock_get.return_value.status_code = 200
  21. self.channel.notify(self.check)
  22. mock_get.assert_called_with(
  23. "get", u"http://example",
  24. headers={"User-Agent": "healthchecks.io"}, timeout=5)
  25. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  26. def test_webhooks_handle_timeouts(self, mock_get):
  27. self._setup_data("webhook", "http://example")
  28. self.channel.notify(self.check)
  29. n = Notification.objects.get()
  30. self.assertEqual(n.error, "Connection timed out")
  31. @patch("hc.api.transports.requests.request", side_effect=ConnectionError)
  32. def test_webhooks_handle_connection_errors(self, mock_get):
  33. self._setup_data("webhook", "http://example")
  34. self.channel.notify(self.check)
  35. n = Notification.objects.get()
  36. self.assertEqual(n.error, "Connection failed")
  37. @patch("hc.api.transports.requests.request")
  38. def test_webhooks_ignore_up_events(self, mock_get):
  39. self._setup_data("webhook", "http://example", status="up")
  40. self.channel.notify(self.check)
  41. self.assertFalse(mock_get.called)
  42. self.assertEqual(Notification.objects.count(), 0)
  43. @patch("hc.api.transports.requests.request")
  44. def test_webhooks_handle_500(self, mock_get):
  45. self._setup_data("webhook", "http://example")
  46. mock_get.return_value.status_code = 500
  47. self.channel.notify(self.check)
  48. n = Notification.objects.get()
  49. self.assertEqual(n.error, "Received status code 500")
  50. def test_email(self):
  51. self._setup_data("email", "[email protected]")
  52. self.channel.notify(self.check)
  53. n = Notification.objects.get()
  54. self.assertEqual(n.error, "")
  55. # And email should have been sent
  56. self.assertEqual(len(mail.outbox), 1)
  57. def test_it_skips_unverified_email(self):
  58. self._setup_data("email", "[email protected]", email_verified=False)
  59. self.channel.notify(self.check)
  60. assert Notification.objects.count() == 1
  61. n = Notification.objects.first()
  62. self.assertEqual(n.error, "Email not verified")
  63. self.assertEqual(len(mail.outbox), 0)
  64. @patch("hc.api.transports.requests.request")
  65. def test_pd(self, mock_post):
  66. self._setup_data("pd", "123")
  67. mock_post.return_value.status_code = 200
  68. self.channel.notify(self.check)
  69. assert Notification.objects.count() == 1
  70. args, kwargs = mock_post.call_args
  71. json = kwargs["json"]
  72. self.assertEqual(json["event_type"], "trigger")
  73. @patch("hc.api.transports.requests.request")
  74. def test_slack(self, mock_post):
  75. self._setup_data("slack", "123")
  76. mock_post.return_value.status_code = 200
  77. self.channel.notify(self.check)
  78. assert Notification.objects.count() == 1
  79. args, kwargs = mock_post.call_args
  80. json = kwargs["json"]
  81. attachment = json["attachments"][0]
  82. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  83. self.assertEqual(fields["Last Ping"], "Never")
  84. @patch("hc.api.transports.requests.request")
  85. def test_slack_handles_500(self, mock_post):
  86. self._setup_data("slack", "123")
  87. mock_post.return_value.status_code = 500
  88. self.channel.notify(self.check)
  89. n = Notification.objects.get()
  90. self.assertEqual(n.error, "Received status code 500")
  91. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  92. def test_slack_handles_timeout(self, mock_post):
  93. self._setup_data("slack", "123")
  94. self.channel.notify(self.check)
  95. n = Notification.objects.get()
  96. self.assertEqual(n.error, "Connection timed out")
  97. @patch("hc.api.transports.requests.request")
  98. def test_hipchat(self, mock_post):
  99. self._setup_data("hipchat", "123")
  100. mock_post.return_value.status_code = 204
  101. self.channel.notify(self.check)
  102. n = Notification.objects.first()
  103. self.assertEqual(n.error, "")
  104. args, kwargs = mock_post.call_args
  105. json = kwargs["json"]
  106. self.assertIn("DOWN", json["message"])
  107. @patch("hc.api.transports.requests.request")
  108. def test_pushover(self, mock_post):
  109. self._setup_data("po", "123|0")
  110. mock_post.return_value.status_code = 200
  111. self.channel.notify(self.check)
  112. assert Notification.objects.count() == 1
  113. args, kwargs = mock_post.call_args
  114. json = kwargs["data"]
  115. self.assertIn("DOWN", json["title"])
  116. @patch("hc.api.transports.requests.request")
  117. def test_victorops(self, mock_post):
  118. self._setup_data("victorops", "123")
  119. mock_post.return_value.status_code = 200
  120. self.channel.notify(self.check)
  121. assert Notification.objects.count() == 1
  122. args, kwargs = mock_post.call_args
  123. json = kwargs["json"]
  124. self.assertEqual(json["message_type"], "CRITICAL")