mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-22 06:44:21 +00:00
🧪 Fix some test bugs
This commit is contained in:
parent
e9f3443a98
commit
9ca716f9f2
2 changed files with 27 additions and 15 deletions
|
@ -95,6 +95,9 @@ class ReadSophonTestCase(BetterAPITestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`list` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`list` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`list` did not return {code}: {response.data!r}")
|
||||
return response.data or None
|
||||
|
@ -122,6 +125,9 @@ class ReadSophonTestCase(BetterAPITestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`retrieve` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`retrieve` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`retrieve` did not return {code}: {response.data!r}")
|
||||
return response.data
|
||||
|
@ -153,6 +159,9 @@ class ReadSophonTestCase(BetterAPITestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`{action}` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`{action}` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`{action}` did not return {code}: {response.data!r}")
|
||||
return response.data
|
||||
|
@ -186,6 +195,9 @@ class ReadSophonTestCase(BetterAPITestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`{action}` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`{action}` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`{action}` did not return {code}: {response.data!r}")
|
||||
return response.data
|
||||
|
@ -219,6 +231,9 @@ class WriteSophonTestCase(ReadSophonTestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`create` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`create` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`create` did not return {code}: {response.data!r}")
|
||||
return response.data
|
||||
|
@ -248,6 +263,9 @@ class WriteSophonTestCase(ReadSophonTestCase, metaclass=abc.ABCMeta):
|
|||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`update` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`update` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`update` did not return {code}: {response.data!r}")
|
||||
return response.data
|
||||
|
@ -273,7 +291,10 @@ class WriteSophonTestCase(ReadSophonTestCase, metaclass=abc.ABCMeta):
|
|||
try:
|
||||
response = self.destroy(pk=pk)
|
||||
except errors.HTTPException as exc:
|
||||
self.assertEqual(exc.status, code, msg=f"`create` did not return {code}: {exc!r}")
|
||||
self.assertEqual(exc.status, code, msg=f"`destroy` did not return {code}: {exc!r}")
|
||||
return None
|
||||
except ValueError as exc:
|
||||
self.assertEqual(400, code, msg=f"`destroy` did not return {code}: {exc!r}")
|
||||
return None
|
||||
else:
|
||||
self.assertEqual(response.status_code, code, msg=f"`destroy` did not return {code}: {response.data!r}")
|
||||
|
@ -317,11 +338,6 @@ class UsersByIdTestCase(ReadSophonTestCase):
|
|||
data = self.assertActionRetrieve(self.third_user.id)
|
||||
self.assertData(data, {"username": "third"})
|
||||
|
||||
def test_retrieve_400(self):
|
||||
self.assertActionRetrieve("qwerty", code=400)
|
||||
self.assertActionRetrieve(1.0, code=400)
|
||||
self.assertActionRetrieve("xyzzy", code=400)
|
||||
|
||||
def test_retrieve_404(self):
|
||||
self.assertActionRetrieve(100, code=404)
|
||||
self.assertActionRetrieve(-1, code=404)
|
||||
|
@ -365,11 +381,6 @@ class UsersByUsernameTestCase(ReadSophonTestCase):
|
|||
data = self.assertActionRetrieve(self.third_user.username)
|
||||
self.assertData(data, {"username": "aaaaaa"})
|
||||
|
||||
def test_retrieve_400(self):
|
||||
self.assertActionRetrieve(1, code=400)
|
||||
self.assertActionRetrieve(-1, code=400)
|
||||
self.assertActionRetrieve(1.0, code=400)
|
||||
|
||||
def test_retrieve_404(self):
|
||||
self.assertActionRetrieve("sas", code=404)
|
||||
self.assertActionRetrieve("sos", code=404)
|
||||
|
|
|
@ -4,6 +4,7 @@ import typing as t
|
|||
import deprecation
|
||||
from django.contrib.auth.models import User
|
||||
from django.db.models import QuerySet
|
||||
from django.shortcuts import get_object_or_404
|
||||
from rest_framework import status as s
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.request import Request
|
||||
|
@ -195,7 +196,7 @@ class UsersByIdViewSet(ReadSophonViewSet):
|
|||
|
||||
def get_object(self):
|
||||
pk = self.kwargs["pk"]
|
||||
return User.objects.filter(id=pk).get()
|
||||
return get_object_or_404(User.objects.filter(id=pk))
|
||||
|
||||
|
||||
class UsersByUsernameViewSet(ReadSophonViewSet):
|
||||
|
@ -211,7 +212,7 @@ class UsersByUsernameViewSet(ReadSophonViewSet):
|
|||
|
||||
def get_object(self):
|
||||
pk = self.kwargs["pk"]
|
||||
return User.objects.filter(username=pk).get()
|
||||
return get_object_or_404(User.objects.filter(username=pk))
|
||||
|
||||
|
||||
class ResearchGroupViewSet(WriteSophonViewSet):
|
||||
|
@ -260,7 +261,7 @@ class ResearchGroupViewSet(WriteSophonViewSet):
|
|||
"""
|
||||
An action that allows an user to join a group with ``"OPEN"`` access.
|
||||
"""
|
||||
group = models.ResearchGroup.objects.get(pk=pk)
|
||||
group = get_object_or_404(models.ResearchGroup.objects, pk=pk)
|
||||
|
||||
if self.request.user.is_anonymous:
|
||||
return Response(status=s.HTTP_401_UNAUTHORIZED)
|
||||
|
@ -284,7 +285,7 @@ class ResearchGroupViewSet(WriteSophonViewSet):
|
|||
|
||||
Group owners aren't allowed to leave the group they created to prevent situations where a group has no owner.
|
||||
"""
|
||||
group = models.ResearchGroup.objects.get(pk=pk)
|
||||
group = get_object_or_404(models.ResearchGroup.objects, pk=pk)
|
||||
|
||||
if self.request.user.is_anonymous:
|
||||
return Response(status=s.HTTP_401_UNAUTHORIZED)
|
||||
|
|
Loading…
Reference in a new issue