Skip to content

Commit

Permalink
[#2940] Add OpenKlant2 configuration model
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling committed Dec 18, 2024
1 parent 0d4c736 commit 7a483d1
Show file tree
Hide file tree
Showing 22 changed files with 386 additions and 116 deletions.
7 changes: 6 additions & 1 deletion src/eherkenning/tests/test_mock_views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from unittest.mock import patch
from urllib.parse import urlencode

from django.contrib.auth import get_user_model
from django.contrib.auth import get_user_model, signals
from django.test import TestCase, modify_settings, override_settings
from django.urls import reverse

from furl import furl

from open_inwoner.accounts.signals import update_user_from_klant_on_login
from open_inwoner.kvk.branches import get_kvk_branch_number

RETURN_URL = "/"
Expand Down Expand Up @@ -88,6 +89,10 @@ def test_get_returns_valid_response(self):
@override_settings(**OVERRIDE_SETTINGS)
@modify_settings(**MODIFY_SETTINGS)
class PasswordLoginViewTests(eHerkenningMockTestCase):
@classmethod
def setUpTestData(cls):
signals.user_logged_in.disconnect(update_user_from_klant_on_login)

def test_get_returns_http400_on_missing_params(self):
url = reverse("eherkenning-mock:password")
response = self.client.get(url)
Expand Down
9 changes: 3 additions & 6 deletions src/open_inwoner/accounts/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging

from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.core.exceptions import ImproperlyConfigured
from django.dispatch import receiver
from django.urls import reverse
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -39,17 +38,15 @@ def update_user_from_klant_on_login(sender, user, request, *args, **kwargs):
# OpenKlant2
try:
service = OpenKlant2Service()
except ImproperlyConfigured:
logger.error("OpenKlant2 configuration missing")
except Exception:
logger.error("OpenKlant2 service failed to build")
else:
_update_user_from_openklant2(user=user, service=service, request=request)

# eSuite
try:
service = eSuiteKlantenService()
except ImproperlyConfigured:
logger.error("eSuiteKlantenService missing configuration")
except RuntimeError:
except Exception:
logger.error("eSuiteKlantenService failed to build")
else:
_update_user_from_esuite(user=user, service=service, request=request)
Expand Down
136 changes: 124 additions & 12 deletions src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
from django_webtest import WebTest
from furl import furl
from pyquery import PyQuery as PQ
from zgw_consumers.constants import APITypes
from zgw_consumers.models import Service

from open_inwoner.accounts.choices import NotificationChannelChoice
from open_inwoner.accounts.signals import update_user_from_klant_on_login
from open_inwoner.configurations.models import SiteConfiguration
from open_inwoner.haalcentraal.tests.mixins import HaalCentraalMixin
from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.kvk.tests.factories import CertificateFactory
from open_inwoner.openklant.models import OpenKlant2Config
from open_inwoner.openklant.tests.data import MockAPIReadPatchData
from open_inwoner.openzaak.models import OpenZaakConfig
from open_inwoner.utils.tests.helpers import AssertTimelineLogMixin
Expand Down Expand Up @@ -55,6 +58,26 @@ class DigiDRegistrationTest(
def setUpTestData(cls):
cls.homepage = cms_tools.create_homepage()

user_logged_in.disconnect(update_user_from_klant_on_login)

zgw_service, _ = Service.objects.update_or_create(
api_root="http://localhost:8338/klantinteracties/api/v1",
defaults={
"label": "Klanten API service",
"slug": "klanten-api-service",
"api_type": APITypes.kc,
"secret": "",
},
)
cls.openklant2_config = OpenKlant2Config.objects.create(
zgw_service=zgw_service,
mijn_vragen_kanaal="oip_mijn_vragen",
mijn_vragen_organisatie_naam="Open Inwoner Platform",
mijn_vragen_actor="ca0783a1-1d74-4e07-b3e0-185b1d2fccd4",
interne_taak_gevraagde_handeling="Beantwoorden vraag Mijn Omgeving",
interne_taak_toelichting="Beantwoorden vraag",
)

@patch("open_inwoner.accounts.models.OpenIDDigiDConfig.get_solo")
def test_registration_page_only_digid(self, mock_solo):
for oidc_enabled in [True, False]:
Expand Down Expand Up @@ -519,6 +542,26 @@ class eHerkenningRegistrationTest(AssertRedirectsMixin, WebTest):
def setUpTestData(cls):
cms_tools.create_homepage()

user_logged_in.disconnect(update_user_from_klant_on_login)

zgw_service, _ = Service.objects.update_or_create(
api_root="http://localhost:8338/klantinteracties/api/v1",
defaults={
"label": "Klanten API service",
"slug": "klanten-api-service",
"api_type": APITypes.kc,
"secret": "",
},
)
cls.openklant2_config = OpenKlant2Config.objects.create(
zgw_service=zgw_service,
mijn_vragen_kanaal="oip_mijn_vragen",
mijn_vragen_organisatie_naam="Open Inwoner Platform",
mijn_vragen_actor="ca0783a1-1d74-4e07-b3e0-185b1d2fccd4",
interne_taak_gevraagde_handeling="Beantwoorden vraag Mijn Omgeving",
interne_taak_toelichting="Beantwoorden vraag",
)

@patch("open_inwoner.accounts.models.OpenIDEHerkenningConfig.get_solo")
@patch("open_inwoner.configurations.models.SiteConfiguration.get_solo")
def test_registration_page_eherkenning(self, mock_solo, mock_eherkenning_config):
Expand Down Expand Up @@ -1104,6 +1147,27 @@ def setUpTestData(cls):
cls.msg_dupes = _("This email is already taken.")
cls.msg_inactive = _("This account has been deactivated")

def setUp(self):
user_logged_in.disconnect(update_user_from_klant_on_login)

zgw_service, _ = Service.objects.update_or_create(
api_root="http://localhost:8338/klantinteracties/api/v1",
defaults={
"label": "Klanten API service",
"slug": "klanten-api-service",
"api_type": APITypes.kc,
"secret": "",
},
)
self.openklant2_config = OpenKlant2Config.objects.create(
zgw_service=zgw_service,
mijn_vragen_kanaal="oip_mijn_vragen",
mijn_vragen_organisatie_naam="Open Inwoner Platform",
mijn_vragen_actor="ca0783a1-1d74-4e07-b3e0-185b1d2fccd4",
interne_taak_gevraagde_handeling="Beantwoorden vraag Mijn Omgeving",
interne_taak_toelichting="Beantwoorden vraag",
)

def test_digid_user_success(self):
"""Assert that digid users can register with duplicate emails"""
test_user = DigidUserFactory.create(
Expand Down Expand Up @@ -1457,6 +1521,27 @@ def setUpTestData(cls):
cms_tools.create_homepage()
cms_tools.create_apphook_page(ProfileApphook)

def setUp(self):
user_logged_in.disconnect(update_user_from_klant_on_login)

zgw_service, _ = Service.objects.update_or_create(
api_root="http://localhost:8338/klantinteracties/api/v1",
defaults={
"label": "Klanten API service",
"slug": "klanten-api-service",
"api_type": APITypes.kc,
"secret": "",
},
)
self.openklant2_config = OpenKlant2Config.objects.create(
zgw_service=zgw_service,
mijn_vragen_kanaal="oip_mijn_vragen",
mijn_vragen_organisatie_naam="Open Inwoner Platform",
mijn_vragen_actor="ca0783a1-1d74-4e07-b3e0-185b1d2fccd4",
interne_taak_gevraagde_handeling="Beantwoorden vraag Mijn Omgeving",
interne_taak_toelichting="Beantwoorden vraag",
)

def test_page_show_config_text(self):
config = SiteConfiguration.get_solo()
config.registration_text = "Hello registration text http://foo.bar/"
Expand Down Expand Up @@ -1904,11 +1989,31 @@ def setUpTestData(cls):
config.enable_notification_channel_choice = True
config.save()

zgw_service, _ = Service.objects.update_or_create(
api_root="http://localhost:8338/klantinteracties/api/v1",
defaults={
"label": "Klanten API service",
"slug": "klanten-api-service",
"api_type": APITypes.kc,
"secret": "",
},
)
cls.openklant2_config = OpenKlant2Config.objects.create(
zgw_service=zgw_service,
mijn_vragen_kanaal="oip_mijn_vragen",
mijn_vragen_organisatie_naam="Open Inwoner Platform",
mijn_vragen_actor="ca0783a1-1d74-4e07-b3e0-185b1d2fccd4",
interne_taak_gevraagde_handeling="Beantwoorden vraag Mijn Omgeving",
interne_taak_toelichting="Beantwoorden vraag",
)

def test_update_hook_is_registered_on_login(self, m):
user_logged_in.connect(update_user_from_klant_on_login)
connected_functions = [receiver[1]() for receiver in user_logged_in.receivers]
self.assertIn(update_user_from_klant_on_login, connected_functions)

def test_update_user_from_klant_hook_only_called_for_digid_and_eherkenning(self, m):
user_logged_in.disconnect(update_user_from_klant_on_login)
self.data = MockAPIReadPatchData().install_mocks(m)
request = RequestFactory().get("/foo")
request.user = self.data.user
Expand All @@ -1922,15 +2027,22 @@ def test_update_user_from_klant_hook_only_called_for_digid_and_eherkenning(self,
with patch(
"open_inwoner.openklant.services.eSuiteKlantenService.update_user_from_klant"
) as update_user_from_klant_mock:
update_user_from_klant_on_login(
self.__class__,
request.user,
request,
)
if login_type in [
LoginTypeChoices.digid,
LoginTypeChoices.eherkenning,
]:
update_user_from_klant_mock.assert_called_once()
else:
update_user_from_klant_mock.assert_not_called()
with patch(
"open_inwoner.openklant.services.OpenKlant2Service.get_or_create_partij_for_user"
) as openklant2_mock:
openklant2_mock.return_value = None, False

update_user_from_klant_on_login(
self.__class__,
request.user,
request,
)
if login_type in [
LoginTypeChoices.digid,
LoginTypeChoices.eherkenning,
]:
update_user_from_klant_mock.assert_called_once()
openklant2_mock.assert_called_once()
else:
update_user_from_klant_mock.assert_not_called()
openklant2_mock.assert_not_called()
6 changes: 5 additions & 1 deletion src/open_inwoner/accounts/tests/test_oidc_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from urllib.parse import urlencode

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth import get_user_model, signals
from django.core.exceptions import ValidationError
from django.test import TestCase, modify_settings, override_settings
from django.urls import reverse
Expand All @@ -17,6 +17,7 @@
from mozilla_django_oidc_db.models import OpenIDConnectConfig
from pyquery import PyQuery as PQ

from open_inwoner.accounts.signals import update_user_from_klant_on_login
from open_inwoner.accounts.views.auth_oidc import (
GENERIC_DIGID_ERROR_MSG,
GENERIC_EHERKENNING_ERROR_MSG,
Expand Down Expand Up @@ -85,6 +86,7 @@ class OIDCFlowTests(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
signals.user_logged_in.disconnect(update_user_from_klant_on_login)
cms_tools.create_homepage()
cms_tools.create_apphook_page(ProfileApphook)

Expand Down Expand Up @@ -501,6 +503,7 @@ class DigiDOIDCFlowTests(WebTest):
@classmethod
def setUpClass(cls):
super().setUpClass()
signals.user_logged_in.disconnect(update_user_from_klant_on_login)
cms_tools.create_homepage()
cms_tools.create_apphook_page(ProfileApphook)

Expand Down Expand Up @@ -1058,6 +1061,7 @@ class eHerkenningOIDCFlowTests(WebTest):
@classmethod
def setUpClass(cls):
super().setUpClass()
signals.user_logged_in.disconnect(update_user_from_klant_on_login)
cms_tools.create_homepage()
cms_tools.create_apphook_page(ProfileApphook)

Expand Down
14 changes: 14 additions & 0 deletions src/open_inwoner/accounts/tests/test_profile_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
)
class ProfileViewTests(WebTest):
def setUp(self):
signals.user_logged_in.disconnect(update_user_from_klant_on_login)

self.url = reverse("profile:detail")
self.return_url = reverse("logout")
self.user = UserFactory(
Expand Down Expand Up @@ -750,6 +752,8 @@ class ProfileDeleteTest(WebTest):

@classmethod
def setUpTestData(cls):
signals.user_logged_in.disconnect(update_user_from_klant_on_login)

cls.url = reverse("profile:detail")

def test_delete_regular_user_success(self):
Expand Down Expand Up @@ -986,6 +990,10 @@ def test_preselected_values(self):
@override_settings(ROOT_URLCONF="open_inwoner.cms.tests.urls")
@patch("open_inwoner.cms.utils.page_display._is_published", return_value=True)
class EditNotificationsTests(AssertTimelineLogMixin, WebTest):
@classmethod
def setUpTestData(cls):
signals.user_logged_in.disconnect(update_user_from_klant_on_login)

def setUp(self):
self.config = SiteConfiguration.get_solo()
self.config.notifications_messages_enabled = True
Expand Down Expand Up @@ -1103,6 +1111,8 @@ class NotificationsDisplayTests(WebTest):

@classmethod
def setUpTestData(cls):
signals.user_logged_in.disconnect(update_user_from_klant_on_login)

cls.url = reverse("profile:notifications")
cls.user = UserFactory()

Expand Down Expand Up @@ -1188,6 +1198,8 @@ class NewsletterSubscriptionTests(ClearCachesMixin, WebTest):
def setUp(self):
super().setUp()

signals.user_logged_in.disconnect(update_user_from_klant_on_login)

self.profile_app = ProfileConfig.objects.create(
namespace=ProfileApphook.app_name, newsletters=True
)
Expand Down Expand Up @@ -1426,6 +1438,8 @@ class UserAppointmentsTests(ClearCachesMixin, WebTest):
def setUp(self):
super().setUp()

signals.user_logged_in.disconnect(update_user_from_klant_on_login)

self.data = QmaticMockData()
self.assertTrue(self.data.user.has_verified_email())

Expand Down
9 changes: 3 additions & 6 deletions src/open_inwoner/accounts/views/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging

from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver

Expand All @@ -23,12 +22,10 @@ def get_or_create_klant_for_new_user(
user = instance

# OpenKlant2
# TODO: replace with proper config and refactor branching
use_ok2 = getattr(settings, "OPENKLANT2_CONFIG", None)
if use_ok2 and (openklant2_config := OpenKlant2Config.from_django_settings()):
if openklant2_config := OpenKlant2Config.get_solo():
try:
service = OpenKlant2Service(config=openklant2_config)
except RuntimeError:
except Exception:
logger.error("OpenKlant2 service failed to build")
return

Expand All @@ -53,7 +50,7 @@ def get_or_create_klant_for_new_user(
# eSuite
try:
service = eSuiteKlantenService()
except RuntimeError:
except Exception:
logger.error("eSuiteKlantenService failed to build")
return

Expand Down
Loading

0 comments on commit 7a483d1

Please sign in to comment.