Skip to content

Commit

Permalink
✅ [#4713] Test for BRP client pre request hooks
Browse files Browse the repository at this point in the history
to ensure that token_exchange works correctly
  • Loading branch information
stevenbal committed Oct 8, 2024
1 parent a6cc31e commit b259da3
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/openforms/contrib/haal_centraal/tests/test_brp_clients.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest.mock import patch
from unittest.mock import MagicMock, patch

from django.test import SimpleTestCase, TestCase
from django.test import SimpleTestCase, TestCase, tag

import requests_mock
from glom import glom
Expand All @@ -9,6 +9,8 @@
from openforms.authentication.service import AuthAttribute
from openforms.authentication.utils import store_registrator_details
from openforms.config.models import GlobalConfiguration
from openforms.pre_requests.base import PreRequestHookBase
from openforms.pre_requests.registry import Registry
from openforms.submissions.tests.factories import SubmissionFactory

from ..clients import get_brp_client
Expand Down Expand Up @@ -171,8 +173,45 @@ def test_default_client_context(self):

self.assertIsNone(client.pre_request_context) # type: ignore

@tag("gh-4713")
def test_pre_request_hooks_called(self):
"""
Regression test for #4713, assert that the pre request hooks are called with the
expected context to make sure that token exchange works properly
"""
pre_req_register = Registry()
mock = MagicMock()

@pre_req_register("test")
class PreRequestHook(PreRequestHookBase):
def __call__(self, *args, **kwargs):
mock(*args, **kwargs)

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",
)
client = get_brp_client(submission_bsn)

class HaalCentraalFindPersonV1Test(HaalCentraalFindPersonTests, SimpleTestCase):
self.requests_mock.get(
"https://personen/api/ingeschrevenpersonen/999990676",
status_code=200,
json=load_json_mock("ingeschrevenpersonen.v1.json"),
)
with patch("openforms.pre_requests.clients.registry", new=pre_req_register):
client.find_person("999990676", attributes=self.attributes_to_query)

self.assertEqual(mock.call_count, 1) # 1 API calls expected
context = mock.call_args.kwargs["context"]
self.assertEqual(context, {"submission": submission_bsn}) # type: ignore


class HaalCentraalFindPersonV1Test(HaalCentraalFindPersonTests, TestCase):
version = BRPVersions.v13

def test_find_person_succesfully(self):
Expand Down

0 comments on commit b259da3

Please sign in to comment.