Skip to content

Commit 648224a

Browse files
authored
Merge pull request #5968 from bjester/fix/remove-self-invalid-uuid
Validation against invalid UUID for remove-self endpoint
2 parents 4a8cf30 + 1bfd670 commit 648224a

2 files changed

Lines changed: 28 additions & 0 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: 5 additions & 0 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

@@ -341,6 +342,10 @@ def remove_self(self, request, pk=None):
341342

342343
if not channel_id:
343344
return HttpResponseBadRequest("Channel ID is required.")
345+
try:
346+
channel_id = uuid.UUID(channel_id).hex
347+
except ValueError:
348+
return HttpResponseBadRequest("Invalid channel ID")
344349

345350
try:
346351
channel = Channel.objects.get(id=channel_id)

0 commit comments

Comments
 (0)