From 9ca716f9f245a3d55fcf8f7f4cd120e21910cb24 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Fri, 29 Oct 2021 02:41:52 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20Fix=20some=20test=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/sophon/core/tests.py | 33 ++++++++++++++++++++++----------- backend/sophon/core/views.py | 9 +++++---- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/backend/sophon/core/tests.py b/backend/sophon/core/tests.py index 520dd88..00d0c1a 100644 --- a/backend/sophon/core/tests.py +++ b/backend/sophon/core/tests.py @@ -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) diff --git a/backend/sophon/core/views.py b/backend/sophon/core/views.py index 9a20695..748d940 100644 --- a/backend/sophon/core/views.py +++ b/backend/sophon/core/views.py @@ -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)