Skip to content

Commit

Permalink
Merge pull request #162 from open-zaak/issue/forbid-notunique-oio-create
Browse files Browse the repository at this point in the history
Issue/forbid non-unique oio create
  • Loading branch information
joeribekker committed Nov 5, 2019
2 parents 3dd96a6 + fd4807a commit 65a60ef
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 63 deletions.
3 changes: 2 additions & 1 deletion src/openzaak/components/documenten/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
Gebruiksrechten,
ObjectInformatieObject,
)
from .validators import StatusValidator
from .validators import InformatieObjectUniqueValidator, StatusValidator


class AnyFileType:
Expand Down Expand Up @@ -466,6 +466,7 @@ class Meta:
model = ObjectInformatieObject
fields = ("url", "informatieobject", "object", "object_type")
extra_kwargs = {"url": {"lookup_field": "uuid"}}
validators = [InformatieObjectUniqueValidator()]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
22 changes: 22 additions & 0 deletions src/openzaak/components/documenten/api/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from collections import OrderedDict

from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

from rest_framework import serializers

Expand All @@ -23,3 +26,22 @@ def __call__(self, attrs: dict):
)
except ValidationError as exc:
raise serializers.ValidationError(exc.error_dict)


class InformatieObjectUniqueValidator:
"""
Validate that the relation between the object and informatieobject does not
exist yet in the Documenten component
"""

message = _("The fields object and infromatieobject must make a unique set.")
code = "unique"

def __call__(self, context: OrderedDict):
informatieobject = context["informatieobject"]
oios = informatieobject.objectinformatieobject_set.filter(
**{context["object_type"]: context["object"]}
)

if oios:
raise serializers.ValidationError(detail=self.message, code=self.code)
8 changes: 0 additions & 8 deletions src/openzaak/components/documenten/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,10 +461,6 @@ class EnkelvoudigInformatieObjectAuditTrailViewSet(AuditTrailViewSet):


class ObjectInformatieObjectViewSet(
# NotificationCreateMixin,
# NotificationDestroyMixin,
AuditTrailCreateMixin,
AuditTrailDestroyMixin,
CheckQueryParamsMixin,
ListFilterByAuthorizationsMixin,
mixins.CreateModelMixin,
Expand Down Expand Up @@ -513,8 +509,6 @@ class ObjectInformatieObjectViewSet(
serializer_class = ObjectInformatieObjectSerializer
filterset_class = ObjectInformatieObjectFilter
lookup_field = "uuid"
# notifications_kanaal = KANAAL_DOCUMENTEN
# notifications_main_resource_key = "informatieobject"
permission_classes = (InformationObjectAuthRequired,)
permission_main_object = "informatieobject"
required_scopes = {
Expand All @@ -525,8 +519,6 @@ class ObjectInformatieObjectViewSet(
"update": SCOPE_DOCUMENTEN_BIJWERKEN,
"partial_update": SCOPE_DOCUMENTEN_BIJWERKEN,
}
audit = AUDIT_DRC
audittrail_main_resource_key = "informatieobject"

def perform_create(self, serializer):
# object was already created by BIO/ZIO creation,
Expand Down
37 changes: 0 additions & 37 deletions src/openzaak/components/documenten/tests/test_audittrails.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,6 @@ def test_create_enkelvoudiginformatieobject_audittrail(self):
informatieobject_create_audittrail.nieuw, informatieobject_data
)

@tag("oio")
def test_create_objectinformatieobject_audittrail(self):
informatieobject = EnkelvoudigInformatieObjectFactory.create()
zio = ZaakInformatieObjectFactory.create(
informatieobject=informatieobject.canonical
)

content = {
"informatieobject": reverse(
"enkelvoudiginformatieobject-detail",
kwargs={"uuid": informatieobject.uuid},
),
"object": reverse(zio.zaak),
"objectType": ObjectTypes.zaak,
}

# Send to the API
objectinformatieobject_response = self.client.post(
self.objectinformatieobject_list_url, content
).data

informatieobject_url = objectinformatieobject_response["informatieobject"]
audittrails = AuditTrail.objects.filter(hoofd_object=informatieobject_url)
self.assertEqual(audittrails.count(), 1)

# Verify that the audittrail for the ObjectInformatieObject creation
# contains the correct information
objectinformatieobject_create_audittrail = audittrails.get()
self.assertEqual(objectinformatieobject_create_audittrail.bron, "DRC")
self.assertEqual(objectinformatieobject_create_audittrail.actie, "create")
self.assertEqual(objectinformatieobject_create_audittrail.resultaat, 201)
self.assertEqual(objectinformatieobject_create_audittrail.oud, None)
self.assertEqual(
objectinformatieobject_create_audittrail.nieuw,
objectinformatieobject_response,
)

def test_create_and_delete_gebruiksrechten_audittrail(self):
informatieobject = EnkelvoudigInformatieObjectFactory.create()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def test_create_with_objecttype_zaak(self):

zaak_url = reverse(zaak)
eio_url = reverse(eio)
oio_url = reverse("objectinformatieobject-detail", kwargs={"uuid": oio.uuid})

response = self.client.post(
self.list_url,
Expand All @@ -50,17 +49,13 @@ def test_create_with_objecttype_zaak(self):
},
)

self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.data)
self.assertEqual(
response.data,
{
"url": f"http://testserver{oio_url}",
"informatieobject": f"http://testserver{eio_url}",
"object": f"http://testserver{zaak_url}",
"object_type": "zaak",
},
response.status_code, status.HTTP_400_BAD_REQUEST, response.data
)

error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["code"], "unique")

def test_create_with_objecttype_besluit(self):
besluit = BesluitFactory.create()
eio = EnkelvoudigInformatieObjectFactory.create()
Expand All @@ -85,17 +80,13 @@ def test_create_with_objecttype_besluit(self):
},
)

self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(
response.data,
{
"url": f"http://testserver{oio_url}",
"object": f"http://testserver{besluit_url}",
"informatieobject": f"http://testserver{eio_url}",
"object_type": "besluit",
},
response.status_code, status.HTTP_400_BAD_REQUEST, response.data
)

error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["code"], "unique")

def test_create_with_objecttype_other_fail(self):
besluit = BesluitFactory.create()
eio = EnkelvoudigInformatieObjectFactory.create()
Expand Down

0 comments on commit 65a60ef

Please sign in to comment.