Browse Source

When creating a check via API, optionally assign all channels.

pull/46/head
Pēteris Caune 9 years ago
parent
commit
6efb822f95
3 changed files with 22 additions and 3 deletions
  1. +2
    -1
      hc/api/schemas.py
  2. +15
    -2
      hc/api/tests/test_create_check.py
  3. +5
    -0
      hc/api/views.py

+ 2
- 1
hc/api/schemas.py View File

@ -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"}
}
}

+ 15
- 2
hc/api/tests/test_create_check.py View File

@ -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")


+ 5
- 0
hc/api/views.py View File

@ -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()
}


Loading…
Cancel
Save