|
@ -5,6 +5,7 @@ import json |
|
|
|
|
|
|
|
|
from django.core import mail |
|
|
from django.core import mail |
|
|
from django.utils.timezone import now |
|
|
from django.utils.timezone import now |
|
|
|
|
|
from hc.api.transports import Transport |
|
|
from hc.api.models import Channel, Check, Notification |
|
|
from hc.api.models import Channel, Check, Notification |
|
|
from hc.test import BaseTestCase |
|
|
from hc.test import BaseTestCase |
|
|
from mock import patch |
|
|
from mock import patch |
|
@ -62,6 +63,14 @@ class NotifyTestCase(BaseTestCase): |
|
|
self.assertFalse(mock_get.called) |
|
|
self.assertFalse(mock_get.called) |
|
|
self.assertEqual(Notification.objects.count(), 0) |
|
|
self.assertEqual(Notification.objects.count(), 0) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_webhooks_ignore_down_events(self, mock_get): |
|
|
|
|
|
self._setup_data("webhook", "\nhttp://example", status="down") |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
|
|
|
self.assertFalse(mock_get.called) |
|
|
|
|
|
self.assertEqual(Notification.objects.count(), 0) |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_webhooks_handle_500(self, mock_get): |
|
|
def test_webhooks_handle_500(self, mock_get): |
|
|
self._setup_data("webhook", "http://example") |
|
|
self._setup_data("webhook", "http://example") |
|
@ -145,6 +154,22 @@ class NotifyTestCase(BaseTestCase): |
|
|
# unicode should be encoded into utf-8 |
|
|
# unicode should be encoded into utf-8 |
|
|
self.assertTrue(isinstance(kwargs["data"], binary_type)) |
|
|
self.assertTrue(isinstance(kwargs["data"], binary_type)) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_webhooks_handle_content_type(self, mock_request): |
|
|
|
|
|
template = u"http://example.com\n\n{}\napplication/json" |
|
|
|
|
|
self._setup_data("webhook", template) |
|
|
|
|
|
self.check.save() |
|
|
|
|
|
|
|
|
|
|
|
headers = { |
|
|
|
|
|
"User-Agent": "healthchecks.io", |
|
|
|
|
|
"Content-Type": "application/json" |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
|
|
|
mock_request.assert_called_with( |
|
|
|
|
|
"post", "http://example.com", data=b"{}", headers=headers, timeout=5) |
|
|
|
|
|
|
|
|
def test_email(self): |
|
|
def test_email(self): |
|
|
self._setup_data("email", "[email protected]") |
|
|
self._setup_data("email", "[email protected]") |
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
@ -167,6 +192,17 @@ class NotifyTestCase(BaseTestCase): |
|
|
self.assertEqual(n.error, "Email not verified") |
|
|
self.assertEqual(n.error, "Email not verified") |
|
|
self.assertEqual(len(mail.outbox), 0) |
|
|
self.assertEqual(len(mail.outbox), 0) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.emails.alert") |
|
|
|
|
|
def test_email_missing_profile(self, mock_emails): |
|
|
|
|
|
self._setup_data("email", "[email protected]") |
|
|
|
|
|
self.profile.sort = "name" |
|
|
|
|
|
self.profile.save() |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_emails.call_args |
|
|
|
|
|
self.assertEqual(args[0], "[email protected]") |
|
|
|
|
|
self.assertEqual(args[1]["sort"], "created") |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_pd(self, mock_post): |
|
|
def test_pd(self, mock_post): |
|
|
self._setup_data("pd", "123") |
|
|
self._setup_data("pd", "123") |
|
@ -276,6 +312,21 @@ class NotifyTestCase(BaseTestCase): |
|
|
payload = kwargs["json"] |
|
|
payload = kwargs["json"] |
|
|
self.assertIn("DOWN", payload["message"]) |
|
|
self.assertIn("DOWN", payload["message"]) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_opsgenie_up(self, mock_post): |
|
|
|
|
|
self._setup_data("opsgenie", "123", status="up") |
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
n = Notification.objects.first() |
|
|
|
|
|
self.assertEqual(n.error, "") |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
|
|
payload = kwargs["json"] |
|
|
|
|
|
self.assertEqual(args[0], "post") |
|
|
|
|
|
self.assertTrue(args[1].endswith("/close")) |
|
|
|
|
|
self.assertNotIn("message", payload) |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_pushover(self, mock_post): |
|
|
def test_pushover(self, mock_post): |
|
|
self._setup_data("po", "123|0") |
|
|
self._setup_data("po", "123|0") |
|
@ -287,6 +338,22 @@ class NotifyTestCase(BaseTestCase): |
|
|
args, kwargs = mock_post.call_args |
|
|
args, kwargs = mock_post.call_args |
|
|
payload = kwargs["data"] |
|
|
payload = kwargs["data"] |
|
|
self.assertIn("DOWN", payload["title"]) |
|
|
self.assertIn("DOWN", payload["title"]) |
|
|
|
|
|
self.assertNotIn("retry", payload) |
|
|
|
|
|
self.assertNotIn("expire", payload) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
|
|
def test_pushover_emergency(self, mock_post): |
|
|
|
|
|
self._setup_data("po", "123|2") |
|
|
|
|
|
mock_post.return_value.status_code = 200 |
|
|
|
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
assert Notification.objects.count() == 1 |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
|
|
payload = kwargs["data"] |
|
|
|
|
|
self.assertIn("DOWN", payload["title"]) |
|
|
|
|
|
self.assertIn("retry", payload) |
|
|
|
|
|
self.assertIn("expire", payload) |
|
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
@patch("hc.api.transports.requests.request") |
|
|
def test_victorops(self, mock_post): |
|
|
def test_victorops(self, mock_post): |
|
@ -387,3 +454,8 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
self.channel.notify(self.check) |
|
|
self.assertTrue(mock_post.called) |
|
|
self.assertTrue(mock_post.called) |
|
|
|
|
|
|
|
|
|
|
|
def test_transport_notify(self): |
|
|
|
|
|
self._setup_data("webhook", "http://example") |
|
|
|
|
|
with self.assertRaises(NotImplementedError): |
|
|
|
|
|
Transport.notify(self.channel, self.check) |