From 195e5e75828d73644cd3c8b4a1e75e26f139e9c4 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 6 Dec 2023 11:57:10 +0100 Subject: [PATCH] [#3607] Use better error messages --- ...KValidatorTestCase.test_brk_validator.yaml | 22 ++++++------- .../contrib/brk/tests/test_validators.py | 7 +++-- src/openforms/contrib/brk/validators.py | 31 ++++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator.yaml b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator.yaml index 1d98710fd7..c2efbb58a3 100644 --- a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator.yaml +++ b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator.yaml @@ -18,19 +18,19 @@ interactions: uri: http://localhost:8150/kadastraalonroerendezaken?postcode=wrong&huisnummer=wrong response: body: - string: '{"detail":"Not Found"}' + string: '{"_embedded":{"kadastraalOnroerendeZaken":[]}}' headers: content-length: - - '22' + - '46' content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: - code: 404 - message: Not Found + code: 200 + message: OK - request: body: null headers: @@ -59,7 +59,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: @@ -91,7 +91,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: @@ -125,7 +125,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: @@ -157,7 +157,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: @@ -191,7 +191,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: @@ -223,7 +223,7 @@ interactions: content-type: - application/json date: - - Tue, 05 Dec 2023 17:40:19 GMT + - Wed, 06 Dec 2023 10:53:20 GMT server: - uvicorn status: diff --git a/src/openforms/contrib/brk/tests/test_validators.py b/src/openforms/contrib/brk/tests/test_validators.py index f41ebe5635..663f99eb25 100644 --- a/src/openforms/contrib/brk/tests/test_validators.py +++ b/src/openforms/contrib/brk/tests/test_validators.py @@ -29,7 +29,7 @@ def test_brk_validator(self): ) with self.assertRaisesMessage( - ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")} + ValidationError, _("No BSN is available to validate your address.") ): validator( {"postcode": "not_relevant", "houseNumber": "same"}, submission_no_bsn @@ -66,7 +66,7 @@ def test_brk_validator(self): ) with self.assertRaisesMessage( - ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")} + ValidationError, _("No property found for this address.") ): validator( {"postcode": "wrong", "houseNumber": "wrong"}, submission_not_hashed @@ -76,7 +76,8 @@ def test_brk_validator(self): validator({"postcode": "1234AA", "houseNumber": "123"}, submission_hashed) with self.assertRaisesMessage( - ValidationError, _("%(type)s does not exist.") % {"type": _("Owner")} + ValidationError, + _("You are not one of the registered owners of this property"), ): validator( {"postcode": "1234AA", "houseNumber": "123"}, submission_wrong_bsn diff --git a/src/openforms/contrib/brk/validators.py b/src/openforms/contrib/brk/validators.py index 48f02e5f3b..3fa837eca5 100644 --- a/src/openforms/contrib/brk/validators.py +++ b/src/openforms/contrib/brk/validators.py @@ -16,9 +16,9 @@ class AddressValue(TypedDict, total=False): postcode: str - housenumber: str - houseletter: str - housenumberaddition: str + houseNumber: str + houseLetter: str + houseNumberAddition: str @register( @@ -30,20 +30,21 @@ class AddressValue(TypedDict, total=False): class BRKZaakgerechtigdeValidator: error_messages = { - "not_found": _("%(type)s does not exist."), + "no_bsn": _("No BSN is available to validate your address."), + "retrieving_error": _( + "There was an error while retrieving the available properties. Please try again later" + ), + "not_owner": _("You are not one of the registered owners of this property"), + "multiple_cadastrals": _("We couldn't accurately determine your properties"), + "not_found": _("No property found for this address."), } def __call__(self, value: AddressValue, submission: Submission) -> bool: - validation_error = ValidationError( - self.error_messages["not_found"], - params={"type": _("Owner")}, - ) - try: client = get_client() except NoServiceConfigured as e: - raise validation_error from e + raise ValidationError(self.error_messages["retrieving_error"]) from e address_query: SearchParams = { "postcode": value["postcode"], @@ -57,7 +58,7 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool: # We assume submission has auth_info available, should we verify that this validator is used # on a step that requires login? if submission.auth_info.attribute != AuthAttribute.bsn: - raise validation_error + raise ValidationError(self.error_messages["no_bsn"]) try: with client: @@ -65,7 +66,9 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool: kadastraals = cadastrals_resp["_embedded"]["kadastraalOnroerendeZaken"] if len(kadastraals) > 1: # The query by address returned more than one cadastral, this shouldn't happen - raise validation_error + raise ValidationError(self.error_messages["multiple_cadastrals"]) + if not kadastraals: + raise ValidationError(self.error_messages["not_found"]) kadastraal_id = kadastraals[0]["identificatie"] titleholders_resp = client.get_cadastral_titleholders_by_cadastral_id( @@ -85,9 +88,9 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool: is_valid = submission.auth_info.value in bsns if not is_valid: - raise validation_error + raise ValidationError(self.error_messages["not_owner"]) except (RequestException, KeyError, TypeError) as e: - raise validation_error from e + raise ValidationError(self.error_messages["retrieving_error"]) from e return True