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.

241 lines
8.2 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. mock_get.assert_called_with(
  64. "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5)
  65. @patch("hc.api.transports.requests.request")
  66. def test_webhooks_dollarsign_escaping(self, mock_get):
  67. # If name or tag contains what looks like a variable reference,
  68. # that should be left alone:
  69. template = "http://host/$NAME"
  70. self._setup_data("webhook", template)
  71. self.check.name = "$TAG1"
  72. self.check.tags = "foo"
  73. self.check.save()
  74. self.channel.notify(self.check)
  75. url = u"http://host/%24TAG1"
  76. mock_get.assert_called_with(
  77. "get", url, headers={"User-Agent": "healthchecks.io"}, timeout=5)
  78. @patch("hc.api.transports.requests.request")
  79. def test_webhook_fires_on_up_event(self, mock_get):
  80. self._setup_data("webhook", "http://foo\nhttp://bar", status="up")
  81. self.channel.notify(self.check)
  82. mock_get.assert_called_with(
  83. "get", "http://bar", headers={"User-Agent": "healthchecks.io"},
  84. timeout=5)
  85. def test_email(self):
  86. self._setup_data("email", "[email protected]")
  87. self.channel.notify(self.check)
  88. n = Notification.objects.get()
  89. self.assertEqual(n.error, "")
  90. # And email should have been sent
  91. self.assertEqual(len(mail.outbox), 1)
  92. def test_it_skips_unverified_email(self):
  93. self._setup_data("email", "[email protected]", email_verified=False)
  94. self.channel.notify(self.check)
  95. assert Notification.objects.count() == 1
  96. n = Notification.objects.first()
  97. self.assertEqual(n.error, "Email not verified")
  98. self.assertEqual(len(mail.outbox), 0)
  99. @override_settings(USE_PAYMENTS=True)
  100. def test_email_contains_upgrade_notice(self):
  101. self._setup_data("email", "[email protected]", status="up")
  102. self.profile.team_access_allowed = False
  103. self.profile.save()
  104. self.channel.notify(self.check)
  105. n = Notification.objects.get()
  106. self.assertEqual(n.error, "")
  107. # Check is up, payments are enabled, and the user does not have team
  108. # access: the email should contain upgrade note
  109. message = mail.outbox[0]
  110. html, _ = message.alternatives[0]
  111. assert "/pricing/" in html
  112. @patch("hc.api.transports.requests.request")
  113. def test_pd(self, mock_post):
  114. self._setup_data("pd", "123")
  115. mock_post.return_value.status_code = 200
  116. self.channel.notify(self.check)
  117. assert Notification.objects.count() == 1
  118. args, kwargs = mock_post.call_args
  119. json = kwargs["json"]
  120. self.assertEqual(json["event_type"], "trigger")
  121. @patch("hc.api.transports.requests.request")
  122. def test_slack(self, mock_post):
  123. self._setup_data("slack", "123")
  124. mock_post.return_value.status_code = 200
  125. self.channel.notify(self.check)
  126. assert Notification.objects.count() == 1
  127. args, kwargs = mock_post.call_args
  128. json = kwargs["json"]
  129. attachment = json["attachments"][0]
  130. fields = {f["title"]: f["value"] for f in attachment["fields"]}
  131. self.assertEqual(fields["Last Ping"], "Never")
  132. @patch("hc.api.transports.requests.request")
  133. def test_slack_with_complex_value(self, mock_post):
  134. v = json.dumps({"incoming_webhook": {"url": "123"}})
  135. self._setup_data("slack", v)
  136. mock_post.return_value.status_code = 200
  137. self.channel.notify(self.check)
  138. assert Notification.objects.count() == 1
  139. args, kwargs = mock_post.call_args
  140. self.assertEqual(args[1], "123")
  141. @patch("hc.api.transports.requests.request")
  142. def test_slack_handles_500(self, mock_post):
  143. self._setup_data("slack", "123")
  144. mock_post.return_value.status_code = 500
  145. self.channel.notify(self.check)
  146. n = Notification.objects.get()
  147. self.assertEqual(n.error, "Received status code 500")
  148. @patch("hc.api.transports.requests.request", side_effect=Timeout)
  149. def test_slack_handles_timeout(self, mock_post):
  150. self._setup_data("slack", "123")
  151. self.channel.notify(self.check)
  152. n = Notification.objects.get()
  153. self.assertEqual(n.error, "Connection timed out")
  154. @patch("hc.api.transports.requests.request")
  155. def test_hipchat(self, mock_post):
  156. self._setup_data("hipchat", "123")
  157. mock_post.return_value.status_code = 204
  158. self.channel.notify(self.check)
  159. n = Notification.objects.first()
  160. self.assertEqual(n.error, "")
  161. args, kwargs = mock_post.call_args
  162. json = kwargs["json"]
  163. self.assertIn("DOWN", json["message"])
  164. @patch("hc.api.transports.requests.request")
  165. def test_pushover(self, mock_post):
  166. self._setup_data("po", "123|0")
  167. mock_post.return_value.status_code = 200
  168. self.channel.notify(self.check)
  169. assert Notification.objects.count() == 1
  170. args, kwargs = mock_post.call_args
  171. json = kwargs["data"]
  172. self.assertIn("DOWN", json["title"])
  173. @patch("hc.api.transports.requests.request")
  174. def test_victorops(self, mock_post):
  175. self._setup_data("victorops", "123")
  176. mock_post.return_value.status_code = 200
  177. self.channel.notify(self.check)
  178. assert Notification.objects.count() == 1
  179. args, kwargs = mock_post.call_args
  180. json = kwargs["json"]
  181. self.assertEqual(json["message_type"], "CRITICAL")