Browse Source

Merge pull request #199 from muff1nman/add-specific-channel

Allow specific channel uuid to be specified in create/update check api
pull/211/head
Pēteris Caune 6 years ago
committed by GitHub
parent
commit
def1a12a4a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions
  1. +45
    -0
      hc/api/tests/test_update_check.py
  2. +13
    -2
      hc/api/views.py

+ 45
- 0
hc/api/tests/test_update_check.py View File

@ -1,3 +1,5 @@
import uuid
from hc.api.models import Channel, Check from hc.api.models import Channel, Check
from hc.test import BaseTestCase from hc.test import BaseTestCase
@ -88,3 +90,46 @@ class UpdateCheckTestCase(BaseTestCase):
self.check.refresh_from_db() self.check.refresh_from_db()
self.assertEqual(self.check.kind, "simple") self.assertEqual(self.check.kind, "simple")
def test_it_updates_specific_channels(self):
channel1 = Channel(user=self.alice)
channel1.save()
channel2 = Channel(user=self.alice)
channel2.save()
r = self.post(self.check.code, {
"api_key": "X" * 32,
"channels": str(channel1.code)
})
self.assertEqual(r.status_code, 200)
check = Check.objects.get()
self.assertEqual(check.channel_set.count(), 1)
self.assertEqual(check.channel_set.first().code, channel1.code)
# Change to the other channel
r = self.post(self.check.code, {
"api_key": "X" * 32,
"channels": str(channel2.code)
})
self.assertEqual(r.status_code, 200)
check = Check.objects.get()
self.assertEqual(check.channel_set.count(), 1)
self.assertEqual(check.channel_set.first().code, channel2.code)
# Now set both channels
r = self.post(self.check.code, {
"api_key": "X" * 32,
"channels": str(channel2.code) + "," + str(channel1.code)
})
self.assertEqual(r.status_code, 200)
check = Check.objects.get()
self.assertEqual(check.channel_set.count(), 2)
# Try to use channel that does not exist
r = self.post(self.check.code, {
"api_key": "X" * 32,
"channels": str(uuid.uuid4())
})
self.assertEqual(r.status_code, 400)
check = Check.objects.get()
self.assertEqual(check.channel_set.count(), 0)

+ 13
- 2
hc/api/views.py View File

@ -1,6 +1,8 @@
from datetime import timedelta as td from datetime import timedelta as td
from uuid import UUID
from django.conf import settings from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.db import connection from django.db import connection
from django.http import (HttpResponse, HttpResponseForbidden, from django.http import (HttpResponse, HttpResponseForbidden,
HttpResponseNotFound, JsonResponse) HttpResponseNotFound, JsonResponse)
@ -12,7 +14,7 @@ from django.views.decorators.http import require_POST
from hc.api import schemas from hc.api import schemas
from hc.api.decorators import authorize, authorize_read, validate_json from hc.api.decorators import authorize, authorize_read, validate_json
from hc.api.models import Check, Notification
from hc.api.models import Check, Notification, Channel
from hc.lib.badges import check_signature, get_badge_svg from hc.lib.badges import check_signature, get_badge_svg
@ -81,8 +83,17 @@ def _update(check, spec):
if "channels" in spec: if "channels" in spec:
if spec["channels"] == "*": if spec["channels"] == "*":
check.assign_all_channels() check.assign_all_channels()
elif spec["channels"] == "":
else:
check.channel_set.clear() check.channel_set.clear()
if spec["channels"] is not None and spec["channels"] != "":
channels = []
for raw_channel in spec["channels"].split(","):
try:
channel = Channel.objects.get(code=UUID(raw_channel))
channels.append(channel)
except Channel.objects.model.DoesNotExist:
raise SuspiciousOperation("One of the specified channels is missing")
check.channel_set.add(*channels)
return check return check


Loading…
Cancel
Save