From e6ee4ab04e76d7c256b3d638335bd58387f37646 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Fri, 20 Dec 2024 15:53:13 +0100 Subject: [PATCH] [#233] Fix tests --- .../api/tests/test_filters.py | 8 +- .../api/tests/test_partijen.py | 40 ++++----- .../klantinteracties/models/validators.py | 68 ++++++++------- .../klantinteracties/tests/test_validators.py | 82 +++++++++++++------ 4 files changed, 116 insertions(+), 82 deletions(-) diff --git a/src/openklant/components/klantinteracties/api/tests/test_filters.py b/src/openklant/components/klantinteracties/api/tests/test_filters.py index 7418508f..0583cd16 100644 --- a/src/openklant/components/klantinteracties/api/tests/test_filters.py +++ b/src/openklant/components/klantinteracties/api/tests/test_filters.py @@ -728,7 +728,7 @@ def test_filter_identificator_soort_object_id(self): partij=partij, partij_identificator_code_soort_object_id="bsn" ) PartijIdentificatorFactory.create( - partij=partij2, partij_identificator_code_soort_object_id="kvk_nummer" + partij=partij2, partij_identificator_code_soort_object_id="kvknummer" ) with self.subTest("happy flow"): @@ -759,18 +759,18 @@ def test_filter_identificator_object_id(self): PartijIdentificatorFactory.create( partij=partij, partij_identificator_code_soort_object_id="bsn", - partij_identificator_object_id="123456789", + partij_identificator_object_id="296648875", ) PartijIdentificatorFactory.create( partij=partij2, partij_identificator_code_soort_object_id="bsn", - partij_identificator_object_id="987654321", + partij_identificator_object_id="111222333", ) with self.subTest("happy flow"): response = self.client.get( self.url, - {"partijIdentificator__objectId": "123456789"}, + {"partijIdentificator__objectId": "296648875"}, ) self.assertEqual(response.status_code, status.HTTP_200_OK) diff --git a/src/openklant/components/klantinteracties/api/tests/test_partijen.py b/src/openklant/components/klantinteracties/api/tests/test_partijen.py index f97214a0..ee44ee60 100644 --- a/src/openklant/components/klantinteracties/api/tests/test_partijen.py +++ b/src/openklant/components/klantinteracties/api/tests/test_partijen.py @@ -1879,7 +1879,7 @@ def test_create_partij_indetificator(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456788", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -1895,7 +1895,7 @@ def test_create_partij_indetificator(self): { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456788", + "objectId": "296648875", "codeRegister": "brp", }, ) @@ -1907,7 +1907,7 @@ def test_update_partij_indetificator(self): andere_partij_identificator="anderePartijIdentificator", partij_identificator_code_objecttype="natuurlijk_persoon", partij_identificator_code_soort_object_id="bsn", - partij_identificator_object_id="123456788", + partij_identificator_object_id="296648875", partij_identificator_code_register="brp", ) @@ -1925,7 +1925,7 @@ def test_update_partij_indetificator(self): { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456788", + "objectId": "296648875", "codeRegister": "brp", }, ) @@ -1936,7 +1936,7 @@ def test_update_partij_indetificator(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -1952,7 +1952,7 @@ def test_update_partij_indetificator(self): { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, ) @@ -1964,7 +1964,7 @@ def test_partial_update_partij_indetificator(self): andere_partij_identificator="anderePartijIdentificator", partij_identificator_code_objecttype="natuurlijk_persoon", partij_identificator_code_soort_object_id="bsn", - partij_identificator_object_id="123456788", + partij_identificator_object_id="296648875", partij_identificator_code_register="brp", ) @@ -1982,7 +1982,7 @@ def test_partial_update_partij_indetificator(self): { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456788", + "objectId": "296648875", "codeRegister": "brp", }, ) @@ -2002,7 +2002,7 @@ def test_partial_update_partij_indetificator(self): { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456788", + "objectId": "296648875", "codeRegister": "brp", }, ) @@ -2030,7 +2030,7 @@ def test_invalid_choice_partij_identificator_code_register(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "test", }, } @@ -2057,7 +2057,7 @@ def test_invalid_choice_partij_identificator_code_objecttype(self): "partijIdentificator": { "codeObjecttype": "test", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -2084,7 +2084,7 @@ def test_invalid_choice_partij_identificator_code_soort_object_id(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "test", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -2111,7 +2111,7 @@ def test_invalid_validation_partij_identificator_code_objecttype(self): "partijIdentificator": { "codeObjecttype": "niet_natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -2137,8 +2137,8 @@ def test_invalid_validation_partij_identificator_code_soort_object_id(self): "anderePartijIdentificator": "anderePartijIdentificator", "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", - "codeSoortObjectId": "kvk_nummer", - "objectId": "123456789", + "codeSoortObjectId": "kvknummer", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -2182,7 +2182,7 @@ def test_invalid_validation_partij_identificator_object_id(self): self.assertEqual(response.data["invalid_params"][0]["code"], "invalid") self.assertEqual( response.data["invalid_params"][0]["reason"], - "De lengte van de objectId moet tussen 8 en 9 liggen.", + "Ongeldig BSN.", ) def test_valid_validation_partij_identificator(self): @@ -2195,7 +2195,7 @@ def test_valid_validation_partij_identificator(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": "123456789", + "objectId": "296648875", "codeRegister": "brp", }, } @@ -2210,7 +2210,7 @@ def test_valid_validation_partij_identificator(self): "bsn", ) self.assertEqual( - response.data["partij_identificator"]["object_id"], "123456789" + response.data["partij_identificator"]["object_id"], "296648875" ) self.assertEqual( response.data["partij_identificator"]["code_register"], @@ -2227,7 +2227,7 @@ def test_valid_overige_validation_partij_identificator(self): "partijIdentificator": { "codeObjecttype": "natuurlijk_persoon", "codeSoortObjectId": "bsn", - "objectId": 123456789, + "objectId": 296648875, "codeRegister": "overige", }, } @@ -2242,7 +2242,7 @@ def test_valid_overige_validation_partij_identificator(self): "bsn", ) self.assertEqual( - response.data["partij_identificator"]["object_id"], "123456789" + response.data["partij_identificator"]["object_id"], "296648875" ) self.assertEqual( response.data["partij_identificator"]["code_register"], diff --git a/src/openklant/components/klantinteracties/models/validators.py b/src/openklant/components/klantinteracties/models/validators.py index 5a9e7851..85beafe4 100644 --- a/src/openklant/components/klantinteracties/models/validators.py +++ b/src/openklant/components/klantinteracties/models/validators.py @@ -1,6 +1,7 @@ from django.utils.translation import gettext_lazy as _ - +from django.core.exceptions import ValidationError from rest_framework import serializers +from digid_eherkenning.validators import BSNValidator from .constants import ( PartijIdentificatorCodeObjectType, @@ -9,6 +10,22 @@ ) +def validate_digits(value: str) -> None: + if not value.isdigit(): + raise serializers.ValidationError( + { + "partijIdentificator.objectId": _( + "ObjectId Verwacht een numerieke waarde." + ) + } + ) + + +def validate_length(value: str, allowed_lengths: list) -> None: + if not len(value) in allowed_lengths: + raise serializers.ValidationError(_("Ongeldige veldlengte")) + + class PartijIdentificatorValidator: REGISTERS = { PartijIdentificatorCodeRegister.brp: { @@ -106,8 +123,10 @@ def validate_object_id(self) -> None: ): return - if validator := getattr(self, f"_validate_{self.code_soort_object_id}", None): - validator() + if validate_object_type := getattr( + self, f"_validate_{self.code_soort_object_id}", None + ): + validate_object_type() else: raise serializers.ValidationError( {"partijIdentificator.objectId": _("Ongeldige codeSoortObjectId.")} @@ -115,44 +134,31 @@ def validate_object_id(self) -> None: def _validate_bsn(self) -> None: """Validates the bsn object_id""" - if len(self.object_id) not in [8, 9]: + try: + bsn = BSNValidator() + bsn(self.object_id) + except ValidationError: raise serializers.ValidationError( - { - "partijIdentificator.objectId": _( - "De lengte van de objectId moet tussen 8 en 9 liggen." - ) - } + {"partijIdentificator.objectId": _("Ongeldig BSN.")} ) def _validate_vestigingsnummer(self) -> None: """Validates the vestigingsNummer object_id""" - if len(self.object_id) not in [12]: - raise serializers.ValidationError( - { - "partijIdentificator.objectId": _( - "De lengte van de objectId moet 12 tekens zijn." - ) - } - ) + validate_digits(self.object_id) + validate_length(self.object_id, [12]) def _validate_rsin(self) -> None: """Validates the rsin object_id""" - if len(self.object_id) not in [8, 9]: + validate_digits(self.object_id) + try: + rsin = BSNValidator() + rsin(self.object_id) + except ValidationError: raise serializers.ValidationError( - { - "partijIdentificator.objectId": _( - "De lengte van de objectId moet tussen 8 en 9 liggen." - ) - } + {"partijIdentificator.objectId": _("Ongeldig RSIN.")} ) def _validate_kvknummer(self) -> None: """Validates the kvkNummer object_id""" - if len(self.object_id) not in [8]: - raise serializers.ValidationError( - { - "partijIdentificator.objectId": _( - "De lengte van de objectId moet 8 tekens zijn." - ) - } - ) + validate_digits(self.object_id) + validate_length(self.object_id, [8, 9]) diff --git a/src/openklant/components/klantinteracties/tests/test_validators.py b/src/openklant/components/klantinteracties/tests/test_validators.py index 19170a5b..b6db56e1 100644 --- a/src/openklant/components/klantinteracties/tests/test_validators.py +++ b/src/openklant/components/klantinteracties/tests/test_validators.py @@ -1,5 +1,5 @@ from django.test import TestCase - +from django.core.exceptions import ValidationError from rest_framework import serializers from openklant.components.klantinteracties.models.constants import ( @@ -17,7 +17,7 @@ def test_valid(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -27,9 +27,9 @@ def test_valid(self): def test_valid_empty_code_objecttype(self): data = { - "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, + "code_objecttype": "", "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -39,7 +39,7 @@ def test_empty_code_register_ok_code_objecttype(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": "", } validator = PartijIdentificatorValidator(**data) @@ -49,7 +49,7 @@ def test_overige_code_register_ok_code_objecttype(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.overige.value, } validator = PartijIdentificatorValidator(**data) @@ -59,7 +59,7 @@ def test_code_objecttype_not_found_in_code_register(self): data = { "code_objecttype": "niet_natuurlijk_persoon", "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } with self.assertRaises(serializers.ValidationError) as error: @@ -91,7 +91,7 @@ def test_empty_code_objecttype_ok_code_soort_object_id(self): data = { "code_objecttype": "", "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -101,7 +101,7 @@ def test_oveirige_code_objecttype_ok_code_soort_object_id(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.overige.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -111,7 +111,7 @@ def test_code_soort_object_id_not_found_in_code_objecttype(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.rsin.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } with self.assertRaises(serializers.ValidationError) as error: @@ -163,7 +163,7 @@ def test_object_id_valid_bsn(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -173,7 +173,7 @@ def test_object_id_valid_vestigingsnummer(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.vestiging.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value, - "object_id": "123456789154", + "object_id": "296648875154", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -183,7 +183,7 @@ def test_object_id_valid_rsin(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.niet_natuurlijk_persoon.value, "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.rsin.value, - "object_id": "12345678", + "object_id": "296648875", "code_register": PartijIdentificatorCodeRegister.brp.value, } validator = PartijIdentificatorValidator(**data) @@ -212,15 +212,15 @@ def test_object_id_invalid_len_bsn(self): details = error.exception.get_full_details() self.assertEqual( details["partijIdentificator.objectId"]["message"], - "De lengte van de objectId moet tussen 8 en 9 liggen.", + "Ongeldig BSN.", ) self.assertEqual(details["partijIdentificator.objectId"]["code"], "invalid") - def test_object_id_invalid_len_vestigingsnummer(self): + def test_object_id_invalid_digit_bsn(self): data = { - "code_objecttype": PartijIdentificatorCodeObjectType.vestiging.value, - "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value, - "object_id": "1234", + "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, + "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, + "object_id": "123TEST123", "code_register": PartijIdentificatorCodeRegister.brp.value, } with self.assertRaises(serializers.ValidationError) as error: @@ -229,15 +229,15 @@ def test_object_id_invalid_len_vestigingsnummer(self): details = error.exception.get_full_details() self.assertEqual( details["partijIdentificator.objectId"]["message"], - "De lengte van de objectId moet 12 tekens zijn.", + "Ongeldig BSN.", ) self.assertEqual(details["partijIdentificator.objectId"]["code"], "invalid") - def test_object_id_invalid_len_rsin(self): + def test_object_id_invalid_proef11_bsn(self): data = { - "code_objecttype": PartijIdentificatorCodeObjectType.niet_natuurlijk_persoon.value, - "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.rsin.value, - "object_id": "1234", + "code_objecttype": PartijIdentificatorCodeObjectType.natuurlijk_persoon.value, + "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.bsn.value, + "object_id": "123456789", "code_register": PartijIdentificatorCodeRegister.brp.value, } with self.assertRaises(serializers.ValidationError) as error: @@ -246,14 +246,28 @@ def test_object_id_invalid_len_rsin(self): details = error.exception.get_full_details() self.assertEqual( details["partijIdentificator.objectId"]["message"], - "De lengte van de objectId moet tussen 8 en 9 liggen.", + "Ongeldig BSN.", ) self.assertEqual(details["partijIdentificator.objectId"]["code"], "invalid") - def test_object_id_invalid_len_kvknummer(self): + def test_object_id_invalid_vestigingsnummer(self): + data = { + "code_objecttype": PartijIdentificatorCodeObjectType.vestiging.value, + "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.vestigingsnummer.value, + "object_id": "1234", + "code_register": PartijIdentificatorCodeRegister.brp.value, + } + with self.assertRaises(serializers.ValidationError) as error: + validator = PartijIdentificatorValidator(**data) + validator.validate_object_id() + details = error.exception.get_full_details() + self.assertEqual(details[0]["message"], "Ongeldige veldlengte") + self.assertEqual(details[0]["code"], "invalid") + + def test_object_id_invalid_rsin(self): data = { "code_objecttype": PartijIdentificatorCodeObjectType.niet_natuurlijk_persoon.value, - "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.kvknummer.value, + "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.rsin.value, "object_id": "1234", "code_register": PartijIdentificatorCodeRegister.brp.value, } @@ -263,6 +277,20 @@ def test_object_id_invalid_len_kvknummer(self): details = error.exception.get_full_details() self.assertEqual( details["partijIdentificator.objectId"]["message"], - "De lengte van de objectId moet 8 tekens zijn.", + "Ongeldig RSIN.", ) self.assertEqual(details["partijIdentificator.objectId"]["code"], "invalid") + + def test_object_id_invalid_kvknummer(self): + data = { + "code_objecttype": PartijIdentificatorCodeObjectType.niet_natuurlijk_persoon.value, + "code_soort_object_id": PartijIdentificatorCodeSoortObjectId.kvknummer.value, + "object_id": "1234", + "code_register": PartijIdentificatorCodeRegister.brp.value, + } + with self.assertRaises(serializers.ValidationError) as error: + validator = PartijIdentificatorValidator(**data) + validator.validate_object_id() + details = error.exception.get_full_details() + self.assertEqual(details[0]["message"], "Ongeldige veldlengte") + self.assertEqual(details[0]["code"], "invalid")