diff --git a/src/open_inwoner/accounts/signals.py b/src/open_inwoner/accounts/signals.py index 19579fb100..6970aaf471 100644 --- a/src/open_inwoner/accounts/signals.py +++ b/src/open_inwoner/accounts/signals.py @@ -8,6 +8,7 @@ from open_inwoner.accounts.models import User from open_inwoner.haalcentraal.models import HaalCentraalConfig from open_inwoner.haalcentraal.utils import update_brp_data_in_db +from open_inwoner.kvk.client import KvKClient from open_inwoner.openklant.services import OpenKlant2Service, eSuiteKlantenService from open_inwoner.utils.logentry import user_action @@ -26,7 +27,7 @@ @receiver(user_logged_in) -def update_user_from_klant_on_login(sender, user, request, *args, **kwargs): +def update_user_on_login(sender, user, request, *args, **kwargs): # This additional guard is mainly to facilitate easier testing, where not # all request factories return a full-fledged request object. if not hasattr(request, "user"): @@ -35,6 +36,9 @@ def update_user_from_klant_on_login(sender, user, request, *args, **kwargs): if user.login_type not in [LoginTypeChoices.digid, LoginTypeChoices.eherkenning]: return + if user.login_type is LoginTypeChoices.eherkenning: + _update_eherkenning_user_from_kvk_api(user=user) + # OpenKlant2 try: service = OpenKlant2Service() @@ -74,6 +78,16 @@ def _update_user_from_esuite( service.update_user_from_klant(klant, user) +def _update_eherkenning_user_from_kvk_api(user: User): + kvk_client = KvKClient() + + vestiging = kvk_client.get_company_headquarters(kvk=user.kvk) + + if company_name := vestiging.get("naam"): + user.company_name = company_name + user.save() + + @receiver(user_logged_in) def log_user_login(sender, user, request, *args, **kwargs): current_path = request.path diff --git a/src/open_inwoner/accounts/tests/test_auth.py b/src/open_inwoner/accounts/tests/test_auth.py index 5d4926b97c..2497e37c9a 100644 --- a/src/open_inwoner/accounts/tests/test_auth.py +++ b/src/open_inwoner/accounts/tests/test_auth.py @@ -14,7 +14,7 @@ from pyquery import PyQuery as PQ from open_inwoner.accounts.choices import NotificationChannelChoice -from open_inwoner.accounts.signals import update_user_from_klant_on_login +from open_inwoner.accounts.signals import update_user_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 @@ -1896,7 +1896,7 @@ def test_anonymous_user_is_redirected_to_login_page_if_password_change_is_access @requests_mock.Mocker() -class TestUpdateUserFromKlantUponLoginTests(TestCase): +class UpdateUserOnLoginTest(TestCase): @classmethod def setUpTestData(cls): MockAPIReadPatchData.setUpServices() @@ -1906,7 +1906,7 @@ def setUpTestData(cls): def test_update_hook_is_registered_on_login(self, m): connected_functions = [receiver[1]() for receiver in user_logged_in.receivers] - self.assertIn(update_user_from_klant_on_login, connected_functions) + self.assertIn(update_user_on_login, connected_functions) def test_update_user_from_klant_hook_only_called_for_digid_and_eherkenning(self, m): self.data = MockAPIReadPatchData().install_mocks(m) @@ -1921,16 +1921,24 @@ def test_update_user_from_klant_hook_only_called_for_digid_and_eherkenning(self, self.data.user.save() 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() + ) as update_user_mock: + with patch( + "open_inwoner.accounts.signals._update_eherkenning_user_from_kvk_api" + ) as mock_kvk_update: + update_user_on_login( + self.__class__, + request.user, + request, + ) + if login_type in [ + LoginTypeChoices.digid, + LoginTypeChoices.eherkenning, + ]: + update_user_mock.assert_called_once() + + if login_type is LoginTypeChoices.eherkenning: + mock_kvk_update.assert_called_once() + else: + mock_kvk_update.assert_not_called() + else: + update_user_mock.assert_not_called() diff --git a/src/open_inwoner/openklant/tests/test_views.py b/src/open_inwoner/openklant/tests/test_views.py index fac5534f64..26c34e3e04 100644 --- a/src/open_inwoner/openklant/tests/test_views.py +++ b/src/open_inwoner/openklant/tests/test_views.py @@ -14,7 +14,7 @@ from pyquery import PyQuery from zgw_consumers.api_models.base import factory -from open_inwoner.accounts.signals import update_user_from_klant_on_login +from open_inwoner.accounts.signals import update_user_on_login from open_inwoner.accounts.tests.factories import DigidUserFactory, UserFactory from open_inwoner.configurations.models import SiteConfiguration from open_inwoner.openklant.api_models import ContactMoment, Klant, KlantContactMoment @@ -60,7 +60,7 @@ def setUp(self): ) super().setUp() - signals.user_logged_in.disconnect(receiver=update_user_from_klant_on_login) + signals.user_logged_in.disconnect(receiver=update_user_on_login) MockAPIReadData.setUpServices() self.api_group = ZGWApiGroupConfig.objects.get()