From c2e00f21052b92a2ce07d668631bc046b0ac2c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 25 Aug 2021 23:45:41 +0300 Subject: [PATCH] Fix assign_all_checks() usage, add tests for it --- hc/front/tests/test_add_email.py | 8 ++++++-- hc/front/tests/test_add_webhook.py | 8 ++++++-- hc/front/tests/test_edit_email.py | 9 +++++++-- hc/front/tests/test_edit_webhook.py | 9 +++++++-- hc/front/views.py | 4 +++- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/hc/front/tests/test_add_email.py b/hc/front/tests/test_add_email.py index 05e35f02..65be61d9 100644 --- a/hc/front/tests/test_add_email.py +++ b/hc/front/tests/test_add_email.py @@ -3,14 +3,15 @@ import json from django.core import mail from django.test.utils import override_settings -from hc.api.models import Channel +from hc.api.models import Channel, Check from hc.test import BaseTestCase class AddEmailTestCase(BaseTestCase): def setUp(self): super().setUp() - self.url = "/projects/%s/add_email/" % self.project.code + self.check = Check.objects.create(project=self.project) + self.url = f"/projects/{self.project.code}/add_email/" def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") @@ -41,6 +42,9 @@ class AddEmailTestCase(BaseTestCase): # Make sure we're sending to an email address, not a JSON string: self.assertEqual(email.to[0], "dan@example.org") + # Make sure it calls assign_all_checks + self.assertEqual(c.checks.count(), 1) + def test_team_access_works(self): form = {"value": "bob@example.org", "down": "true", "up": "true"} diff --git a/hc/front/tests/test_add_webhook.py b/hc/front/tests/test_add_webhook.py index 53c5bef6..fdf58404 100644 --- a/hc/front/tests/test_add_webhook.py +++ b/hc/front/tests/test_add_webhook.py @@ -1,12 +1,13 @@ from django.test.utils import override_settings -from hc.api.models import Channel +from hc.api.models import Channel, Check from hc.test import BaseTestCase class AddWebhookTestCase(BaseTestCase): def setUp(self): super().setUp() - self.url = "/projects/%s/add_webhook/" % self.project.code + self.check = Check.objects.create(project=self.project) + self.url = f"/projects/{self.project.code}/add_webhook/" def test_instructions_work(self): self.client.login(username="alice@example.org", password="password") @@ -29,6 +30,9 @@ class AddWebhookTestCase(BaseTestCase): c = Channel.objects.get() self.assertEqual(c.name, "Call foo.com") + # Make sure it calls assign_all_checks + self.assertEqual(c.checks.count(), 1) + def test_it_adds_two_webhook_urls_and_redirects(self): form = { "method_down": "GET", diff --git a/hc/front/tests/test_edit_email.py b/hc/front/tests/test_edit_email.py index d17401b1..5424c999 100644 --- a/hc/front/tests/test_edit_email.py +++ b/hc/front/tests/test_edit_email.py @@ -3,7 +3,7 @@ import json from django.core import mail from django.test.utils import override_settings -from hc.api.models import Channel +from hc.api.models import Channel, Check from hc.test import BaseTestCase @@ -11,6 +11,8 @@ class EditEmailTestCase(BaseTestCase): def setUp(self): super().setUp() + self.check = Check.objects.create(project=self.project) + self.channel = Channel(project=self.project, kind="email") self.channel.value = json.dumps( {"value": "alerts@example.org", "up": True, "down": True} @@ -18,7 +20,7 @@ class EditEmailTestCase(BaseTestCase): self.channel.email_verified = True self.channel.save() - self.url = "/integrations/%s/edit/" % self.channel.code + self.url = f"/integrations/{self.channel.code}/edit/" def test_it_shows_form(self): self.client.login(username="alice@example.org", password="password") @@ -44,6 +46,9 @@ class EditEmailTestCase(BaseTestCase): self.assertTrue(email.subject.startswith("Verify email address on")) self.assertEqual(email.to[0], "new@example.org") + # Make sure it does not call assign_all_checks + self.assertFalse(self.channel.checks.exists()) + def test_it_skips_verification_if_email_unchanged(self): form = {"value": "alerts@example.org", "down": "false", "up": "true"} diff --git a/hc/front/tests/test_edit_webhook.py b/hc/front/tests/test_edit_webhook.py index 1c71e7a3..34907d21 100644 --- a/hc/front/tests/test_edit_webhook.py +++ b/hc/front/tests/test_edit_webhook.py @@ -1,6 +1,6 @@ import json -from hc.api.models import Channel +from hc.api.models import Channel, Check from hc.test import BaseTestCase @@ -8,6 +8,8 @@ class EditWebhookTestCase(BaseTestCase): def setUp(self): super().setUp() + self.check = Check.objects.create(project=self.project) + definition = { "method_down": "GET", "url_down": "http://example.org/down", @@ -24,7 +26,7 @@ class EditWebhookTestCase(BaseTestCase): self.channel.value = json.dumps(definition) self.channel.save() - self.url = "/integrations/%s/edit/" % self.channel.code + self.url = f"/integrations/{self.channel.code}/edit/" def test_it_shows_form(self): self.client.login(username="alice@example.org", password="password") @@ -74,6 +76,9 @@ class EditWebhookTestCase(BaseTestCase): self.assertEqual(up_spec["body"], "going up") self.assertEqual(up_spec["headers"], {"Content-Type": "text/plain"}) + # Make sure it does not call assign_all_checks + self.assertFalse(self.channel.checks.exists()) + def test_it_requires_kind_webhook(self): self.channel.kind = "sms" self.channel.value = "foo@example.org" diff --git a/hc/front/views.py b/hc/front/views.py index f1fe1720..25878d42 100644 --- a/hc/front/views.py +++ b/hc/front/views.py @@ -1031,7 +1031,9 @@ def webhook_form(request, channel=None, code=None): channel.value = form.get_value() channel.save() - channel.assign_all_checks() + if is_new: + channel.assign_all_checks() + return redirect("hc-channels", channel.project.code) elif is_new: