From a13d020f8588a6bb09ad7ea45afb7165a6d55c0b Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 3 Jan 2024 14:39:14 +0100 Subject: [PATCH] [#3607] Improve coverage --- src/openforms/contrib/brk/client.py | 2 +- ...idatorTestCase.test_brk_validator_bsn.yaml | 44 +++++++++++++++-- ...TestCase.test_brk_validator_wrong_bsn.yaml | 4 +- .../contrib/brk/tests/test_validators.py | 48 +++++++++++++++++++ src/openforms/contrib/brk/validators.py | 4 +- src/openforms/validations/registry.py | 3 ++ 6 files changed, 97 insertions(+), 8 deletions(-) diff --git a/src/openforms/contrib/brk/client.py b/src/openforms/contrib/brk/client.py index cddca38183..1cdb9c4c01 100644 --- a/src/openforms/contrib/brk/client.py +++ b/src/openforms/contrib/brk/client.py @@ -20,7 +20,7 @@ def get_client() -> "BRKClient": config = BRKConfig.get_solo() assert isinstance(config, BRKConfig) if not (service := config.service): - raise NoServiceConfigured("No KVK service configured!") + raise NoServiceConfigured("No BRK service configured!") service_client_factory = ServiceClientFactory(service) return BRKClient.configure_from(service_client_factory) diff --git a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_bsn.yaml b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_bsn.yaml index e20eb8f1cf..d79376e4d5 100644 --- a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_bsn.yaml +++ b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_bsn.yaml @@ -29,7 +29,45 @@ interactions: Content-Type: - application/hal+json Date: - - Tue, 02 Jan 2024 13:19:39 GMT + - Wed, 03 Jan 2024 13:36:10 GMT + Keep-Alive: + - timeout=60 + Server: + - Unspecified + status: + code: 200 + message: '' +- request: + body: null + headers: + Accept: + - '*/*' + Accept-Encoding: + - gzip, deflate, br + Connection: + - keep-alive + User-Agent: + - python-requests/2.31.0 + X-Api-Key: + - fake_changeme_when_testing + method: GET + uri: https://api.brk.kadaster.nl/esd-eto-apikey/bevragen/v2/kadastraalonroerendezaken?postcode=7361EW&huisnummer=21&huisletter=A&huisnummertoevoeging=B + response: + body: + string: '{"_links":{"self":{"href":"/kadastraalonroerendezaken?postcode=7361EW&huisnummer=21&huisletter=A&huisnummertoevoeging=B"}},"_embedded":{}}' + headers: + Api-Version: + - 2.0.0 + Connection: + - keep-alive + Content-Encoding: + - gzip + Content-Length: + - '135' + Content-Type: + - application/hal+json + Date: + - Wed, 03 Jan 2024 13:36:11 GMT Keep-Alive: - timeout=60 Server: @@ -72,7 +110,7 @@ interactions: Content-Type: - application/hal+json Date: - - Tue, 02 Jan 2024 13:19:40 GMT + - Wed, 03 Jan 2024 13:36:10 GMT Keep-Alive: - timeout=60 Server: @@ -116,7 +154,7 @@ interactions: Content-Type: - application/hal+json Date: - - Tue, 02 Jan 2024 13:19:40 GMT + - Wed, 03 Jan 2024 13:36:11 GMT Keep-Alive: - timeout=60 Server: diff --git a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_wrong_bsn.yaml b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_wrong_bsn.yaml index 3829e658b1..7f12bf5615 100644 --- a/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_wrong_bsn.yaml +++ b/src/openforms/contrib/brk/tests/files/vcr_cassettes/BRKValidatorTestCase/BRKValidatorTestCase.test_brk_validator_wrong_bsn.yaml @@ -34,7 +34,7 @@ interactions: Content-Type: - application/hal+json Date: - - Tue, 02 Jan 2024 13:19:40 GMT + - Wed, 03 Jan 2024 13:36:10 GMT Keep-Alive: - timeout=60 Server: @@ -78,7 +78,7 @@ interactions: Content-Type: - application/hal+json Date: - - Tue, 02 Jan 2024 13:19:40 GMT + - Wed, 03 Jan 2024 13:36:11 GMT Keep-Alive: - timeout=60 Server: diff --git a/src/openforms/contrib/brk/tests/test_validators.py b/src/openforms/contrib/brk/tests/test_validators.py index b224519b7a..8148a2548f 100644 --- a/src/openforms/contrib/brk/tests/test_validators.py +++ b/src/openforms/contrib/brk/tests/test_validators.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + from django.core.exceptions import ValidationError from django.test import TestCase from django.utils.translation import gettext as _ @@ -6,6 +8,7 @@ from privates.test import temp_private_root from openforms.authentication.constants import AuthAttribute +from openforms.contrib.brk.models import BRKConfig from openforms.submissions.tests.factories import SubmissionFactory from openforms.utils.tests.vcr import OFVCRMixin @@ -87,6 +90,19 @@ def test_brk_validator_bsn(self): ): validator({"postcode": "1234AA", "house_number": "1"}, submission_bsn) + with self.assertRaisesMessage( + ValidationError, _("No property found for this address.") + ): + validator( + { + "postcode": "7361EW", + "house_number": "21", + "house_letter": "A", + "house_number_addition": "B", + }, + submission_bsn, + ) + try: validator({"postcode": "7361EW", "house_number": "21"}, submission_bsn) except ValidationError as exc: @@ -120,3 +136,35 @@ def test_brk_validator_requests_error(self, m: requests_mock.Mocker): ), ): validator({"postcode": "1234AA", "house_number": "1"}, submission_bsn) + + +class BRKValidatorNotConfiguredTestCase(TestCase): + def setUp(self): + + patcher = patch( + "openforms.contrib.brk.client.BRKConfig.get_solo", + return_value=BRKConfig(), + ) + self.config_mock = patcher.start() + self.addCleanup(patcher.stop) + + def test_brk_validator_not_configured(self): + validator = BRKZakelijkGerechtigdeValidator() + + submission_bsn = SubmissionFactory.create( + form__generate_minimal_setup=True, + form__authentication_backends=["demo"], + form__formstep__form_definition__login_required=False, + auth_info__attribute_hashed=False, + auth_info__attribute=AuthAttribute.bsn, + auth_info__value="71291440", + auth_info__plugin="demo", + ) + + with self.assertRaisesMessage( + ValidationError, + _( + "There was an error while retrieving the available properties. Please try again later." + ), + ): + validator({"postcode": "1234AA", "house_number": "1"}, submission_bsn) diff --git a/src/openforms/contrib/brk/validators.py b/src/openforms/contrib/brk/validators.py index 7701702400..31c8383c30 100644 --- a/src/openforms/contrib/brk/validators.py +++ b/src/openforms/contrib/brk/validators.py @@ -107,9 +107,9 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool: "postcode": value["postcode"], "huisnummer": value["house_number"], } - if "houseLetter" in value: + if "house_letter" in value: address_query["huisletter"] = value["house_letter"] - if "houseNumberAddition" in value: + if "house_number_addition" in value: address_query["huisnummertoevoeging"] = value["house_number_addition"] with (client, suppress_api_errors(self.error_messages["retrieving_error"])): diff --git a/src/openforms/validations/registry.py b/src/openforms/validations/registry.py index 8867c6cc0a..670fe6c7df 100644 --- a/src/openforms/validations/registry.py +++ b/src/openforms/validations/registry.py @@ -138,6 +138,9 @@ def validate( # first, run the cheap validation to check that the data actually conforms to the expected schema. # only if that succeeds we may invoke the actual validator which potentially performs expensive # (network) checks. + # TODO this will raise a 400 and will not have the same structure as below. This should only + # raise here if someone's playing with the API directly. Otherwise, the SDK should perform the + # necessary checks before making a call. serializer.is_valid(raise_exception=True) try: validator(serializer.data["value"], submission)