From 6efb822f95359573528e3e9a66a323ae947b3e67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Wed, 17 Feb 2016 22:35:06 +0200 Subject: [PATCH] When creating a check via API, optionally assign all channels. --- hc/api/schemas.py | 3 ++- hc/api/tests/test_create_check.py | 17 +++++++++++++++-- hc/api/views.py | 5 +++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/hc/api/schemas.py b/hc/api/schemas.py index 55337b8b..3db4282d 100644 --- a/hc/api/schemas.py +++ b/hc/api/schemas.py @@ -3,6 +3,7 @@ check = { "name": {"type": "string"}, "tags": {"type": "string"}, "timeout": {"type": "number", "minimum": 60, "maximum": 604800}, - "grace": {"type": "number", "minimum": 60, "maximum": 604800} + "grace": {"type": "number", "minimum": 60, "maximum": 604800}, + "channels": {"type": "string"} } } diff --git a/hc/api/tests/test_create_check.py b/hc/api/tests/test_create_check.py index 84bd66e1..52d3084f 100644 --- a/hc/api/tests/test_create_check.py +++ b/hc/api/tests/test_create_check.py @@ -1,6 +1,6 @@ import json -from hc.api.models import Check +from hc.api.models import Channel, Check from hc.test import BaseTestCase from hc.accounts.models import Profile @@ -35,6 +35,19 @@ class CreateCheckTestCase(BaseTestCase): self.assertEqual(check.timeout.total_seconds(), 3600) self.assertEqual(check.grace.total_seconds(), 60) + def test_it_assigns_channels(self): + channel = Channel(user=self.alice) + channel.save() + + r = self.post("/api/v1/checks/", { + "api_key": "abc", + "channels": "*" + }) + + self.assertEqual(r.status_code, 201) + check = Check.objects.get() + self.assertEqual(check.channel_set.get(), channel) + def test_it_handles_missing_request_body(self): r = self.client.post("/api/v1/checks/", content_type="application/json") @@ -50,7 +63,7 @@ class CreateCheckTestCase(BaseTestCase): content_type="application/json") self.assertEqual(r.json()["error"], "could not parse request body") - def test_it_reject_small_timeout(self): + def test_it_rejects_small_timeout(self): r = self.post("/api/v1/checks/", {"api_key": "abc", "timeout": 0}) self.assertEqual(r.json()["error"], "timeout is too small") diff --git a/hc/api/views.py b/hc/api/views.py index c94c6349..0de24b27 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -93,6 +93,11 @@ def create_check(request): check.save() + # This needs to be done after saving the check, because of + # the M2M relation between checks and channels: + if request.json.get("channels") == "*": + check.assign_all_channels() + response = { "ping_url": check.url() }