Skip to content

Commit e6ee4ab

Browse files
[#233] Fix tests
1 parent 5ca167e commit e6ee4ab

File tree

4 files changed

+116
-82
lines changed

4 files changed

+116
-82
lines changed

src/openklant/components/klantinteracties/api/tests/test_filters.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ def test_filter_identificator_soort_object_id(self):
728728
partij=partij, partij_identificator_code_soort_object_id="bsn"
729729
)
730730
PartijIdentificatorFactory.create(
731-
partij=partij2, partij_identificator_code_soort_object_id="kvk_nummer"
731+
partij=partij2, partij_identificator_code_soort_object_id="kvknummer"
732732
)
733733

734734
with self.subTest("happy flow"):
@@ -759,18 +759,18 @@ def test_filter_identificator_object_id(self):
759759
PartijIdentificatorFactory.create(
760760
partij=partij,
761761
partij_identificator_code_soort_object_id="bsn",
762-
partij_identificator_object_id="123456789",
762+
partij_identificator_object_id="296648875",
763763
)
764764
PartijIdentificatorFactory.create(
765765
partij=partij2,
766766
partij_identificator_code_soort_object_id="bsn",
767-
partij_identificator_object_id="987654321",
767+
partij_identificator_object_id="111222333",
768768
)
769769

770770
with self.subTest("happy flow"):
771771
response = self.client.get(
772772
self.url,
773-
{"partijIdentificator__objectId": "123456789"},
773+
{"partijIdentificator__objectId": "296648875"},
774774
)
775775
self.assertEqual(response.status_code, status.HTTP_200_OK)
776776

src/openklant/components/klantinteracties/api/tests/test_partijen.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -1879,7 +1879,7 @@ def test_create_partij_indetificator(self):
18791879
"partijIdentificator": {
18801880
"codeObjecttype": "natuurlijk_persoon",
18811881
"codeSoortObjectId": "bsn",
1882-
"objectId": "123456788",
1882+
"objectId": "296648875",
18831883
"codeRegister": "brp",
18841884
},
18851885
}
@@ -1895,7 +1895,7 @@ def test_create_partij_indetificator(self):
18951895
{
18961896
"codeObjecttype": "natuurlijk_persoon",
18971897
"codeSoortObjectId": "bsn",
1898-
"objectId": "123456788",
1898+
"objectId": "296648875",
18991899
"codeRegister": "brp",
19001900
},
19011901
)
@@ -1907,7 +1907,7 @@ def test_update_partij_indetificator(self):
19071907
andere_partij_identificator="anderePartijIdentificator",
19081908
partij_identificator_code_objecttype="natuurlijk_persoon",
19091909
partij_identificator_code_soort_object_id="bsn",
1910-
partij_identificator_object_id="123456788",
1910+
partij_identificator_object_id="296648875",
19111911
partij_identificator_code_register="brp",
19121912
)
19131913

@@ -1925,7 +1925,7 @@ def test_update_partij_indetificator(self):
19251925
{
19261926
"codeObjecttype": "natuurlijk_persoon",
19271927
"codeSoortObjectId": "bsn",
1928-
"objectId": "123456788",
1928+
"objectId": "296648875",
19291929
"codeRegister": "brp",
19301930
},
19311931
)
@@ -1936,7 +1936,7 @@ def test_update_partij_indetificator(self):
19361936
"partijIdentificator": {
19371937
"codeObjecttype": "natuurlijk_persoon",
19381938
"codeSoortObjectId": "bsn",
1939-
"objectId": "123456789",
1939+
"objectId": "296648875",
19401940
"codeRegister": "brp",
19411941
},
19421942
}
@@ -1952,7 +1952,7 @@ def test_update_partij_indetificator(self):
19521952
{
19531953
"codeObjecttype": "natuurlijk_persoon",
19541954
"codeSoortObjectId": "bsn",
1955-
"objectId": "123456789",
1955+
"objectId": "296648875",
19561956
"codeRegister": "brp",
19571957
},
19581958
)
@@ -1964,7 +1964,7 @@ def test_partial_update_partij_indetificator(self):
19641964
andere_partij_identificator="anderePartijIdentificator",
19651965
partij_identificator_code_objecttype="natuurlijk_persoon",
19661966
partij_identificator_code_soort_object_id="bsn",
1967-
partij_identificator_object_id="123456788",
1967+
partij_identificator_object_id="296648875",
19681968
partij_identificator_code_register="brp",
19691969
)
19701970

@@ -1982,7 +1982,7 @@ def test_partial_update_partij_indetificator(self):
19821982
{
19831983
"codeObjecttype": "natuurlijk_persoon",
19841984
"codeSoortObjectId": "bsn",
1985-
"objectId": "123456788",
1985+
"objectId": "296648875",
19861986
"codeRegister": "brp",
19871987
},
19881988
)
@@ -2002,7 +2002,7 @@ def test_partial_update_partij_indetificator(self):
20022002
{
20032003
"codeObjecttype": "natuurlijk_persoon",
20042004
"codeSoortObjectId": "bsn",
2005-
"objectId": "123456788",
2005+
"objectId": "296648875",
20062006
"codeRegister": "brp",
20072007
},
20082008
)
@@ -2030,7 +2030,7 @@ def test_invalid_choice_partij_identificator_code_register(self):
20302030
"partijIdentificator": {
20312031
"codeObjecttype": "natuurlijk_persoon",
20322032
"codeSoortObjectId": "bsn",
2033-
"objectId": "123456789",
2033+
"objectId": "296648875",
20342034
"codeRegister": "test",
20352035
},
20362036
}
@@ -2057,7 +2057,7 @@ def test_invalid_choice_partij_identificator_code_objecttype(self):
20572057
"partijIdentificator": {
20582058
"codeObjecttype": "test",
20592059
"codeSoortObjectId": "bsn",
2060-
"objectId": "123456789",
2060+
"objectId": "296648875",
20612061
"codeRegister": "brp",
20622062
},
20632063
}
@@ -2084,7 +2084,7 @@ def test_invalid_choice_partij_identificator_code_soort_object_id(self):
20842084
"partijIdentificator": {
20852085
"codeObjecttype": "natuurlijk_persoon",
20862086
"codeSoortObjectId": "test",
2087-
"objectId": "123456789",
2087+
"objectId": "296648875",
20882088
"codeRegister": "brp",
20892089
},
20902090
}
@@ -2111,7 +2111,7 @@ def test_invalid_validation_partij_identificator_code_objecttype(self):
21112111
"partijIdentificator": {
21122112
"codeObjecttype": "niet_natuurlijk_persoon",
21132113
"codeSoortObjectId": "bsn",
2114-
"objectId": "123456789",
2114+
"objectId": "296648875",
21152115
"codeRegister": "brp",
21162116
},
21172117
}
@@ -2137,8 +2137,8 @@ def test_invalid_validation_partij_identificator_code_soort_object_id(self):
21372137
"anderePartijIdentificator": "anderePartijIdentificator",
21382138
"partijIdentificator": {
21392139
"codeObjecttype": "natuurlijk_persoon",
2140-
"codeSoortObjectId": "kvk_nummer",
2141-
"objectId": "123456789",
2140+
"codeSoortObjectId": "kvknummer",
2141+
"objectId": "296648875",
21422142
"codeRegister": "brp",
21432143
},
21442144
}
@@ -2182,7 +2182,7 @@ def test_invalid_validation_partij_identificator_object_id(self):
21822182
self.assertEqual(response.data["invalid_params"][0]["code"], "invalid")
21832183
self.assertEqual(
21842184
response.data["invalid_params"][0]["reason"],
2185-
"De lengte van de objectId moet tussen 8 en 9 liggen.",
2185+
"Ongeldig BSN.",
21862186
)
21872187

21882188
def test_valid_validation_partij_identificator(self):
@@ -2195,7 +2195,7 @@ def test_valid_validation_partij_identificator(self):
21952195
"partijIdentificator": {
21962196
"codeObjecttype": "natuurlijk_persoon",
21972197
"codeSoortObjectId": "bsn",
2198-
"objectId": "123456789",
2198+
"objectId": "296648875",
21992199
"codeRegister": "brp",
22002200
},
22012201
}
@@ -2210,7 +2210,7 @@ def test_valid_validation_partij_identificator(self):
22102210
"bsn",
22112211
)
22122212
self.assertEqual(
2213-
response.data["partij_identificator"]["object_id"], "123456789"
2213+
response.data["partij_identificator"]["object_id"], "296648875"
22142214
)
22152215
self.assertEqual(
22162216
response.data["partij_identificator"]["code_register"],
@@ -2227,7 +2227,7 @@ def test_valid_overige_validation_partij_identificator(self):
22272227
"partijIdentificator": {
22282228
"codeObjecttype": "natuurlijk_persoon",
22292229
"codeSoortObjectId": "bsn",
2230-
"objectId": 123456789,
2230+
"objectId": 296648875,
22312231
"codeRegister": "overige",
22322232
},
22332233
}
@@ -2242,7 +2242,7 @@ def test_valid_overige_validation_partij_identificator(self):
22422242
"bsn",
22432243
)
22442244
self.assertEqual(
2245-
response.data["partij_identificator"]["object_id"], "123456789"
2245+
response.data["partij_identificator"]["object_id"], "296648875"
22462246
)
22472247
self.assertEqual(
22482248
response.data["partij_identificator"]["code_register"],

src/openklant/components/klantinteracties/models/validators.py

+37-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.utils.translation import gettext_lazy as _
2-
2+
from django.core.exceptions import ValidationError
33
from rest_framework import serializers
4+
from digid_eherkenning.validators import BSNValidator
45

56
from .constants import (
67
PartijIdentificatorCodeObjectType,
@@ -9,6 +10,22 @@
910
)
1011

1112

13+
def validate_digits(value: str) -> None:
14+
if not value.isdigit():
15+
raise serializers.ValidationError(
16+
{
17+
"partijIdentificator.objectId": _(
18+
"ObjectId Verwacht een numerieke waarde."
19+
)
20+
}
21+
)
22+
23+
24+
def validate_length(value: str, allowed_lengths: list) -> None:
25+
if not len(value) in allowed_lengths:
26+
raise serializers.ValidationError(_("Ongeldige veldlengte"))
27+
28+
1229
class PartijIdentificatorValidator:
1330
REGISTERS = {
1431
PartijIdentificatorCodeRegister.brp: {
@@ -106,53 +123,42 @@ def validate_object_id(self) -> None:
106123
):
107124
return
108125

109-
if validator := getattr(self, f"_validate_{self.code_soort_object_id}", None):
110-
validator()
126+
if validate_object_type := getattr(
127+
self, f"_validate_{self.code_soort_object_id}", None
128+
):
129+
validate_object_type()
111130
else:
112131
raise serializers.ValidationError(
113132
{"partijIdentificator.objectId": _("Ongeldige codeSoortObjectId.")}
114133
)
115134

116135
def _validate_bsn(self) -> None:
117136
"""Validates the bsn object_id"""
118-
if len(self.object_id) not in [8, 9]:
137+
try:
138+
bsn = BSNValidator()
139+
bsn(self.object_id)
140+
except ValidationError:
119141
raise serializers.ValidationError(
120-
{
121-
"partijIdentificator.objectId": _(
122-
"De lengte van de objectId moet tussen 8 en 9 liggen."
123-
)
124-
}
142+
{"partijIdentificator.objectId": _("Ongeldig BSN.")}
125143
)
126144

127145
def _validate_vestigingsnummer(self) -> None:
128146
"""Validates the vestigingsNummer object_id"""
129-
if len(self.object_id) not in [12]:
130-
raise serializers.ValidationError(
131-
{
132-
"partijIdentificator.objectId": _(
133-
"De lengte van de objectId moet 12 tekens zijn."
134-
)
135-
}
136-
)
147+
validate_digits(self.object_id)
148+
validate_length(self.object_id, [12])
137149

138150
def _validate_rsin(self) -> None:
139151
"""Validates the rsin object_id"""
140-
if len(self.object_id) not in [8, 9]:
152+
validate_digits(self.object_id)
153+
try:
154+
rsin = BSNValidator()
155+
rsin(self.object_id)
156+
except ValidationError:
141157
raise serializers.ValidationError(
142-
{
143-
"partijIdentificator.objectId": _(
144-
"De lengte van de objectId moet tussen 8 en 9 liggen."
145-
)
146-
}
158+
{"partijIdentificator.objectId": _("Ongeldig RSIN.")}
147159
)
148160

149161
def _validate_kvknummer(self) -> None:
150162
"""Validates the kvkNummer object_id"""
151-
if len(self.object_id) not in [8]:
152-
raise serializers.ValidationError(
153-
{
154-
"partijIdentificator.objectId": _(
155-
"De lengte van de objectId moet 8 tekens zijn."
156-
)
157-
}
158-
)
163+
validate_digits(self.object_id)
164+
validate_length(self.object_id, [8, 9])

0 commit comments

Comments
 (0)