Skip to content

Commit

Permalink
👌 process PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
annashamray committed Nov 25, 2020
1 parent bb0499f commit df9184d
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/objecttypes/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class VersionUpdateValidator:
message = _("Only draft versions can be changed")
code = "non-draft-object"
code = "non-draft-version-update"

def set_context(self, serializer):
"""
Expand Down
48 changes: 35 additions & 13 deletions src/objecttypes/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from drf_yasg.utils import no_body, swagger_auto_schema
from rest_framework import mixins, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from django.utils.translation import ugettext_lazy as _

from rest_framework import viewsets
from rest_framework.exceptions import ValidationError
from rest_framework.settings import api_settings
from vng_api_common.viewsets import NestedViewSetMixin

from objecttypes.core.constants import ObjectVersionStatus
Expand All @@ -11,21 +12,42 @@
from .serializers import ObjectTypeSerializer, ObjectVersionSerializer


class ObjectTypeViewSet(
mixins.CreateModelMixin, mixins.UpdateModelMixin, viewsets.ReadOnlyModelViewSet
):
class ObjectTypeViewSet(viewsets.ModelViewSet):
queryset = ObjectType.objects.prefetch_related("versions").order_by("-pk")
serializer_class = ObjectTypeSerializer
lookup_field = "uuid"
filterset_class = ObjectTypeFilterSet

def perform_destroy(self, instance):
if instance.versions.exists():
raise ValidationError(
{
api_settings.NON_FIELD_ERRORS_KEY: [
_(
"All related versions should be destroyed before destroying the objecttype"
)
]
},
code="pending-versions",
)

super().perform_destroy(instance)


class ObjectVersionViewSet(
NestedViewSetMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
viewsets.ReadOnlyModelViewSet,
):
class ObjectVersionViewSet(NestedViewSetMixin, viewsets.ModelViewSet):
queryset = ObjectVersion.objects.order_by("object_type", "-version")
serializer_class = ObjectVersionSerializer
lookup_field = "version"

def perform_destroy(self, instance):
if instance.status != ObjectVersionStatus.draft:
raise ValidationError(
{
api_settings.NON_FIELD_ERRORS_KEY: [
_("Only draft versions can be destroyed")
]
},
code="non-draft-version-destroy",
)

super().perform_destroy(instance)
28 changes: 26 additions & 2 deletions src/objecttypes/tests/test_objecttype_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ def test_get_objecttypes(self):
},
)

def test_get_objecttypes_with_versions(self):
object_types = ObjectTypeFactory.create_batch(2)
object_versions = [
ObjectVersionFactory.create(object_type=object_type)
for object_type in object_types
]
for i, object_type in enumerate(object_types):
with self.subTest(object_type=object_type):
url = reverse("objecttype-detail", args=[object_type.uuid])

response = self.client.get(url)

self.assertEqual(response.status_code, status.HTTP_200_OK)

data = response.json()
self.assertEqual(len(data["versions"]), 1)
self.assertEqual(
data["versions"],
[
f"http://testserver{reverse('objectversion-detail', args=[object_type.uuid, object_versions[i].version])}"
],
)

def test_create_objecttype(self):
url = reverse("objecttype-list")
data = {
Expand Down Expand Up @@ -100,10 +123,11 @@ def test_update_objecttype(self):
object_type.data_classification, DataClassificationChoices.open
)

def test_delete_objecttype_not_supported(self):
def test_delete_objecttype(self):
object_type = ObjectTypeFactory.create()
url = reverse("objecttype-detail", args=[object_type.uuid])

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(ObjectType.objects.count(), 0)
37 changes: 37 additions & 0 deletions src/objecttypes/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@
from objecttypes.utils.test import TokenAuthMixin


class ObjectTypeValidationTests(TokenAuthMixin, APITestCase):
def test_delete_objecttype_with_versions_fail(self):
object_type = ObjectTypeFactory.create()
ObjectVersionFactory.create(object_type=object_type)
url = reverse("objecttype-detail", args=[object_type.uuid])

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
self.assertEqual(
data["non_field_errors"],
[
"All related versions should be destroyed before destroying the objecttype"
],
)


class ObjectVersionValidationTests(TokenAuthMixin, APITestCase):
def test_create_version_with_incorrect_schema_fail(self):
object_type = ObjectTypeFactory.create()
Expand Down Expand Up @@ -49,3 +68,21 @@ def test_update_published_version_fail(self):
self.assertEqual(
data["non_field_errors"], ["Only draft versions can be changed"]
)

def test_delete_puclished_version_fail(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(
object_type=object_type, status=ObjectVersionStatus.published
)
url = reverse(
"objectversion-detail", args=[object_type.uuid, object_version.version]
)

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

data = response.json()
self.assertEqual(
data["non_field_errors"], ["Only draft versions can be destroyed"]
)
5 changes: 3 additions & 2 deletions src/objecttypes/tests/test_version_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_update_version(self):
self.assertEqual(object_version.json_schema, new_json_schema)
self.assertEqual(object_version.status, ObjectVersionStatus.published)

def test_delete_version_not_supported(self):
def test_delete_version(self):
object_type = ObjectTypeFactory.create()
object_version = ObjectVersionFactory.create(object_type=object_type)
url = reverse(
Expand All @@ -95,4 +95,5 @@ def test_delete_version_not_supported(self):

response = self.client.delete(url)

self.assertEqual(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(ObjectVersion.objects.count(), 0)
Loading

0 comments on commit df9184d

Please sign in to comment.