Skip to content

Commit 5d199cc

Browse files
authored
Merge pull request #1655 from maykinmedia/demo-2025-03-04
Ensure case contact form selects klanten backend from API group
2 parents 0baddce + ccd05ce commit 5d199cc

File tree

3 files changed

+82
-9
lines changed

3 files changed

+82
-9
lines changed

src/open_inwoner/cms/cases/tests/test_contactform.py

+51-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import uuid
12
from unittest.mock import ANY, patch
23

34
from django.conf import settings
45
from django.contrib.auth import signals
56
from django.core import mail
6-
from django.test import override_settings
7+
from django.test import Client, TestCase, override_settings
78
from django.urls import reverse
89
from django.utils.translation import gettext as _
910

@@ -31,7 +32,11 @@
3132
from open_inwoner.openklant.tests.data import CONTACTMOMENTEN_ROOT, KLANTEN_ROOT
3233
from open_inwoner.openklant.tests.factories import OpenKlant2ConfigFactory
3334
from open_inwoner.openklant.tests.mocks import MockOpenKlant2Service
34-
from open_inwoner.openzaak.models import CatalogusConfig, OpenZaakConfig
35+
from open_inwoner.openzaak.models import (
36+
CatalogusConfig,
37+
OpenZaakConfig,
38+
ZGWApiGroupConfig,
39+
)
3540
from open_inwoner.openzaak.tests.factories import (
3641
ServiceFactory,
3742
ZaakTypeConfigFactory,
@@ -530,6 +535,14 @@ def test_form_success_with_api_esuite(
530535
self._setUpOpenKlantMocks(m)
531536

532537
response = self.app.get(self.case_detail_url, user=self.user)
538+
539+
# Set the primary to the opposite API, to ensure we pick the connected
540+
# backend from the ZGWApiGroup
541+
self.klant_config.primary_backend = KlantenServiceType.OPENKLANT2.value
542+
self.klant_config.save()
543+
self.api_group.klant_backend = KlantenServiceType.ESUITE.value
544+
self.api_group.save()
545+
533546
form = response.forms["contact-form"]
534547
form.action = reverse(
535548
"cases:case_detail_contact_form",
@@ -595,8 +608,12 @@ def test_form_success_with_api_openklant(
595608
subject="oip_subject",
596609
)
597610

598-
self.klant_config.primary_backend = KlantenServiceType.OPENKLANT2.value
611+
# Set the primary to the opposite API, to ensure we pick the connected
612+
# backend from the ZGWApiGroup
613+
self.klant_config.primary_backend = KlantenServiceType.ESUITE.value
599614
self.klant_config.save()
615+
self.api_group.klant_backend = KlantenServiceType.OPENKLANT2.value
616+
self.api_group.save()
600617

601618
response = self.app.get(self.case_detail_url, user=self.user)
602619
form = response.forms["contact-form"]
@@ -886,3 +903,34 @@ def test_send_email_confirmation_is_configurable__send_disabled(
886903
form["question"] = "Sample text"
887904
response = form.submit()
888905
mock_send_confirm.assert_not_called()
906+
907+
908+
@override_settings(
909+
ROOT_URLCONF="open_inwoner.cms.tests.urls", MIDDLEWARE=PATCHED_MIDDLEWARE
910+
)
911+
class CasesContactFormInvalidParamsTestCase(TestCase):
912+
def setUp(self) -> None:
913+
super().setUp()
914+
self.user = DigidUserFactory(bsn="900222086", email="[email protected]")
915+
self.client = Client()
916+
self.client.force_login(self.user)
917+
918+
def test_non_existent_zgw_api_group_yields_404(self):
919+
fake_group_id = 8888
920+
self.assertFalse(ZGWApiGroupConfig.objects.filter(id=fake_group_id).exists())
921+
922+
for method in ("get", "post"):
923+
with self.subTest(method):
924+
action = getattr(self.client, method)
925+
response = action(
926+
reverse(
927+
"cases:case_detail_contact_form",
928+
kwargs={
929+
"object_id": str(uuid.uuid4()),
930+
"api_group_id": fake_group_id,
931+
},
932+
),
933+
user=self.user,
934+
)
935+
936+
self.assertEqual(response.status_code, 404)

src/open_inwoner/cms/cases/views/mixins.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22

33
from django.contrib.auth.mixins import AccessMixin, LoginRequiredMixin
4+
from django.http import Http404
45
from django.template.response import TemplateResponse
56
from django.utils.translation import gettext_lazy as _
67

@@ -62,7 +63,12 @@ def dispatch(self, request, *args, **kwargs):
6263
object_id := self.kwargs.get("object_id")
6364
)
6465
if is_retrieving_case:
65-
api_group = ZGWApiGroupConfig.objects.get(pk=api_group_id)
66+
try:
67+
api_group = ZGWApiGroupConfig.objects.get(pk=api_group_id)
68+
except ZGWApiGroupConfig.DoesNotExist as exc:
69+
logger.exception("Non-existent ZGWApiGroupConfig passed")
70+
raise Http404 from exc
71+
6672
client = api_group.zaken_client
6773
self.case = client.fetch_single_case(object_id)
6874
if self.case:

src/open_inwoner/cms/cases/views/status.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,12 @@ class CaseContactFormView(CaseAccessMixin, LogMixin, FormView):
914914
form_class = CaseContactForm
915915

916916
def post(self, request, *args, **kwargs):
917+
try:
918+
api_group = ZGWApiGroupConfig.objects.get(pk=self.kwargs["api_group_id"])
919+
except ZGWApiGroupConfig.DoesNotExist as exc:
920+
logger.exception("Non-existent ZGWApiGroupConfig passed")
921+
raise Http404 from exc
922+
917923
form = self.get_form()
918924

919925
if form.is_valid():
@@ -933,7 +939,7 @@ def post(self, request, *args, **kwargs):
933939
send_confirmation = email_success
934940

935941
if klant_config.register_contact_via_api:
936-
api_success = self.register_by_api(form, config=klant_config)
942+
api_success = self.register_by_api(form, api_group)
937943
if api_success:
938944
send_confirmation = klant_config.send_email_confirmation
939945
# else keep the send_confirmation if email set it
@@ -1000,10 +1006,23 @@ def register_by_email(self, form, recipient_email):
10001006
)
10011007
return False
10021008

1003-
def register_by_api(self, form, config: KlantenSysteemConfig):
1004-
if config.primary_backend == KlantenServiceType.ESUITE.value:
1005-
return self._register_via_esuite(form, config=ESuiteKlantConfig.get_solo())
1006-
return self._register_via_openklant(form, config=OpenKlant2Config.get_solo())
1009+
def register_by_api(self, form, api_group: ZGWApiGroupConfig):
1010+
if not api_group.klant_backend:
1011+
return
1012+
1013+
match api_group.klant_backend:
1014+
case KlantenServiceType.ESUITE.value:
1015+
return self._register_via_esuite(
1016+
form, config=ESuiteKlantConfig.get_solo()
1017+
)
1018+
case KlantenServiceType.OPENKLANT2.value:
1019+
return self._register_via_openklant(
1020+
form, config=OpenKlant2Config.get_solo()
1021+
)
1022+
case _:
1023+
logger.error(
1024+
"Got non-existent klanten backend %s", api_group.klant_backend
1025+
)
10071026

10081027
def _register_via_openklant(self, form, config: OpenKlant2Config) -> bool:
10091028
user = self.request.user

0 commit comments

Comments
 (0)