Skip to content

Commit

Permalink
[#2931] Update company name from KVK API on login
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling committed Dec 19, 2024
1 parent 2429bd2 commit 39335d7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
16 changes: 15 additions & 1 deletion src/open_inwoner/accounts/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"):
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
40 changes: 24 additions & 16 deletions src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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()
4 changes: 2 additions & 2 deletions src/open_inwoner/openklant/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 39335d7

Please sign in to comment.