From 7534f1856fd6df883507764f05c0f47e21372dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 14 Oct 2020 18:12:35 +0300 Subject: [PATCH] Add testcases for setting channels in the "Create Check" API call --- hc/api/tests/test_create_check.py | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/hc/api/tests/test_create_check.py b/hc/api/tests/test_create_check.py index d57e7827..5ca4f4ab 100644 --- a/hc/api/tests/test_create_check.py +++ b/hc/api/tests/test_create_check.py @@ -86,7 +86,28 @@ class CreateCheckTestCase(BaseTestCase): check = Check.objects.get() self.assertEqual(check.channel_set.get(), channel) - def test_it_rejects_bad_channel_code(self): + def test_it_sets_channel_by_name(self): + channel = Channel.objects.create(project=self.project, name="alerts") + + r = self.post({"api_key": "X" * 32, "channels": "alerts"}) + self.assertEqual(r.status_code, 201) + + check = Check.objects.get() + assigned_channel = check.channel_set.get() + self.assertEqual(assigned_channel, channel) + + def test_it_sets_channel_by_name_formatted_as_uuid(self): + name = "102eaa82-a274-4b15-a499-c1bb6bbcd7b6" + channel = Channel.objects.create(project=self.project, name=name) + + r = self.post({"api_key": "X" * 32, "channels": name}) + self.assertEqual(r.status_code, 201) + + check = Check.objects.get() + assigned_channel = check.channel_set.get() + self.assertEqual(assigned_channel, channel) + + def test_it_handles_channel_lookup_by_name_with_no_results(self): r = self.post({"api_key": "X" * 32, "channels": "abc"}) self.assertEqual(r.status_code, 400) self.assertEqual(r.json()["error"], "invalid channel identifier: abc") @@ -94,6 +115,28 @@ class CreateCheckTestCase(BaseTestCase): # The check should not have been saved self.assertFalse(Check.objects.exists()) + def test_it_handles_channel_lookup_by_name_with_multiple_results(self): + Channel.objects.create(project=self.project, name="foo") + Channel.objects.create(project=self.project, name="foo") + + r = self.post({"api_key": "X" * 32, "channels": "foo"}) + + self.assertEqual(r.status_code, 400) + self.assertEqual(r.json()["error"], "non-unique channel identifier: foo") + + # The check should not have been saved + self.assertFalse(Check.objects.exists()) + + def test_it_rejects_multiple_empty_channel_names(self): + Channel.objects.create(project=self.project, name="") + + r = self.post({"api_key": "X" * 32, "channels": ","}) + self.assertEqual(r.status_code, 400) + self.assertEqual(r.json()["error"], "empty channel identifier") + + # The check should not have been saved + self.assertFalse(Check.objects.exists()) + def test_it_supports_unique(self): check = Check.objects.create(project=self.project, name="Foo")