Skip to content

Commit 1bfd670

Browse files
committed
Cleanup validation and add test coverage
1 parent 6d791df commit 1bfd670

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

contentcuration/contentcuration/tests/viewsets/test_user.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from contentcuration.models import Change
77
from contentcuration.tests import testdata
88
from contentcuration.tests.base import StudioAPITestCase
9+
from contentcuration.tests.helpers import reverse_with_query
910
from contentcuration.tests.viewsets.base import generate_create_event
1011
from contentcuration.tests.viewsets.base import generate_delete_event
1112
from contentcuration.tests.viewsets.base import SyncTestMixin
@@ -367,6 +368,28 @@ def test_fetch_users_no_permissions(self):
367368
self.assertEqual(response.status_code, 200, response.content)
368369
self.assertEqual(response.json(), [])
369370

371+
def test_remove_self_with_invalid_channel_id_returns_bad_request(self):
372+
self.client.force_authenticate(user=self.user)
373+
response = self.client.delete(
374+
reverse_with_query(
375+
"channeluser-remove-self",
376+
kwargs={"pk": self.user.id},
377+
query={"channel_id": "not-a-valid-uuid"},
378+
)
379+
)
380+
self.assertEqual(response.status_code, 400, response.content)
381+
382+
def test_remove_self_with_missing_channel_returns_not_found(self):
383+
self.client.force_authenticate(user=self.user)
384+
response = self.client.delete(
385+
reverse_with_query(
386+
"channeluser-remove-self",
387+
kwargs={"pk": self.user.id},
388+
query={"channel_id": "00000000-0000-0000-0000-000000000000"},
389+
)
390+
)
391+
self.assertEqual(response.status_code, 404, response.content)
392+
370393

371394
class MarkReadNotificationsTimestampTestCase(StudioAPITestCase):
372395
def setUp(self):

contentcuration/contentcuration/viewsets/user.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
import logging
3+
import uuid
34
from datetime import date
45
from functools import reduce
56

@@ -51,7 +52,7 @@
5152
from contentcuration.viewsets.sync.constants import DELETED
5253
from contentcuration.viewsets.sync.constants import EDITOR_M2M
5354
from contentcuration.viewsets.sync.constants import VIEWER_M2M
54-
import uuid
55+
5556

5657
logger = logging.getLogger(__name__)
5758

@@ -342,16 +343,14 @@ def remove_self(self, request, pk=None):
342343
if not channel_id:
343344
return HttpResponseBadRequest("Channel ID is required.")
344345
try:
345-
uuid.UUID(channel_id)
346+
channel_id = uuid.UUID(channel_id).hex
346347
except ValueError:
347348
return HttpResponseBadRequest("Invalid channel ID")
348349

349350
try:
350351
channel = Channel.objects.get(id=channel_id)
351352
except Channel.DoesNotExist:
352353
return HttpResponseNotFound("Channel not found {}".format(channel_id))
353-
except ValueError:
354-
return HttpResponseBadRequest("Invalid channel ID: {}".format(channel_id))
355354

356355
if request.user != user and not request.user.can_edit(channel_id):
357356
return HttpResponseForbidden(

0 commit comments

Comments
 (0)