Fix "Send Test Notification" for webhooks that only fire on checks going up

This commit is contained in:
Pēteris Caune 2019-12-27 14:36:32 +02:00
parent be286518b7
commit 6ebae33579
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
4 changed files with 57 additions and 1 deletions

View File

@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file.
- Fix List-Unsubscribe email header value: add angle brackets - Fix List-Unsubscribe email header value: add angle brackets
- Unsubscribe links serve a form, and require HTTP POST to actually unsubscribe - Unsubscribe links serve a form, and require HTTP POST to actually unsubscribe
- For webhook integration, validate each header line separately - For webhook integration, validate each header line separately
- Fix "Send Test Notification" for webhooks that only fire on checks going up
## v1.11.0 - 2019-11-22 ## v1.11.0 - 2019-11-22

View File

@ -221,7 +221,8 @@ class Webhook(HttpTransport):
def notify(self, check): def notify(self, check):
spec = self.channel.webhook_spec(check.status) spec = self.channel.webhook_spec(check.status)
assert spec["url"] if not spec["url"]:
return "Empty webhook URL"
url = self.prepare(spec["url"], check, urlencode=True) url = self.prepare(spec["url"], check, urlencode=True)
headers = {} headers = {}

View File

@ -1,6 +1,9 @@
import json
from django.core import mail from django.core import mail
from hc.api.models import Channel from hc.api.models import Channel
from hc.test import BaseTestCase from hc.test import BaseTestCase
from mock import patch
class SendTestNotificationTestCase(BaseTestCase): class SendTestNotificationTestCase(BaseTestCase):
@ -27,3 +30,48 @@ class SendTestNotificationTestCase(BaseTestCase):
self.assertEqual(email.to[0], "alice@example.org") self.assertEqual(email.to[0], "alice@example.org")
self.assertTrue("X-Bounce-Url" in email.extra_headers) self.assertTrue("X-Bounce-Url" in email.extra_headers)
self.assertTrue("List-Unsubscribe" in email.extra_headers) self.assertTrue("List-Unsubscribe" in email.extra_headers)
@patch("hc.api.transports.requests.request")
def test_it_handles_webhooks_with_no_down_url(self, mock_get):
mock_get.return_value.status_code = 200
self.channel.kind = "webhook"
self.channel.value = json.dumps(
{
"method_down": "GET",
"url_down": "",
"body_down": "",
"headers_down": {},
"method_up": "GET",
"url_up": "http://example-url",
"body_up": "",
"headers_up": {},
}
)
self.channel.save()
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {}, follow=True)
self.assertRedirects(r, "/integrations/")
self.assertContains(r, "Test notification sent!")
def test_it_handles_webhooks_with_no_urls(self):
self.channel.kind = "webhook"
self.channel.value = json.dumps(
{
"method_down": "GET",
"url_down": "",
"body_down": "",
"headers_down": {},
"method_up": "GET",
"url_up": "",
"body_up": "",
"headers_up": {},
}
)
self.channel.save()
self.client.login(username="alice@example.org", password="password")
r = self.client.post(self.url, {}, follow=True)
self.assertRedirects(r, "/integrations/")
self.assertContains(r, "Could not send a test notification")

View File

@ -747,6 +747,12 @@ def send_test_notification(request, code):
dummy.last_ping = timezone.now() - td(days=1) dummy.last_ping = timezone.now() - td(days=1)
dummy.n_pings = 42 dummy.n_pings = 42
if channel.kind == "webhook" and not channel.url_down:
if channel.url_up:
# If we don't have url_down, but do have have url_up then
# send "TEST is UP" notification instead:
dummy.status = "up"
if channel.kind == "email": if channel.kind == "email":
error = channel.transport.notify(dummy, channel.get_unsub_link()) error = channel.transport.notify(dummy, channel.get_unsub_link())
else: else: