From f504f530698928f15b76045fcf1adde39a07c362 Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Thu, 4 Jan 2024 09:56:05 +0100 Subject: [PATCH] [#3607] PR feedback 3 --- src/openforms/contrib/brk/checks.py | 2 +- src/openforms/contrib/brk/client.py | 6 +++--- src/openforms/contrib/brk/constants.py | 10 ++++++++++ src/openforms/contrib/brk/tests/test_client.py | 8 ++++---- src/openforms/contrib/brk/tests/test_validators.py | 1 + src/openforms/contrib/brk/validators.py | 13 +++---------- src/openforms/formio/formatters/custom.py | 4 +++- src/openforms/validations/tests/test_api.py | 8 +++----- src/openforms/validations/tests/test_registry.py | 2 +- 9 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 src/openforms/contrib/brk/constants.py diff --git a/src/openforms/contrib/brk/checks.py b/src/openforms/contrib/brk/checks.py index 0614170214..255eba53e6 100644 --- a/src/openforms/contrib/brk/checks.py +++ b/src/openforms/contrib/brk/checks.py @@ -17,7 +17,7 @@ class BRKValidatorCheck: def check_config(): try: with get_client() as client: - results = client.get_cadastrals_by_address( + results = client.get_real_estate_by_address( {"postcode": "1234AB", "huisnummer": "1"} ) except NoServiceConfigured as exc: diff --git a/src/openforms/contrib/brk/client.py b/src/openforms/contrib/brk/client.py index 1cdb9c4c01..2a2fa6d173 100644 --- a/src/openforms/contrib/brk/client.py +++ b/src/openforms/contrib/brk/client.py @@ -33,9 +33,9 @@ class SearchParams(TypedDict): class BRKClient(APIClient): - def get_cadastrals_by_address(self, query_params: SearchParams): + def get_real_estate_by_address(self, query_params: SearchParams): """ - Search for cadastrals by querying for a specifc address. + Search for real estate by querying for a specific address. API docs: https://vng-realisatie.github.io/Haal-Centraal-BRK-bevragen/swagger-ui-2.0#/Kadastraal%20Onroerende%20Zaken/GetKadastraalOnroerendeZaken """ @@ -55,7 +55,7 @@ def get_cadastrals_by_address(self, query_params: SearchParams): def get_cadastral_titleholders_by_cadastral_id(self, cadastral_id: str): """ - Search for commercial titleholders of a cadastral immovable property. + Look up the rightholders of a property (e.g. a house) in the Dutch cadastre. API docs: https://vng-realisatie.github.io/Haal-Centraal-BRK-bevragen/swagger-ui-2.0#/Zakelijke%20Gerechtigden/GetZakelijkGerechtigden """ diff --git a/src/openforms/contrib/brk/constants.py b/src/openforms/contrib/brk/constants.py new file mode 100644 index 0000000000..c39b4962d4 --- /dev/null +++ b/src/openforms/contrib/brk/constants.py @@ -0,0 +1,10 @@ +from typing import TypedDict + +from typing_extensions import NotRequired + + +class AddressValue(TypedDict): + postcode: str + house_number: str + house_letter: NotRequired[str] + house_number_addition: NotRequired[str] diff --git a/src/openforms/contrib/brk/tests/test_client.py b/src/openforms/contrib/brk/tests/test_client.py index bf768b9eee..9000411d80 100644 --- a/src/openforms/contrib/brk/tests/test_client.py +++ b/src/openforms/contrib/brk/tests/test_client.py @@ -17,7 +17,7 @@ class BRKCadastralClientTests(OFVCRMixin, BRKTestMixin, SimpleTestCase): def test_client(self): with get_client() as client: - res = client.get_cadastrals_by_address( + res = client.get_real_estate_by_address( {"postcode": "7361EW", "huisnummer": "21"} ) @@ -28,7 +28,7 @@ def test_client(self): def test_client_404(self): with get_client() as client: - res = client.get_cadastrals_by_address( + res = client.get_real_estate_by_address( {"postcode": "1234AB", "huisnummer": "1"} # Does not exist ) self.assertEqual(res["_embedded"], {}) @@ -39,7 +39,7 @@ def test_client_500(self, m): with get_client() as client: with self.assertRaises(requests.RequestException): - client.get_cadastrals_by_address( + client.get_real_estate_by_address( {"postcode": "1234AA", "huisnummer": "123"} ) @@ -71,4 +71,4 @@ def test_client_500(self, m): with get_client() as client: with self.assertRaises(requests.RequestException): - client.get_cadastrals_by_address("cadastral_id") + client.get_real_estate_by_address("cadastral_id") diff --git a/src/openforms/contrib/brk/tests/test_validators.py b/src/openforms/contrib/brk/tests/test_validators.py index 8148a2548f..a86ae4acec 100644 --- a/src/openforms/contrib/brk/tests/test_validators.py +++ b/src/openforms/contrib/brk/tests/test_validators.py @@ -140,6 +140,7 @@ def test_brk_validator_requests_error(self, m: requests_mock.Mocker): class BRKValidatorNotConfiguredTestCase(TestCase): def setUp(self): + super().setUp() patcher = patch( "openforms.contrib.brk.client.BRKConfig.get_solo", diff --git a/src/openforms/contrib/brk/validators.py b/src/openforms/contrib/brk/validators.py index 31c8383c30..369c5b4ad8 100644 --- a/src/openforms/contrib/brk/validators.py +++ b/src/openforms/contrib/brk/validators.py @@ -1,6 +1,6 @@ import logging from contextlib import contextmanager -from typing import Iterator, TypedDict +from typing import Iterator from django.core.exceptions import ValidationError from django.utils.deconstruct import deconstructible @@ -9,13 +9,13 @@ from glom import glom from requests import RequestException from rest_framework import serializers -from typing_extensions import NotRequired from openforms.authentication.constants import AuthAttribute from openforms.submissions.models import Submission from openforms.validations.registry import register from .client import NoServiceConfigured, SearchParams, get_client +from .constants import AddressValue logger = logging.getLogger(__name__) @@ -31,13 +31,6 @@ def suppress_api_errors(error_message: str) -> Iterator[None]: raise ValidationError(error_message) from e -class AddressValue(TypedDict): - postcode: str - house_number: str - house_letter: NotRequired[str] - house_number_addition: NotRequired[str] - - class AddressValueSerializer(serializers.Serializer): postcode = serializers.RegexField( "^[1-9][0-9]{3} ?(?!sa|sd|ss|SA|SD|SS)[a-zA-Z]{2}$", @@ -113,7 +106,7 @@ def __call__(self, value: AddressValue, submission: Submission) -> bool: address_query["huisnummertoevoeging"] = value["house_number_addition"] with (client, suppress_api_errors(self.error_messages["retrieving_error"])): - real_estate_objects_resp = client.get_cadastrals_by_address(address_query) + real_estate_objects_resp = client.get_real_estate_by_address(address_query) real_estate_objects = glom( real_estate_objects_resp, "_embedded.kadastraalOnroerendeZaken", diff --git a/src/openforms/formio/formatters/custom.py b/src/openforms/formio/formatters/custom.py index b94018e1cb..a6b85ee28b 100644 --- a/src/openforms/formio/formatters/custom.py +++ b/src/openforms/formio/formatters/custom.py @@ -3,6 +3,8 @@ from django.utils.dateparse import parse_date, parse_datetime from django.utils.html import format_html +from openforms.contrib.brk.constants import AddressValue + from ..typing import Component from .base import FormatterBase @@ -28,7 +30,7 @@ class AddressNLFormatter(FormatterBase): empty_values = ({},) - def format(self, component: Component, value: dict[str, str]) -> str: + def format(self, component: Component, value: AddressValue) -> str: value = value.copy() value.setdefault("houseLetter", "") value.setdefault("houseNumberAddition", "") diff --git a/src/openforms/validations/tests/test_api.py b/src/openforms/validations/tests/test_api.py index be33500be9..64f1ecd4b9 100644 --- a/src/openforms/validations/tests/test_api.py +++ b/src/openforms/validations/tests/test_api.py @@ -7,8 +7,8 @@ from openforms.accounts.tests.factories import StaffUserFactory from openforms.config.models import GlobalConfiguration -from openforms.submissions.constants import SUBMISSIONS_SESSION_KEY from openforms.submissions.tests.factories import SubmissionFactory +from openforms.submissions.tests.mixins import SubmissionsMixin from openforms.validations.registry import Registry, StringValueSerializer from openforms.validations.tests.test_registry import ( DjangoValidator, @@ -17,7 +17,7 @@ ) -class ValidationsAPITests(APITestCase): +class ValidationsAPITests(SubmissionsMixin, APITestCase): def setUp(self): self.user = StaffUserFactory() self.client.force_login(self.user) @@ -168,9 +168,7 @@ def test_validation(self): submission = SubmissionFactory.create() submission_uuid = str(submission.uuid) url = reverse("api:validate-value", kwargs={"validator": "django"}) - session = self.client.session - session[SUBMISSIONS_SESSION_KEY] = [submission_uuid] - session.save() + self._add_submission_to_session(submission) response = self.client.post( url, {"value": "VALID", "submission_uuid": submission_uuid}, format="json" diff --git a/src/openforms/validations/tests/test_registry.py b/src/openforms/validations/tests/test_registry.py index f09df25287..1ddc7bef5d 100644 --- a/src/openforms/validations/tests/test_registry.py +++ b/src/openforms/validations/tests/test_registry.py @@ -121,7 +121,7 @@ def test_validate_plugin_not_enabled(self): registry = Registry() registry("disabled", "Disabled")(DisabledValidator()) - res = registry.validate("disabled", "VALID", "dummy_submission") + res = registry.validate("disabled", "VALID", Submission()) self.assertEqual(res.is_valid, False) self.assertEqual( res.messages,