Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2996] Create questions with OpenKlant linked to a zaak #1593

Merged
merged 2 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 104 additions & 2 deletions src/open_inwoner/cms/cases/tests/test_contactform.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from unittest.mock import ANY, patch

from django.conf import settings
from django.contrib.auth import signals
from django.core import mail
from django.test import override_settings
from django.urls import reverse
from django.utils.translation import gettext as _

import factory
import requests_mock
from django_webtest import WebTest
from zgw_consumers.api_models.constants import (
Expand All @@ -20,9 +22,15 @@
eHerkenningUserFactory,
)
from open_inwoner.openklant.constants import KlantenServiceType, Status
from open_inwoner.openklant.models import ESuiteKlantConfig, KlantenSysteemConfig
from open_inwoner.openklant.models import (
ContactFormSubject,
ESuiteKlantConfig,
KlantenSysteemConfig,
)
from open_inwoner.openklant.services import eSuiteVragenService
from open_inwoner.openklant.tests.data import CONTACTMOMENTEN_ROOT, KLANTEN_ROOT
from open_inwoner.openklant.tests.factories import OpenKlant2ConfigFactory
from open_inwoner.openklant.tests.mocks import MockOpenKlant2Service
from open_inwoner.openzaak.models import CatalogusConfig, OpenZaakConfig
from open_inwoner.openzaak.tests.factories import (
ServiceFactory,
Expand All @@ -37,13 +45,16 @@
)
from open_inwoner.utils.test import ClearCachesMixin, paginated_response
from open_inwoner.utils.tests.helpers import AssertMockMatchersMixin
from openklant2.types.resources.klant_contact import KlantContactValidator

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

OPENKLANT2_ROOT = "http://localhost:8338/klantinteracties/api/v1/"


@requests_mock.Mocker()
@patch(
Expand Down Expand Up @@ -89,6 +100,8 @@ def setUp(self):
self.klant_config.send_email_confirmation = True
self.klant_config.save()

self.openklant_config = OpenKlant2ConfigFactory()

self.esuite_config = ESuiteKlantConfig.get_solo()
self.esuite_config.register_bronorganisatie_rsin = "123456788"
self.esuite_config.register_type = "Melding"
Expand Down Expand Up @@ -369,11 +382,43 @@ def _setUpExtraMocks(self, m):
json=self.contactmoment,
)

def _setUpOpenKlantMocks(self, m):
klantcontact = {
"uuid": "095be615-a8ad-4c33-8e9c-c7612fbf6c9f",
"url": "http://example.com",
"gingOverOnderwerpobjecten": [],
"hadBetrokkenActoren": [],
"omvatteBijlagen": [],
"hadBetrokkenen": [],
"leiddeTotInterneTaken": [],
"nummer": "string",
"kanaal": "string",
"onderwerp": "string",
"inhoud": "string",
"indicatieContactGelukt": True,
"taal": "dut",
"vertrouwelijk": False,
"plaatsgevondenOp": "2019-08-24T14:15:22Z",
"_expand": {},
}
KlantContactValidator.validate_python(klantcontact)
m.get(
f"{OPENKLANT2_ROOT}klantcontacten?onderwerpobject__onderwerpobjectidentificatorObjectId=ZAAK-2022-0000000024",
headers={"Content-Type": "application/json"},
json={
"count": 123,
"next": None,
"previous": None,
"results": [klantcontact],
},
)

def test_form_is_shown_if_open_klant_api_configured(
self, m, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

self.assertTrue(self.klant_config.has_api_configuration)

Expand All @@ -390,6 +435,7 @@ def test_form_is_shown_if_open_klant_email_configured(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

self.esuite_config.klanten_service = None
self.esuite_config.save()
Expand All @@ -413,6 +459,7 @@ def test_form_is_shown_if_open_klant_email_and_api_configured(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

self.klant_config.register_email = "example@example.com"
self.klant_config.save()
Expand All @@ -432,6 +479,7 @@ def test_no_form_shown_if_open_klant_not_configured(
self, m, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpOpenKlantMocks(m)

# reset
self.esuite_config.klanten_service = None
Expand All @@ -456,6 +504,7 @@ def test_no_form_shown_if_contact_form_disabled(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

CatalogusConfig.objects.all().delete()
self.zaak_type_config.delete()
Expand All @@ -473,9 +522,12 @@ def test_no_form_shown_if_contact_form_disabled(

mock_send_confirm.assert_not_called()

def test_form_success_with_api(self, m, mock_contactmoment, mock_send_confirm):
def test_form_success_with_api_esuite(
self, m, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

response = self.app.get(self.case_detail_url, user=self.user)
form = response.forms["contact-form"]
Expand Down Expand Up @@ -528,11 +580,56 @@ def test_form_success_with_api(self, m, mock_contactmoment, mock_send_confirm):

mock_send_confirm.assert_called_once_with("foo@example.com", ANY)

@factory.django.mute_signals(signals.user_logged_in)
@patch(
"open_inwoner.cms.cases.views.status.OpenKlant2Service",
return_value=MockOpenKlant2Service(),
)
def test_form_success_with_api_openklant(
self, m, mock_openklant_service, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)

self.contactformsubject = ContactFormSubject.objects.create(
subject="oip_subject",
)

self.klant_config.primary_backend = KlantenServiceType.OPENKLANT2.value
self.klant_config.save()

response = self.app.get(self.case_detail_url, user=self.user)
form = response.forms["contact-form"]
form.action = reverse(
"cases:case_detail_contact_form",
kwargs={"object_id": self.zaak["uuid"], "api_group_id": self.api_group.id},
)
form["question"] = "What?"
response = form.submit()

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

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

self.assertEqual(redirect_messages[0].message, _("Vraag verstuurd!"))
mock_send_confirm.assert_called_once_with("foo@example.com", ANY)

def test_form_success_missing_medewerker(
self, m, mock_contactmoment, mock_send_confirm
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

config = ESuiteKlantConfig.get_solo()
# empty id should be excluded from contactmoment_create_data
Expand Down Expand Up @@ -594,6 +691,7 @@ def test_form_success_with_api_eherkenning_user(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

for use_rsin_for_innNnpId_query_parameter in [True, False]:
with self.subTest(
Expand Down Expand Up @@ -669,6 +767,7 @@ def test_form_success_with_api_eherkenning_user(
def test_form_success_with_email(self, m, mock_contactmoment, mock_send_confirm):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

self.klant_config.register_contact_email = "example@example.com"
self.klant_config.register_contact_via_api = False
Expand Down Expand Up @@ -712,6 +811,7 @@ def test_form_success_with_both_email_and_api(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

self.klant_config.register_contact_email = "example@example.com"
self.klant_config.save()
Expand Down Expand Up @@ -750,6 +850,7 @@ def test_send_email_confirmation_is_configurable__send_enabled(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

config = KlantenSysteemConfig.get_solo()
config.send_email_confirmation = True
Expand All @@ -770,6 +871,7 @@ def test_send_email_confirmation_is_configurable__send_disabled(
):
self._setUpMocks(m)
self._setUpExtraMocks(m)
self._setUpOpenKlantMocks(m)

config = KlantenSysteemConfig.get_solo()
config.send_email_confirmation = False
Expand Down
2 changes: 1 addition & 1 deletion src/open_inwoner/cms/cases/views/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def resolve_case(self, case: Zaak, group: ZGWApiGroupConfig) -> Zaak:

case.zaaktype_config = zaaktype_config

if zaaktype_config:
if zaaktype_config and case.status:
statustype_config = ZaakTypeStatusTypeConfig.objects.get(
zaaktype_config=zaaktype_config,
statustype_url=case.status.statustype.url,
Expand Down
25 changes: 21 additions & 4 deletions src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,11 +999,28 @@ def register_by_email(self, form, recipient_email):
def register_by_api(self, form, config: KlantenSysteemConfig):
if config.primary_backend == KlantenServiceType.ESUITE.value:
return self._register_via_esuite(form, config=ESuiteKlantConfig.get_solo())
return self._register_via_openklant2(form, config=OpenKlant2Config.get_solo())
return self._register_via_openklant(form, config=OpenKlant2Config.get_solo())

# TODO
def _register_via_openklant2(self, form, config: OpenKlant2Config):
raise NotImplementedError
def _register_via_openklant(self, form, config: OpenKlant2Config) -> bool:
user = self.request.user
service = OpenKlant2Service(config=config)

partij, created = service.get_or_create_partij_for_user(
fetch_params=service.get_fetch_parameters(user=user),
user=user,
)

cleaned_data = form.cleaned_data
question = cleaned_data["question"]

question = service.create_question_for_zaak(
partij_uuid=partij["uuid"],
question=question,
subject=self.case.omschrijving,
zaak=self.case,
)

return bool(question)

def _register_via_esuite(self, form, config: ESuiteKlantConfig):
assert config.has_api_configuration
Expand Down
Loading
Loading