Skip to content

Commit

Permalink
[#3607] Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 4, 2024
1 parent 647a9f5 commit a13d020
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/openforms/contrib/brk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
48 changes: 48 additions & 0 deletions src/openforms/contrib/brk/tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -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 _
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions src/openforms/contrib/brk/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])):
Expand Down
3 changes: 3 additions & 0 deletions src/openforms/validations/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a13d020

Please sign in to comment.