From 824729707e1e6fcfdee98b68522e0ac9cef14810 Mon Sep 17 00:00:00 2001 From: Andrew DeMaria Date: Wed, 7 Nov 2018 07:35:21 -0700 Subject: [PATCH] Allow specific channel uuid to be specified in create/update check api --- hc/api/tests/test_update_check.py | 45 +++++++++++++++++++++++++++++++ hc/api/views.py | 15 +++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/hc/api/tests/test_update_check.py b/hc/api/tests/test_update_check.py index 0b5e85e9..3c97edd9 100644 --- a/hc/api/tests/test_update_check.py +++ b/hc/api/tests/test_update_check.py @@ -1,3 +1,5 @@ +import uuid + from hc.api.models import Channel, Check from hc.test import BaseTestCase @@ -88,3 +90,46 @@ class UpdateCheckTestCase(BaseTestCase): self.check.refresh_from_db() 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) diff --git a/hc/api/views.py b/hc/api/views.py index 742f686d..28395111 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -1,6 +1,8 @@ from datetime import timedelta as td +from uuid import UUID from django.conf import settings +from django.core.exceptions import SuspiciousOperation from django.db import connection from django.http import (HttpResponse, HttpResponseForbidden, HttpResponseNotFound, JsonResponse) @@ -12,7 +14,7 @@ from django.views.decorators.http import require_POST from hc.api import schemas 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 @@ -81,8 +83,17 @@ def _update(check, spec): if "channels" in spec: if spec["channels"] == "*": check.assign_all_channels() - elif spec["channels"] == "": + else: 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