Browse Source

Refactoring and a testcase for channels=None

pull/211/head
Pēteris Caune 6 years ago
parent
commit
16d78db72e
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 18 additions and 11 deletions
  1. +8
    -0
      hc/api/tests/test_update_check.py
  2. +10
    -11
      hc/api/views.py

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

@ -151,3 +151,11 @@ class UpdateCheckTestCase(BaseTestCase):
self.check.refresh_from_db()
self.assertEqual(self.check.channel_set.count(), 0)
def test_it_rejects_non_string_channels_key(self):
r = self.post(self.check.code, {
"api_key": "X" * 32,
"channels": None
})
self.assertEqual(r.status_code, 400)

+ 10
- 11
hc/api/views.py View File

@ -1,5 +1,4 @@
from datetime import timedelta as td
from uuid import UUID
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
@ -83,17 +82,17 @@ def _update(check, spec):
if "channels" in spec:
if spec["channels"] == "*":
check.assign_all_channels()
else:
elif spec["channels"] == "":
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)
else:
channels = []
for chunk in spec["channels"].split(","):
try:
channel = Channel.objects.get(code=chunk)
channels.append(channel)
except Channel.DoesNotExist:
raise SuspiciousOperation("Invalid channel identifier")
check.channel_set.set(channels)
return check


Loading…
Cancel
Save