Skip to content

Commit

Permalink
[#233] Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmursa-dev committed Dec 20, 2024
1 parent 5ca167e commit e6ee4ab
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1879,7 +1879,7 @@ def test_create_partij_indetificator(self):
"partijIdentificator": {
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456788",
"objectId": "296648875",
"codeRegister": "brp",
},
}
Expand All @@ -1895,7 +1895,7 @@ def test_create_partij_indetificator(self):
{
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456788",
"objectId": "296648875",
"codeRegister": "brp",
},
)
Expand All @@ -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",
)

Expand All @@ -1925,7 +1925,7 @@ def test_update_partij_indetificator(self):
{
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456788",
"objectId": "296648875",
"codeRegister": "brp",
},
)
Expand All @@ -1936,7 +1936,7 @@ def test_update_partij_indetificator(self):
"partijIdentificator": {
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456789",
"objectId": "296648875",
"codeRegister": "brp",
},
}
Expand All @@ -1952,7 +1952,7 @@ def test_update_partij_indetificator(self):
{
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456789",
"objectId": "296648875",
"codeRegister": "brp",
},
)
Expand All @@ -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",
)

Expand All @@ -1982,7 +1982,7 @@ def test_partial_update_partij_indetificator(self):
{
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456788",
"objectId": "296648875",
"codeRegister": "brp",
},
)
Expand All @@ -2002,7 +2002,7 @@ def test_partial_update_partij_indetificator(self):
{
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456788",
"objectId": "296648875",
"codeRegister": "brp",
},
)
Expand Down Expand Up @@ -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",
},
}
Expand All @@ -2057,7 +2057,7 @@ def test_invalid_choice_partij_identificator_code_objecttype(self):
"partijIdentificator": {
"codeObjecttype": "test",
"codeSoortObjectId": "bsn",
"objectId": "123456789",
"objectId": "296648875",
"codeRegister": "brp",
},
}
Expand All @@ -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",
},
}
Expand All @@ -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",
},
}
Expand All @@ -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",
},
}
Expand Down Expand Up @@ -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):
Expand All @@ -2195,7 +2195,7 @@ def test_valid_validation_partij_identificator(self):
"partijIdentificator": {
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": "123456789",
"objectId": "296648875",
"codeRegister": "brp",
},
}
Expand All @@ -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"],
Expand All @@ -2227,7 +2227,7 @@ def test_valid_overige_validation_partij_identificator(self):
"partijIdentificator": {
"codeObjecttype": "natuurlijk_persoon",
"codeSoortObjectId": "bsn",
"objectId": 123456789,
"objectId": 296648875,
"codeRegister": "overige",
},
}
Expand All @@ -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"],
Expand Down
68 changes: 37 additions & 31 deletions src/openklant/components/klantinteracties/models/validators.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -106,53 +123,42 @@ 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.")}
)

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])
Loading

0 comments on commit e6ee4ab

Please sign in to comment.