|
@ -1,4 +1,5 @@ |
|
|
from django.contrib.auth.models import User |
|
|
from django.contrib.auth.models import User |
|
|
|
|
|
from django.core import mail |
|
|
from django.test import TestCase |
|
|
from django.test import TestCase |
|
|
from mock import patch |
|
|
from mock import patch |
|
|
from requests.exceptions import ReadTimeout |
|
|
from requests.exceptions import ReadTimeout |
|
@ -8,35 +9,54 @@ from hc.api.models import Channel, Check, Notification |
|
|
|
|
|
|
|
|
class NotifyTestCase(TestCase): |
|
|
class NotifyTestCase(TestCase): |
|
|
|
|
|
|
|
|
@patch("hc.api.models.requests.get") |
|
|
|
|
|
def test_webhook(self, mock_get): |
|
|
|
|
|
alice = User(username="alice") |
|
|
|
|
|
alice.save() |
|
|
|
|
|
|
|
|
def _setup_data(self, channel_kind, channel_value, email_verified=True): |
|
|
|
|
|
self.alice = User(username="alice") |
|
|
|
|
|
self.alice.save() |
|
|
|
|
|
|
|
|
|
|
|
self.check = Check() |
|
|
|
|
|
self.check.status = "down" |
|
|
|
|
|
self.check.save() |
|
|
|
|
|
|
|
|
check = Check() |
|
|
|
|
|
check.status = "down" |
|
|
|
|
|
check.save() |
|
|
|
|
|
|
|
|
self.channel = Channel(user=self.alice) |
|
|
|
|
|
self.channel.kind = channel_kind |
|
|
|
|
|
self.channel.value = channel_value |
|
|
|
|
|
self.channel.email_verified = email_verified |
|
|
|
|
|
self.channel.save() |
|
|
|
|
|
self.channel.checks.add(self.check) |
|
|
|
|
|
|
|
|
channel = Channel(user=alice, kind="webhook", value="http://example") |
|
|
|
|
|
channel.save() |
|
|
|
|
|
channel.checks.add(check) |
|
|
|
|
|
|
|
|
@patch("hc.api.models.requests.get") |
|
|
|
|
|
def test_webhook(self, mock_get): |
|
|
|
|
|
self._setup_data("webhook", "http://example") |
|
|
|
|
|
|
|
|
channel.notify(check) |
|
|
|
|
|
|
|
|
self.channel.notify(self.check) |
|
|
mock_get.assert_called_with(u"http://example", timeout=5) |
|
|
mock_get.assert_called_with(u"http://example", timeout=5) |
|
|
|
|
|
|
|
|
@patch("hc.api.models.requests.get", side_effect=ReadTimeout) |
|
|
@patch("hc.api.models.requests.get", side_effect=ReadTimeout) |
|
|
def test_it_handles_requests_exceptions(self, mock_get): |
|
|
|
|
|
alice = User(username="alice") |
|
|
|
|
|
alice.save() |
|
|
|
|
|
|
|
|
def test_webhooks_handle_timeouts(self, mock_get): |
|
|
|
|
|
self._setup_data("webhook", "http://example") |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
assert Notification.objects.count() == 1 |
|
|
|
|
|
|
|
|
|
|
|
def test_email(self): |
|
|
|
|
|
self._setup_data("email", "[email protected]") |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
assert Notification.objects.count() == 1 |
|
|
|
|
|
|
|
|
check = Check() |
|
|
|
|
|
check.status = "down" |
|
|
|
|
|
check.save() |
|
|
|
|
|
|
|
|
# And email should have been sent |
|
|
|
|
|
self.assertEqual(len(mail.outbox), 1) |
|
|
|
|
|
|
|
|
channel = Channel(user=alice, kind="webhook", value="http://example") |
|
|
|
|
|
channel.save() |
|
|
|
|
|
channel.checks.add(check) |
|
|
|
|
|
|
|
|
def test_it_skips_unverified_email(self): |
|
|
|
|
|
self._setup_data("email", "[email protected]", email_verified=False) |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
|
|
|
|
|
|
channel.notify(check) |
|
|
|
|
|
|
|
|
assert Notification.objects.count() == 0 |
|
|
|
|
|
self.assertEqual(len(mail.outbox), 0) |
|
|
|
|
|
|
|
|
|
|
|
@patch("hc.api.models.requests.post") |
|
|
|
|
|
def test_pd(self, mock_post): |
|
|
|
|
|
self._setup_data("pd", "123") |
|
|
|
|
|
self.channel.notify(self.check) |
|
|
assert Notification.objects.count() == 1 |
|
|
assert Notification.objects.count() == 1 |
|
|
|
|
|
|
|
|
|
|
|
args, kwargs = mock_post.call_args |
|
|
|
|
|
assert "trigger" in kwargs["data"] |