From 90d78067837d14595e044ff8c897b2a8d8a65aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Sun, 31 Jan 2016 18:54:35 +0200 Subject: [PATCH] More tests for notifications. --- hc/api/models.py | 4 +-- hc/api/tests/test_notify.py | 50 ++++++++++++++++++++++++++++++++++--- hc/api/transports.py | 4 +-- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index a281be46..7bf8d944 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -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-- diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py index 00c435af..7865e669 100644 --- a/hc/api/tests/test_notify.py +++ b/hc/api/tests/test_notify.py @@ -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"]) diff --git a/hc/api/transports.py b/hc/api/transports.py index 43a982f3..2421a9ad 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -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")