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.

284 lines
9.8 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
  1. import json
  2. from django.core import mail
  3. from django.test import override_settings
  4. from hc.api.models import Channel, Check, Notification
  5. from hc.test import BaseTestCase
  6. from mock import patch
  7. from requests.exceptions import ConnectionError, Timeout
  8. class NotifyTestCase(BaseTestCase):
  9. def _setup_data(self, kind, value, status="down", email_verified=True):
  10. self.check = Check()
  11. self.check.status = status
  12. self.check.user = self.alice
  13. self.check.save()
  14. self.channel = Channel(user=self.alice)
  15. self.channel.kind = kind
  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_webhook(self, mock_get):
  22. self._setup_data("webhook", "http://example")
  23. mock_get.return_value.status_code = 200
  24. self.channel.notify(self.check)
  25. mock_get.assert_called_with(
  26. "get", u"http://example",
  27. headers={"User-Agent": "healthchecks.io"}, timeout=5)
  28. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  29. def test_webhooks_handle_timeouts(self, mock_get):
  30. self._setup_data("webhook", "http://example")
  31. self.channel.notify(self.check)
  32. n = Notification.objects.get()
  33. self.assertEqual(n.error, "Connection timed out")
  34. @patch("hc.api.transports.requests.request", side_effect=ConnectionError)
  35. def test_webhooks_handle_connection_errors(self, mock_get):
  36. self._setup_data("webhook", "http://example")
  37. self.channel.notify(self.check)
  38. n = Notification.objects.get()
  39. self.assertEqual(n.error, "Connection failed")
  40. @patch("hc.api.transports.requests.request")
  41. def test_webhooks_ignore_up_events(self, mock_get):
  42. self._setup_data("webhook", "http://example", status="up")
  43. self.channel.notify(self.check)
  44. self.assertFalse(mock_get.called)
  45. self.assertEqual(Notification.objects.count(), 0)
  46. @patch("hc.api.transports.requests.request")
  47. def test_webhooks_handle_500(self, mock_get):
  48. self._setup_data("webhook", "http://example")
  49. mock_get.return_value.status_code = 500
  50. self.channel.notify(self.check)
  51. n = Notification.objects.get()
  52. self.assertEqual(n.error, "Received status code 500")
  53. @patch("hc.api.transports.requests.request")
  54. def test_webhooks_support_variables(self, mock_get):
  55. template = "http://host/$CODE/$STATUS/$TAG1/$TAG2/?name=$NAME"
  56. self._setup_data("webhook", template)
  57. self.check.name = "Hello World"
  58. self.check.tags = "foo bar"
  59. self.check.save()
  60. self.channel.notify(self.check)
  61. url = u"http://host/%s/down/foo/bar/?name=Hello%%20World" \
  62. % self.check.code
  63. args, kwargs = mock_get.call_args
  64. self.assertEqual(args[0], "get")
  65. self.assertEqual(args[1], url)
  66. self.assertEqual(kwargs["headers"], {"User-Agent": "healthchecks.io"})
  67. self.assertEqual(kwargs["timeout"], 5)
  68. @patch("hc.api.transports.requests.request")
  69. def test_webhooks_support_post(self, mock_request):
  70. template = "http://example.com\n\nThe Time Is $NOW"
  71. self._setup_data("webhook", template)
  72. self.check.save()
  73. self.channel.notify(self.check)
  74. args, kwargs = mock_request.call_args
  75. self.assertEqual(args[0], "post")
  76. self.assertEqual(args[1], "http://example.com")
  77. # spaces should not have been urlencoded:
  78. self.assertTrue(kwargs["data"].startswith("The Time Is 2"))
  79. @patch("hc.api.transports.requests.request")
  80. def test_webhooks_dollarsign_escaping(self, mock_get):
  81. # If name or tag contains what looks like a variable reference,
  82. # that should be left alone:
  83. template = "http://host/$NAME"
  84. self._setup_data("webhook", template)
  85. self.check.name = "$TAG1"
  86. self.check.tags = "foo"
  87. self.check.save()
  88. self.channel.notify(self.check)
  89. url = u"http://host/%24TAG1"
  90. mock_get.assert_called_with(
  91. "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5)
  92. @patch("hc.api.transports.requests.request")
  93. def test_webhook_fires_on_up_event(self, mock_get):
  94. self._setup_data("webhook", "http://foo\nhttp://bar", status="up")
  95. self.channel.notify(self.check)
  96. mock_get.assert_called_with(
  97. "get", "http://bar", headers={"User-Agent": "healthchecks.io"},
  98. timeout=5)
  99. def test_email(self):
  100. self._setup_data("email", "[email protected]")
  101. self.channel.notify(self.check)
  102. n = Notification.objects.get()
  103. self.assertEqual(n.error, "")
  104. # And email should have been sent
  105. self.assertEqual(len(mail.outbox), 1)
  106. email = mail.outbox[0]
  107. self.assertTrue("X-Bounce-Url" in email.extra_headers)
  108. def test_it_skips_unverified_email(self):
  109. self._setup_data("email", "[email protected]", email_verified=False)
  110. self.channel.notify(self.check)
  111. assert Notification.objects.count() == 1
  112. n = Notification.objects.first()
  113. self.assertEqual(n.error, "Email not verified")
  114. self.assertEqual(len(mail.outbox), 0)
  115. @patch("hc.api.transports.requests.request")
  116. def test_pd(self, mock_post):
  117. self._setup_data("pd", "123")
  118. mock_post.return_value.status_code = 200
  119. self.channel.notify(self.check)
  120. assert Notification.objects.count() == 1
  121. args, kwargs = mock_post.call_args
  122. payload = kwargs["json"]
  123. self.assertEqual(payload["event_type"], "trigger")
  124. @patch("hc.api.transports.requests.request")
  125. def test_slack(self, mock_post):
  126. self._setup_data("slack", "123")
  127. mock_post.return_value.status_code = 200
  128. self.channel.notify(self.check)
  129. assert Notification.objects.count() == 1
  130. args, kwargs = mock_post.call_args
  131. payload = kwargs["json"]
  132. attachment = payload["attachments"][0]
  133. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  134. self.assertEqual(fields["Last Ping"], "Never")
  135. @patch("hc.api.transports.requests.request")
  136. def test_slack_with_complex_value(self, mock_post):
  137. v = json.dumps({"incoming_webhook": {"url": "123"}})
  138. self._setup_data("slack", v)
  139. mock_post.return_value.status_code = 200
  140. self.channel.notify(self.check)
  141. assert Notification.objects.count() == 1
  142. args, kwargs = mock_post.call_args
  143. self.assertEqual(args[1], "123")
  144. @patch("hc.api.transports.requests.request")
  145. def test_slack_handles_500(self, mock_post):
  146. self._setup_data("slack", "123")
  147. mock_post.return_value.status_code = 500
  148. self.channel.notify(self.check)
  149. n = Notification.objects.get()
  150. self.assertEqual(n.error, "Received status code 500")
  151. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  152. def test_slack_handles_timeout(self, mock_post):
  153. self._setup_data("slack", "123")
  154. self.channel.notify(self.check)
  155. n = Notification.objects.get()
  156. self.assertEqual(n.error, "Connection timed out")
  157. @patch("hc.api.transports.requests.request")
  158. def test_hipchat(self, mock_post):
  159. self._setup_data("hipchat", "123")
  160. mock_post.return_value.status_code = 204
  161. self.channel.notify(self.check)
  162. n = Notification.objects.first()
  163. self.assertEqual(n.error, "")
  164. args, kwargs = mock_post.call_args
  165. payload = kwargs["json"]
  166. self.assertIn("DOWN", payload["message"])
  167. @patch("hc.api.transports.requests.request")
  168. def test_opsgenie(self, mock_post):
  169. self._setup_data("opsgenie", "123")
  170. mock_post.return_value.status_code = 200
  171. self.channel.notify(self.check)
  172. n = Notification.objects.first()
  173. self.assertEqual(n.error, "")
  174. args, kwargs = mock_post.call_args
  175. payload = kwargs["json"]
  176. self.assertIn("DOWN", payload["message"])
  177. @patch("hc.api.transports.requests.request")
  178. def test_pushover(self, mock_post):
  179. self._setup_data("po", "123|0")
  180. mock_post.return_value.status_code = 200
  181. self.channel.notify(self.check)
  182. assert Notification.objects.count() == 1
  183. args, kwargs = mock_post.call_args
  184. payload = kwargs["data"]
  185. self.assertIn("DOWN", payload["title"])
  186. @patch("hc.api.transports.requests.request")
  187. def test_victorops(self, mock_post):
  188. self._setup_data("victorops", "123")
  189. mock_post.return_value.status_code = 200
  190. self.channel.notify(self.check)
  191. assert Notification.objects.count() == 1
  192. args, kwargs = mock_post.call_args
  193. payload = kwargs["json"]
  194. self.assertEqual(payload["message_type"], "CRITICAL")
  195. @patch("hc.api.transports.requests.request")
  196. def test_discord(self, mock_post):
  197. v = json.dumps({"webhook": {"url": "123"}})
  198. self._setup_data("discord", v)
  199. mock_post.return_value.status_code = 200
  200. self.channel.notify(self.check)
  201. assert Notification.objects.count() == 1
  202. args, kwargs = mock_post.call_args
  203. payload = kwargs["json"]
  204. attachment = payload["attachments"][0]
  205. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  206. self.assertEqual(fields["Last Ping"], "Never")
  207. @patch("hc.api.transports.requests.request")
  208. def test_pushbullet(self, mock_post):
  209. self._setup_data("pushbullet", "fake-token")
  210. mock_post.return_value.status_code = 200
  211. self.channel.notify(self.check)
  212. assert Notification.objects.count() == 1
  213. _, kwargs = mock_post.call_args
  214. self.assertEqual(kwargs["json"]["type"], "note")
  215. self.assertEqual(kwargs["headers"]["Access-Token"], "fake-token")