Skip to content

Commit 3de61ac

Browse files
committed
make blacklist endpoints accessible for any authenticated user
1 parent dd77bdd commit 3de61ac

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

osmchadjango/supervise/tests/test_views.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,11 @@ def setUp(self):
844844
uid='3435',
845845
added_by=self.staff_user,
846846
)
847+
BlacklistedUser.objects.create(
848+
username='New bad user',
849+
uid='9888',
850+
added_by=self.user,
851+
)
847852
self.url = reverse('supervise:blacklist-list-create')
848853

849854
def test_list_view_unauthenticated(self):
@@ -853,7 +858,8 @@ def test_list_view_unauthenticated(self):
853858
def test_list_view_normal_user(self):
854859
self.client.login(username=self.user.username, password='password')
855860
response = self.client.get(self.url)
856-
self.assertEqual(response.status_code, 403)
861+
self.assertEqual(response.status_code, 200)
862+
self.assertEqual(len(response.data.get('results')), 1)
857863

858864
def test_list_view_staff_user(self):
859865
self.client.login(username=self.staff_user.username, password='password')
@@ -896,8 +902,8 @@ def test_create_view_unauthenticated(self):
896902
def test_create_view_normal_user(self):
897903
self.client.login(username=self.user.username, password='password')
898904
response = self.client.post(self.url, self.data)
899-
self.assertEqual(response.status_code, 403)
900-
self.assertEqual(BlacklistedUser.objects.count(), 0)
905+
self.assertEqual(response.status_code, 201)
906+
self.assertEqual(BlacklistedUser.objects.count(), 1)
901907

902908
def test_create_view_staff_user(self):
903909
self.client.login(username=self.staff_user.username, password='password')
@@ -935,6 +941,11 @@ def setUp(self):
935941
uid='3434',
936942
added_by=self.staff_user,
937943
)
944+
self.blacklisted_2 = BlacklistedUser.objects.create(
945+
username='Bad User',
946+
uid='3434',
947+
added_by=self.user,
948+
)
938949
self.url = reverse(
939950
'supervise:blacklist-detail', args=[self.blacklisted.uid]
940951
)
@@ -946,7 +957,23 @@ def test_unauthenticated_get(self):
946957
def test_normal_user_get(self):
947958
self.client.login(username=self.user.username, password='password')
948959
response = self.client.get(self.url)
949-
self.assertEqual(response.status_code, 403)
960+
self.assertEqual(response.status_code, 200)
961+
self.assertEqual(response.data.get('username'), 'Bad User')
962+
self.assertEqual(response.data.get('added_by'), 'test_user')
963+
self.assertIsNotNone(response.data.get('uid'))
964+
self.assertIn('date', response.data.keys())
965+
966+
def test_normal_user_getting_staff_user_blacklist(self):
967+
blacklisted = BlacklistedUser.objects.create(
968+
username='Bad User',
969+
uid='4999',
970+
added_by=self.staff_user,
971+
)
972+
self.client.login(username=self.user.username, password='password')
973+
response = self.client.get(
974+
reverse('supervise:blacklist-detail', args=[4999])
975+
)
976+
self.assertEqual(response.status_code, 404)
950977

951978
def test_staff_user_get(self):
952979
self.client.login(username=self.staff_user.username, password='password')
@@ -960,19 +987,19 @@ def test_staff_user_get(self):
960987
def test_unauthenticated_delete(self):
961988
response = self.client.delete(self.url)
962989
self.assertEqual(response.status_code, 401)
963-
self.assertEqual(BlacklistedUser.objects.count(), 1)
990+
self.assertEqual(BlacklistedUser.objects.count(), 2)
964991

965992
def test_normal_user_delete(self):
966993
self.client.login(username=self.user.username, password='password')
967994
response = self.client.delete(self.url)
968-
self.assertEqual(response.status_code, 403)
995+
self.assertEqual(response.status_code, 204)
969996
self.assertEqual(BlacklistedUser.objects.count(), 1)
970997

971998
def test_staff_user_delete(self):
972999
self.client.login(username=self.staff_user.username, password='password')
9731000
response = self.client.delete(self.url)
9741001
self.assertEqual(response.status_code, 204)
975-
self.assertEqual(BlacklistedUser.objects.count(), 0)
1002+
self.assertEqual(BlacklistedUser.objects.count(), 1)
9761003

9771004
def test_unauthenticated_patch(self):
9781005
response = self.client.patch(self.url, {'username': 'other_user'})
@@ -982,8 +1009,9 @@ def test_unauthenticated_patch(self):
9821009
def test_normal_user_patch(self):
9831010
self.client.login(username=self.user.username, password='password')
9841011
response = self.client.patch(self.url, {'username': 'other_user'})
985-
self.assertEqual(response.status_code, 403)
986-
self.assertEqual(self.blacklisted.username, 'Bad User')
1012+
self.assertEqual(response.status_code, 200)
1013+
self.blacklisted_2.refresh_from_db()
1014+
self.assertEqual(self.blacklisted_2.username, 'other_user')
9871015

9881016
def test_staff_user_patch(self):
9891017
self.client.login(username=self.staff_user.username, password='password')

osmchadjango/supervise/views.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.urls import reverse
66

77
from rest_framework.generics import (
8-
ListCreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView
8+
ListCreateAPIView, ListAPIView, RetrieveUpdateDestroyAPIView, get_object_or_404
99
)
1010
from rest_framework.response import Response
1111
from rest_framework.filters import OrderingFilter
@@ -222,7 +222,13 @@ class BlacklistedUserListCreateAPIView(ListCreateAPIView):
222222
"""
223223
queryset = BlacklistedUser.objects.all()
224224
serializer_class = BlacklistSerializer
225-
permission_classes = (IsAdminUser,)
225+
permission_classes = (IsAuthenticated,)
226+
227+
def get_queryset(self):
228+
if self.request:
229+
return BlacklistedUser.objects.filter(added_by=self.request.user)
230+
else:
231+
BlacklistedUser.objects.none()
226232

227233
def perform_create(self, serializer):
228234
serializer.save(added_by=self.request.user)
@@ -234,21 +240,25 @@ class BlacklistedUserDetailAPIView(RetrieveUpdateDestroyAPIView):
234240
Get details about a BlacklistedUser.
235241
Access restricted to staff users.
236242
delete:
237-
Delete a User from the Blacklist.
238-
Only staff users can use this method.
243+
Delete a User from your Blacklist.
239244
patch:
240245
Update a BlacklistedUser.
241-
It's useful if you need to update the username of a User. Only staff users
242-
can use this method.
246+
It's useful if you need to update the username of a User.
243247
put:
244248
Update a BlacklistedUser.
245-
It's useful if you need to update the username of a User. Only staff users
246-
can use this method.
249+
It's useful if you need to update the username of a User.
247250
"""
248251
queryset = BlacklistedUser.objects.all()
249252
serializer_class = BlacklistSerializer
250-
permission_classes = (IsAdminUser,)
251-
lookup_field = 'uid'
253+
permission_classes = (IsAuthenticated, IsOwnerOrReadOnly)
252254

253255
def perform_update(self, serializer):
254256
serializer.save(added_by=self.request.user)
257+
258+
def get_object(self):
259+
queryset = self.get_queryset()
260+
return get_object_or_404(
261+
queryset,
262+
added_by=self.request.user,
263+
uid=self.kwargs['uid']
264+
)

0 commit comments

Comments
 (0)