forked from GithubBackups/healthchecks
More tests for notifications.
This commit is contained in:
parent
21a042aa16
commit
90d7806783
@ -65,7 +65,7 @@ class Check(models.Model):
|
||||
|
||||
def send_alert(self):
|
||||
if self.status not in ("up", "down"):
|
||||
raise NotImplemented("Unexpected status: %s" % self.status)
|
||||
raise NotImplementedError("Unexpected status: %s" % self.status)
|
||||
|
||||
for channel in self.channel_set.all():
|
||||
channel.notify(self)
|
||||
@ -138,7 +138,7 @@ class Channel(models.Model):
|
||||
elif self.kind == "po":
|
||||
return transports.Pushover()
|
||||
else:
|
||||
raise NotImplemented("Unknown channel kind: %s" % self.kind)
|
||||
raise NotImplementedError("Unknown channel kind: %s" % self.kind)
|
||||
|
||||
def notify(self, check):
|
||||
# Make 3 attempts--
|
||||
|
@ -8,14 +8,14 @@ from hc.test import BaseTestCase
|
||||
|
||||
class NotifyTestCase(BaseTestCase):
|
||||
|
||||
def _setup_data(self, channel_kind, channel_value, email_verified=True):
|
||||
def _setup_data(self, kind, value, status="down", email_verified=True):
|
||||
self.check = Check()
|
||||
self.check.status = "down"
|
||||
self.check.status = status
|
||||
self.check.save()
|
||||
|
||||
self.channel = Channel(user=self.alice)
|
||||
self.channel.kind = channel_kind
|
||||
self.channel.value = channel_value
|
||||
self.channel.kind = kind
|
||||
self.channel.value = value
|
||||
self.channel.email_verified = email_verified
|
||||
self.channel.save()
|
||||
self.channel.checks.add(self.check)
|
||||
@ -38,6 +38,26 @@ class NotifyTestCase(BaseTestCase):
|
||||
n = Notification.objects.get()
|
||||
self.assertEqual(n.error, "Connection timed out")
|
||||
|
||||
@patch("hc.api.transports.requests.get")
|
||||
def test_webhooks_ignore_up_events(self, mock_get):
|
||||
self._setup_data("webhook", "http://example", status="up")
|
||||
self.channel.notify(self.check)
|
||||
|
||||
self.assertFalse(mock_get.called)
|
||||
|
||||
n = Notification.objects.get()
|
||||
self.assertEqual(n.error, "")
|
||||
|
||||
@patch("hc.api.transports.requests.get")
|
||||
def test_webhooks_handle_500(self, mock_get):
|
||||
self._setup_data("webhook", "http://example")
|
||||
mock_get.return_value.status_code = 500
|
||||
|
||||
self.channel.notify(self.check)
|
||||
|
||||
n = Notification.objects.get()
|
||||
self.assertEqual(n.error, "Received status code 500")
|
||||
|
||||
def test_email(self):
|
||||
self._setup_data("email", "alice@example.org")
|
||||
self.channel.notify(self.check)
|
||||
@ -82,3 +102,25 @@ class NotifyTestCase(BaseTestCase):
|
||||
attachment = json["attachments"][0]
|
||||
fields = {f["title"]: f["value"] for f in attachment["fields"]}
|
||||
self.assertEqual(fields["Last Ping"], "Never")
|
||||
|
||||
@patch("hc.api.transports.requests.post")
|
||||
def test_slack_handles_500(self, mock_post):
|
||||
self._setup_data("slack", "123")
|
||||
mock_post.return_value.status_code = 500
|
||||
|
||||
self.channel.notify(self.check)
|
||||
|
||||
n = Notification.objects.get()
|
||||
self.assertEqual(n.error, "Received status code 500")
|
||||
|
||||
@patch("hc.api.transports.requests.post")
|
||||
def test_hipchat(self, mock_post):
|
||||
self._setup_data("hipchat", "123")
|
||||
mock_post.return_value.status_code = 200
|
||||
|
||||
self.channel.notify(self.check)
|
||||
assert Notification.objects.count() == 1
|
||||
|
||||
args, kwargs = mock_post.call_args
|
||||
json = kwargs["json"]
|
||||
self.assertIn("DOWN", json["message"])
|
||||
|
@ -24,7 +24,7 @@ class Transport(object):
|
||||
|
||||
"""
|
||||
|
||||
raise NotImplemented()
|
||||
raise NotImplementedError()
|
||||
|
||||
def test(self):
|
||||
""" Send test message.
|
||||
@ -34,7 +34,7 @@ class Transport(object):
|
||||
|
||||
"""
|
||||
|
||||
raise NotImplemented()
|
||||
raise NotImplementedError()
|
||||
|
||||
def checks(self):
|
||||
return self.channel.user.check_set.order_by("created")
|
||||
|
Loading…
x
Reference in New Issue
Block a user