Refactoring and a testcase for channels=None

This commit is contained in:
Pēteris Caune 2018-11-08 11:59:04 +02:00
parent e866d63ca4
commit 16d78db72e
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
2 changed files with 18 additions and 11 deletions

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)

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