|
|
@ -5,7 +5,6 @@ import json |
|
|
|
|
|
|
|
from django.core import mail |
|
|
|
from django.utils.timezone import now |
|
|
|
from hc.api.transports import Transport |
|
|
|
from hc.api.models import Channel, Check, Notification |
|
|
|
from hc.test import BaseTestCase |
|
|
|
from mock import patch |
|
|
@ -63,14 +62,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
self.assertFalse(mock_get.called) |
|
|
|
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") |
|
|
|
def test_webhooks_handle_500(self, mock_get): |
|
|
|
self._setup_data("webhook", "http://example") |
|
|
@ -155,20 +146,44 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
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() |
|
|
|
def test_webhooks_handle_json_value(self, mock_request): |
|
|
|
self._setup_data("webhook", '{"url_down": "http://foo.com", ' |
|
|
|
'"url_up": "", "post_data": "", "headers": ""}') |
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
headers = { |
|
|
|
"User-Agent": "healthchecks.io", |
|
|
|
"Content-Type": "application/json" |
|
|
|
"User-Agent": "healthchecks.io" |
|
|
|
} |
|
|
|
mock_request.assert_called_with( |
|
|
|
"get", "http://foo.com", headers=headers, |
|
|
|
timeout=5) |
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
def test_webhooks_handle_json_up_event(self, mock_request): |
|
|
|
self._setup_data("webhook", '{"url_down": "", ' |
|
|
|
'"url_up": "http://bar", "post_data": "", "headers": ""}', status="up") |
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
headers = { |
|
|
|
"User-Agent": "healthchecks.io" |
|
|
|
} |
|
|
|
mock_request.assert_called_with( |
|
|
|
"get", "http://bar", headers=headers, |
|
|
|
timeout=5) |
|
|
|
|
|
|
|
@patch("hc.api.transports.requests.request") |
|
|
|
def test_webhooks_handle_headers(self, mock_request): |
|
|
|
self._setup_data("webhook", '{"url_down": "http://foo.com", ' |
|
|
|
'"url_up": "", "post_data": "data", "headers": ' |
|
|
|
'"{\\\"Content-Type\\\": \\\"application/json\\\"}"}') |
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
headers = { |
|
|
|
"User-Agent": "healthchecks.io", |
|
|
|
"Content-Type": "application/json" |
|
|
|
} |
|
|
|
mock_request.assert_called_with( |
|
|
|
"post", "http://example.com", data=b"{}", headers=headers, timeout=5) |
|
|
|
"post", "http://foo.com", data=b"data", headers=headers, timeout=5) |
|
|
|
|
|
|
|
def test_email(self): |
|
|
|
self._setup_data("email", "[email protected]") |
|
|
@ -192,17 +207,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
self.assertEqual(n.error, "Email not verified") |
|
|
|
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") |
|
|
|
def test_pd(self, mock_post): |
|
|
|
self._setup_data("pd", "123") |
|
|
@ -312,21 +316,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
payload = kwargs["json"] |
|
|
|
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") |
|
|
|
def test_pushover(self, mock_post): |
|
|
|
self._setup_data("po", "123|0") |
|
|
@ -338,22 +327,6 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
payload = kwargs["data"] |
|
|
|
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") |
|
|
|
def test_victorops(self, mock_post): |
|
|
@ -454,8 +427,3 @@ class NotifyTestCase(BaseTestCase): |
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
self.assertTrue(mock_post.called) |
|
|
|
|
|
|
|
def test_transport_notify(self): |
|
|
|
self._setup_data("webhook", "http://example") |
|
|
|
with self.assertRaises(NotImplementedError): |
|
|
|
Transport(self.channel).notify(self.check) |