From 38ed309a3c0cedfed75ce6a4cd6ceee3348f883a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 27 Dec 2019 17:13:44 +0200 Subject: [PATCH] Don't allow adding webhook integrations with both URLs blank --- CHANGELOG.md | 1 + hc/front/forms.py | 9 +++++++++ hc/front/tests/test_add_webhook.py | 15 +++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c2169a0..97b4886e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file. - Unsubscribe links serve a form, and require HTTP POST to actually unsubscribe - For webhook integration, validate each header line separately - Fix "Send Test Notification" for webhooks that only fire on checks going up +- Don't allow adding webhook integrations with both URLs blank ## v1.11.0 - 2019-11-22 diff --git a/hc/front/forms.py b/hc/front/forms.py index 70f3b4c7..c9c86c9d 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -122,6 +122,15 @@ class AddWebhookForm(forms.Form): max_length=1000, required=False, validators=[WebhookValidator()] ) + def clean(self): + super().clean() + + url_down = self.cleaned_data.get("url_down") + url_up = self.cleaned_data.get("url_up") + + if not url_down and not url_up: + self.add_error("url_down", "Enter a valid URL.") + def get_value(self): return json.dumps(dict(self.cleaned_data), sort_keys=True) diff --git a/hc/front/tests/test_add_webhook.py b/hc/front/tests/test_add_webhook.py index 7e231db2..405f3b1a 100644 --- a/hc/front/tests/test_add_webhook.py +++ b/hc/front/tests/test_add_webhook.py @@ -144,3 +144,18 @@ class AddWebhookTestCase(BaseTestCase): c = Channel.objects.get() self.assertEqual(c.down_webhook_spec["headers"], {"test": "123"}) + + def test_it_rejects_both_empty(self): + + self.client.login(username="alice@example.org", password="password") + form = { + "method_down": "GET", + "url_down": "", + "method_up": "GET", + "url_up": "", + } + + r = self.client.post(self.url, form) + self.assertContains(r, "Enter a valid URL.") + + self.assertEqual(Channel.objects.count(), 0)