Skip to content

Commit

Permalink
Merge pull request #134 from open-zaak/feature/catalogus-validation-z…
Browse files Browse the repository at this point in the history
…aakinformatieobjecttype

Feature/catalogus validation zaakinformatieobjecttype
  • Loading branch information
joeribekker committed Oct 29, 2019
2 parents 68d7bea + 015bf57 commit a5131e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ...constants import RichtingChoices
from ...models import ZaakInformatieobjectType
from ..validators import ZaakInformatieObjectTypeCatalogusValidator


class ZaakTypeInformatieObjectTypeSerializer(serializers.HyperlinkedModelSerializer):
Expand All @@ -28,6 +29,7 @@ class Meta:
"informatieobjecttype": {"lookup_field": "uuid"},
"statustype": {"lookup_field": "uuid"},
}
validators = [ZaakInformatieObjectTypeCatalogusValidator()]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down
21 changes: 21 additions & 0 deletions src/openzaak/components/catalogi/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,24 @@ def __call__(self, attrs: dict):
}
)
raise ValidationError(error_dict)


class ZaakInformatieObjectTypeCatalogusValidator:
code = "relations-incorrect-catalogus"
message = _("The zaaktype has catalogus different from informatieobjecttype")

def set_context(self, serializer):
"""
This hook is called by the serializer instance,
prior to the validation call being made.
"""
self.instance = getattr(serializer, "instance", None)

def __call__(self, attrs: dict):
zaaktype = attrs.get("zaaktype") or self.instance.zaaktype
informatieobjecttype = (
attrs.get("informatieobjecttype") or self.instance.informatieobjecttype
)

if zaaktype.catalogus != informatieobjecttype.catalogus:
raise ValidationError(self.message, code=self.code)
38 changes: 35 additions & 3 deletions src/openzaak/components/catalogi/tests/test_relatieklassen.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..models import ZaakInformatieobjectType
from .base import APITestCase
from .factories import (
CatalogusFactory,
InformatieObjectTypeFactory,
ZaakInformatieobjectTypeArchiefregimeFactory,
ZaakInformatieobjectTypeFactory,
Expand Down Expand Up @@ -64,7 +65,9 @@ def test_get_detail(self):
def test_create_ziot(self):
zaaktype = ZaakTypeFactory.create()
zaaktype_url = reverse(zaaktype)
informatieobjecttype = InformatieObjectTypeFactory.create()
informatieobjecttype = InformatieObjectTypeFactory.create(
catalogus=zaaktype.catalogus
)
informatieobjecttype_url = reverse(informatieobjecttype)
data = {
"zaaktype": f"http://testserver{zaaktype_url}",
Expand All @@ -85,7 +88,9 @@ def test_create_ziot(self):
def test_create_ziot_fail_not_concept_zaaktype(self):
zaaktype = ZaakTypeFactory.create(concept=False)
zaaktype_url = reverse(zaaktype)
informatieobjecttype = InformatieObjectTypeFactory.create()
informatieobjecttype = InformatieObjectTypeFactory.create(
catalogus=zaaktype.catalogus
)
informatieobjecttype_url = reverse(informatieobjecttype)
data = {
"zaaktype": f"http://testserver{zaaktype_url}",
Expand All @@ -107,7 +112,9 @@ def test_create_ziot_fail_not_concept_zaaktype(self):
def test_create_ziot_fail_not_concept_informatieobjecttype(self):
zaaktype = ZaakTypeFactory.create()
zaaktype_url = reverse(zaaktype)
informatieobjecttype = InformatieObjectTypeFactory.create(concept=False)
informatieobjecttype = InformatieObjectTypeFactory.create(
concept=False, catalogus=zaaktype.catalogus
)
informatieobjecttype_url = reverse(informatieobjecttype)
data = {
"zaaktype": f"http://testserver{zaaktype_url}",
Expand Down Expand Up @@ -392,3 +399,28 @@ def test_get_detail(self):
"rstzdt.selectielijstklasse": None,
}
self.assertEqual(response.json(), expected)


class ZaakInformatieobjecttypeValidationTests(APITestCase):
maxDiff = None

list_url = reverse_lazy(ZaakInformatieobjectType)

def test_catalogus_mismatch(self):
zaaktype = ZaakTypeFactory.create()
zaaktype_url = reverse(zaaktype)
informatieobjecttype = InformatieObjectTypeFactory.create()
informatieobjecttype_url = reverse(informatieobjecttype)
data = {
"zaaktype": f"http://testserver{zaaktype_url}",
"informatieobjecttype": f"http://testserver{informatieobjecttype_url}",
"volgnummer": 13,
"richting": RichtingChoices.inkomend,
}

response = self.client.post(self.list_url, data)

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

error = get_validation_errors(response, "nonFieldErrors")
self.assertEqual(error["code"], "relations-incorrect-catalogus")

0 comments on commit a5131e4

Please sign in to comment.