Skip to content

Commit

Permalink
[#5006] Updated AddressValueSerializer to adapt manual filling of str…
Browse files Browse the repository at this point in the history
…eetname and city

Updated street name and city serializer fields in order to be able to
require these fields in case the component is required.
  • Loading branch information
vaszig committed Jan 17, 2025
1 parent 52cb077 commit cde749b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/openforms/contrib/brk/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ class AddressValue(TypedDict):
city: NotRequired[str]
streetName: NotRequired[str]
secretStreetCity: NotRequired[str]
autoPopulated: NotRequired[bool]
30 changes: 26 additions & 4 deletions src/openforms/formio/components/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,27 +410,43 @@ class AddressValueSerializer(serializers.Serializer):
streetName = serializers.CharField(
label=_("street name"),
help_text=_("Derived street name"),
required=False,
allow_blank=True,
)
city = serializers.CharField(
label=_("city"),
help_text=_("Derived city"),
required=False,
allow_blank=True,
)
secretStreetCity = serializers.CharField(
label=_("city and street name secret"),
help_text=_("Secret for the combination of city and street name"),
required=False,
allow_blank=True,
)
autoPopulated = serializers.BooleanField(
label=_("city and street name auto populated"),
help_text=_("Whether city and street name have been retrieved from the API"),
default=False,
)

def __init__(self, **kwargs):
self.derive_address = kwargs.pop("derive_address", None)
self.component = kwargs.pop("component", None)
super().__init__(**kwargs)

def get_fields(self):
fields = super().get_fields()

# Street name and city are only required when the addressNL component is
# required, so we modify the 'required' and 'allow_blank' accordingly
if self.component and (
not (validate := self.component.get("validate"))
or validate.get("required") is False
):
fields["city"].required = False
fields["city"].allow_blank = True
fields["streetName"].required = False
fields["streetName"].allow_blank = True
return fields

def validate_city(self, value: str) -> str:
if city_regex := glom(
self.component, "openForms.components.city.validate.pattern", default=""
Expand All @@ -457,10 +473,16 @@ def validate_postcode(self, value: str) -> str:
def validate(self, attrs):
attrs = super().validate(attrs)

auto_populated = attrs.get("autoPopulated", False)
city = attrs.get("city", "")
street_name = attrs.get("streetName", "")

if self.derive_address:
# when the user fills in manually the city and the street name we do not
# need to check the secret city - street name combination
if not auto_populated:
return attrs

existing_hmac = attrs.get("secretStreetCity", "")
postcode = attrs.get("postcode", "")
number = attrs.get("houseNumber", "")
Expand Down
1 change: 1 addition & 0 deletions src/openforms/formio/formatters/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class AddressValue(TypedDict):
city: NotRequired[str]
streetName: NotRequired[str]
secretStreetCity: NotRequired[str]
autoPopulated: NotRequired[bool]


class AddressNLFormatter(FormatterBase):
Expand Down
62 changes: 60 additions & 2 deletions src/openforms/formio/tests/validation/test_addressnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def test_addressNL_field_regex_pattern_success(self):

self.assertTrue(is_valid)

def test_missing_keys(self):
def test_missing_keys_when_component_optional(self):
component: AddressNLComponent = {
"key": "addressNl",
"type": "addressNL",
Expand All @@ -129,6 +129,34 @@ def test_missing_keys(self):
self.assertEqual(postcode_error.code, "required")
self.assertEqual(house_number_error.code, "required")

def test_missing_keys_when_component_required(self):
component: AddressNLComponent = {
"key": "addressNl",
"type": "addressNL",
"label": "AddressNL missing keys",
"deriveAddress": False,
"validate": {"required": True},
}

invalid_values = {
"addressNl": {
"houseLetter": "A",
}
}

is_valid, errors = validate_formio_data(component, invalid_values)

postcode_error = extract_error(errors["addressNl"], "postcode")
house_number_error = extract_error(errors["addressNl"], "houseNumber")
street_name_error = extract_error(errors["addressNl"], "streetName")
city_error = extract_error(errors["addressNl"], "city")

self.assertFalse(is_valid)
self.assertEqual(postcode_error.code, "required")
self.assertEqual(house_number_error.code, "required")
self.assertEqual(street_name_error.code, "required")
self.assertEqual(city_error.code, "required")

def test_plugin_validator(self):
with replace_validators_registry() as register:
register("postcode_validator")(PostcodeValidator)
Expand All @@ -150,6 +178,8 @@ def test_plugin_validator(self):
"houseNumber": "3",
"houseLetter": "A",
"houseNumberAddition": "",
"streetName": "Keizersgracht",
"city": "Amsterdam",
}
},
)
Expand All @@ -176,7 +206,7 @@ def test_addressNL_field_secret_success(self):
"key": "addressNl",
"type": "addressNL",
"label": "AddressNL secret success",
"deriveAddress": False,
"deriveAddress": True,
}

message = "1015CJ/117/Amsterdam/Keizersgracht"
Expand All @@ -190,6 +220,7 @@ def test_addressNL_field_secret_success(self):
"city": "Amsterdam",
"streetName": "Keizersgracht",
"secretStreetCity": secret,
"autoPopulated": True,
}
}

Expand All @@ -214,6 +245,7 @@ def test_addressNL_field_secret_failure(self):
"city": "Amsterdam",
"streetName": "Keizersgracht",
"secretStreetCity": "invalid secret",
"autoPopulated": True,
}
}

Expand All @@ -224,6 +256,32 @@ def test_addressNL_field_secret_failure(self):
self.assertFalse(is_valid)
self.assertEqual(secret_error.code, "invalid")

def test_addressNL_field_secret_not_used_when_manual_address(self):
component: AddressNLComponent = {
"key": "addressNl",
"type": "addressNL",
"label": "AddressNL secret failure",
"deriveAddress": True,
"validate": {"required": False},
}

data = {
"addressNl": {
"postcode": "1015CJ",
"houseNumber": "117",
"houseLetter": "",
"houseNumberAddition": "",
"city": "Amsterdam",
"streetName": "Keizersgracht",
"secretStreetCity": "a secret",
"autoPopulated": False,
}
}

is_valid, _ = validate_formio_data(component, data)

self.assertTrue(is_valid)

def test_addressNL_field_missing_city(self):
component: AddressNLComponent = {
"key": "addressNl",
Expand Down

0 comments on commit cde749b

Please sign in to comment.