Skip to content

Commit

Permalink
✅ [#1957] Add/fix tests for klant resources for companies
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Jan 5, 2024
1 parent 353c621 commit 59b3e1d
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 24 deletions.
59 changes: 55 additions & 4 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from open_inwoner.accounts.choices import StatusChoices
from open_inwoner.cms.profile.cms_appconfig import ProfileConfig
from open_inwoner.haalcentraal.tests.mixins import HaalCentraalMixin
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.pdc.tests.factories import CategoryFactory
from open_inwoner.plans.tests.factories import PlanFactory
from open_inwoner.utils.logentry import LOG_ACTIONS
Expand Down Expand Up @@ -281,7 +282,9 @@ def test_messages_enabled_disabled(self):
self.assertEqual(link_text(), _("Stuur een bericht"))


@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
@override_settings(
ROOT_URLCONF="open_inwoner.cms.tests.urls", MIDDLEWARE=PATCHED_MIDDLEWARE
)
class EditProfileTests(AssertTimelineLogMixin, WebTest):
def setUp(self):
self.url = reverse("profile:edit")
Expand Down Expand Up @@ -530,11 +533,59 @@ def test_modify_phone_and_email_updates_klant_api(self, m):
"telefoonnummer": "0612345678",
},
)
self.assertTimelineLog("retrieved klant for BSN-user")
self.assertTimelineLog("retrieved klant for user")
self.assertTimelineLog(
"patched klant from user profile edit with fields: emailadres, telefoonnummer"
)

@requests_mock.Mocker()
def test_eherkenning_user_updates_klant_api(self, m):
MockAPIReadPatchData.setUpServices()

for use_rsin_for_innNnpId_query_parameter in [True, False]:
with self.subTest(
use_rsin_for_innNnpId_query_parameter=use_rsin_for_innNnpId_query_parameter
):
# NOTE Explicitly creating a new Mocker object here, because for some reason
# `m` is overridden somewhere, which causes issues when `MockAPIReadPatchData.install_mocks`
# is run for the second time
with requests_mock.Mocker() as m:
data = MockAPIReadPatchData().install_mocks_eherkenning(
m, use_rsin=use_rsin_for_innNnpId_query_parameter
)

config = OpenKlantConfig.get_solo()
config.use_rsin_for_innNnpId_query_parameter = (
use_rsin_for_innNnpId_query_parameter
)
config.save()

response = self.app.get(self.url, user=data.eherkenning_user)

# reset noise from signals
m.reset_mock()
self.clearTimelineLogs()

form = response.forms["profile-edit"]
form["email"] = "[email protected]"
form["phonenumber"] = "0612345678"
form.submit()

# user data tested in other cases
self.assertTrue(data.matchers[0].called)
klant_patch_data = data.matchers[1].request_history[0].json()
self.assertEqual(
klant_patch_data,
{
"emailadres": "[email protected]",
"telefoonnummer": "0612345678",
},
)
self.assertTimelineLog("retrieved klant for user")
self.assertTimelineLog(
"patched klant from user profile edit with fields: emailadres, telefoonnummer"
)

@requests_mock.Mocker()
def test_modify_phone_updates_klant_api_but_skips_unchanged(self, m):
MockAPIReadPatchData.setUpServices()
Expand Down Expand Up @@ -579,7 +630,7 @@ def test_modify_phone_updates_klant_api_but_skip_unchanged_email(self, m):
"telefoonnummer": "0612345678",
},
)
self.assertTimelineLog("retrieved klant for BSN-user")
self.assertTimelineLog("retrieved klant for user")
self.assertTimelineLog(
"patched klant from user profile edit with fields: telefoonnummer"
)
Expand Down Expand Up @@ -609,7 +660,7 @@ def test_modify_phone_updates_klant_api_but_skip_unchanged_phone(self, m):
"emailadres": "[email protected]",
},
)
self.assertTimelineLog("retrieved klant for BSN-user")
self.assertTimelineLog("retrieved klant for user")
self.assertTimelineLog(
"patched klant from user profile edit with fields: emailadres"
)
Expand Down
125 changes: 113 additions & 12 deletions src/open_inwoner/cms/cases/tests/test_contactform.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.core import mail
from django.test import override_settings
from django.urls import reverse
Expand All @@ -13,7 +14,10 @@
from zgw_consumers.constants import APITypes
from zgw_consumers.test import generate_oas_component, mock_service_oas_get

from open_inwoner.accounts.tests.factories import DigidUserFactory
from open_inwoner.accounts.tests.factories import (
DigidUserFactory,
eHerkenningUserFactory,
)
from open_inwoner.openklant.constants import Status
from open_inwoner.openklant.models import OpenKlantConfig
from open_inwoner.openklant.tests.data import (
Expand All @@ -31,9 +35,17 @@
from open_inwoner.utils.test import ClearCachesMixin, paginated_response
from open_inwoner.utils.tests.helpers import AssertMockMatchersMixin

PATCHED_MIDDLEWARE = [
m
for m in settings.MIDDLEWARE
if m != "open_inwoner.kvk.middleware.KvKLoginMiddleware"
]


@requests_mock.Mocker()
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
@override_settings(
ROOT_URLCONF="open_inwoner.cms.tests.urls", MIDDLEWARE=PATCHED_MIDDLEWARE
)
class CasesContactFormTestCase(AssertMockMatchersMixin, ClearCachesMixin, WebTest):
def setUp(self):
super().setUp()
Expand Down Expand Up @@ -102,6 +114,32 @@ def setUp(self):
"geslachtsnaam": "Bazz",
},
)
self.eherkenning_user_role = generate_oas_component(
"zrc",
"schemas/Rol",
url=f"{ZAKEN_ROOT}rollen/3ff7686f-db35-4181-8e48-57521220f887",
omschrijvingGeneriek=RolOmschrijving.initiator,
betrokkeneType=RolTypes.niet_natuurlijk_persoon,
betrokkeneIdentificatie={
"innNnpId": "000000000",
"voornamen": "Foo Bar",
"voorvoegselGeslachtsnaam": "van der",
"geslachtsnaam": "Bazz",
},
)
self.eherkenning_user_role_kvk = generate_oas_component(
"zrc",
"schemas/Rol",
url=f"{ZAKEN_ROOT}rollen/5885531e-9b7f-46af-947e-f2278a2e72a8",
omschrijvingGeneriek=RolOmschrijving.initiator,
betrokkeneType=RolTypes.niet_natuurlijk_persoon,
betrokkeneIdentificatie={
"innNnpId": "12345678",
"voornamen": "Foo Bar",
"voorvoegselGeslachtsnaam": "van der",
"geslachtsnaam": "Bazz",
},
)
self.zaaktype = generate_oas_component(
"ztc",
"schemas/ZaakType",
Expand Down Expand Up @@ -221,18 +259,10 @@ def _setUpMocks(self, m):
self.zaaktype,
self.status_finish,
self.status_type_finish,
]:
self.matchers.append(m.get(resource["url"], json=resource))

for resource in [
self.zaak,
self.result,
self.zaaktype,
self.status_type_new,
self.status_type_in_behandeling,
self.status_type_finish,
]:
m.get(resource["url"], json=resource)
self.matchers.append(m.get(resource["url"], json=resource))

# mock `fetch_status_types_no_cache`
m.get(
Expand All @@ -243,7 +273,13 @@ def _setUpMocks(self, m):
self.matchers += [
m.get(
f"{ZAKEN_ROOT}rollen?zaak={self.zaak['url']}",
json=paginated_response([self.user_role]),
json=paginated_response(
[
self.user_role,
self.eherkenning_user_role,
self.eherkenning_user_role_kvk,
]
),
),
m.get(
f"{ZAKEN_ROOT}zaakinformatieobjecten?zaak={self.zaak['url']}",
Expand Down Expand Up @@ -407,6 +443,71 @@ def test_form_success_with_api(self, m):
},
)

def test_form_success_with_api_eherkenning_user(self, m):
self._setUpMocks(m)
self._setUpExtraMocks(m)

for use_rsin_for_innNnpId_query_parameter in [True, False]:
with self.subTest(
use_rsin_for_innNnpId_query_parameter=use_rsin_for_innNnpId_query_parameter
):
eherkenning_user = eHerkenningUserFactory(
kvk="12345678", rsin="000000000"
)

config = OpenKlantConfig.get_solo()
config.use_rsin_for_innNnpId_query_parameter = (
use_rsin_for_innNnpId_query_parameter
)
config.save()

identifier = (
eherkenning_user.rsin
if use_rsin_for_innNnpId_query_parameter
else eherkenning_user.kvk
)
m.get(
f"{KLANTEN_ROOT}klanten?subjectNietNatuurlijkPersoon__innNnpId={identifier}",
json=paginated_response([self.klant]),
),

response = self.app.get(self.case_detail_url, user=eherkenning_user)

form = response.forms["contact-form"]
form.action = reverse(
"cases:case_detail_contact_form",
kwargs={"object_id": self.zaak["uuid"]},
)
form["question"] = "Sample text"
response = form.submit()

self.assertEqual(
response.headers["HX-Redirect"],
reverse(
"cases:case_detail",
kwargs={"object_id": str(self.zaak["uuid"])},
),
)

redirect = self.app.get(response.headers["HX-Redirect"])
redirect_messages = list(redirect.context["messages"])

self.assertEqual(redirect_messages[0].message, _("Vraag verstuurd!"))
self.assertMockMatchersCalled(self.extra_matchers)

payload = self.matcher_create_contactmoment.request_history[0].json()
self.assertEqual(
payload,
{
"bronorganisatie": "123456788",
"kanaal": "Internet",
"medewerkerIdentificatie": {"identificatie": "FooVonBar"},
"onderwerp": "afdeling-x",
"tekst": "Sample text",
"type": "Melding",
},
)

def test_form_success_with_email(self, m):
self._setUpMocks(m)
self._setUpExtraMocks(m)
Expand Down
8 changes: 3 additions & 5 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,12 +748,10 @@ def register_by_api(self, form, config: OpenKlantConfig):

klant = fetch_klant(**get_fetch_parameters(self.request.user))
if klant:
self.log_system_action(
"retrieved klant for BSN-user", user=self.request.user
)
self.log_system_action("retrieved klant for user", user=self.request.user)
else:
self.log_system_action(
"could not retrieve klant for BSN-user", user=self.request.user
"could not retrieve klant for user", user=self.request.user
)
data = {
"bronorganisatie": config.register_bronorganisatie_rsin,
Expand All @@ -772,7 +770,7 @@ def register_by_api(self, form, config: OpenKlantConfig):
)
else:
self.log_system_action(
"could not create klant for BSN-user", user=self.request.user
"could not create klant for user", user=self.request.user
)

# create contact moment
Expand Down
27 changes: 27 additions & 0 deletions src/open_inwoner/openklant/tests/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def __init__(self):
email="[email protected]",
phonenumber="0100000000",
)
self.eherkenning_user = eHerkenningUserFactory(
email="[email protected]",
kvk="12345678",
rsin="000000000",
)

self.klant_old = generate_oas_component(
"kc",
Expand Down Expand Up @@ -71,6 +76,28 @@ def install_mocks(self, m) -> "MockAPIReadPatchData":
]
return self

def install_mocks_eherkenning(self, m, use_rsin=True) -> "MockAPIReadPatchData":
self.setUpOASMocks(m)
if use_rsin:
first_eherkenning_matcher = m.get(
f"{KLANTEN_ROOT}klanten?subjectNietNatuurlijkPersoon__innNnpId={self.eherkenning_user.rsin}",
json=paginated_response([self.klant_old]),
)
else:
first_eherkenning_matcher = m.get(
f"{KLANTEN_ROOT}klanten?subjectNietNatuurlijkPersoon__innNnpId={self.eherkenning_user.kvk}",
json=paginated_response([self.klant_old]),
)
self.matchers = [
first_eherkenning_matcher,
m.patch(
f"{KLANTEN_ROOT}klant/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
json=self.klant_updated,
status_code=200,
),
]
return self


class MockAPIReadData(MockAPIData):
def __init__(self):
Expand Down
Loading

0 comments on commit 59b3e1d

Please sign in to comment.