From 7240ce9ba8bdace27c6960ad0d4f2ab0357f3516 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 14 Aug 2015 12:37:02 +0300 Subject: [PATCH] Fix bug with pinging webhook --- .travis.yml | 2 +- hc/api/models.py | 2 +- hc/api/tests/test_notify.py | 24 ++++++++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 hc/api/tests/test_notify.py diff --git a/.travis.yml b/.travis.yml index da576b87..a01d93a2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ python: - "3.4" install: - pip install -r requirements.txt - - pip install coveralls mysqlclient + - pip install coveralls mock mysqlclient env: - DB=sqlite - DB=mysql diff --git a/hc/api/models.py b/hc/api/models.py index 71c39584..8dd38195 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -117,7 +117,7 @@ class Channel(models.Model): } emails.alert(self.value, ctx) n.save() - elif self.kind == "webhook" and self.status == "down": + elif self.kind == "webhook" and check.status == "down": r = requests.get(self.value) n.status = r.status_code n.save() diff --git a/hc/api/tests/test_notify.py b/hc/api/tests/test_notify.py new file mode 100644 index 00000000..8d5fd745 --- /dev/null +++ b/hc/api/tests/test_notify.py @@ -0,0 +1,24 @@ +from django.contrib.auth.models import User +from django.test import TestCase +from mock import patch + +from hc.api.models import Channel, Check + + +class NotifyTestCase(TestCase): + + @patch("hc.api.models.requests") + def test_webhook(self, mock_requests): + alice = User(username="alice") + alice.save() + + check = Check() + check.status = "down" + check.save() + + channel = Channel(user=alice, kind="webhook", value="http://example") + channel.save() + channel.checks.add(check) + + channel.notify(check) + mock_requests.get.assert_called_with(u"http://example")