Skip to content

Commit

Permalink
[#2932] Skip KVK branch selection if vestigingsnummer already in session
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling committed Dec 10, 2024
1 parent ad3ad1c commit 2403f68
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/open_inwoner/accounts/tests/test_oidc_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Literal
from unittest.mock import patch

from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError
from django.test import TestCase, modify_settings, override_settings
Expand Down Expand Up @@ -1828,3 +1829,69 @@ def test_redirect_after_login_no_registration_and_no_branch_selection(
profile_response = self.app.get(profile_response.url)

self.assertEqual(profile_response.status_code, 200)

@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches")
@patch("open_inwoner.utils.context_processors.SiteConfiguration")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_userinfo")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.store_tokens")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.verify_token")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_token")
@patch(
"open_inwoner.accounts.models.OpenIDEHerkenningConfig.get_solo",
return_value=OpenIDEHerkenningConfig(
id=1,
enabled=True,
legal_subject_claim=["kvk"],
oidc_op_authorization_endpoint="http://idp.local/auth",
),
)
def test_redirect_after_login_branch_already_selected(
self,
mock_get_solo,
mock_get_token,
mock_verify_token,
mock_store_tokens,
mock_get_userinfo,
mock_siteconfig,
mock_kvk,
):
"""
KVK branch selection should be skipped if KVK_BRANCH_SESSION_VARIABLE is present in session
"""
user = eHerkenningUserFactory.create(kvk="12345678", rsin="123456789")
mock_get_userinfo.return_value = {
"sub": "some_username",
"kvk": "12345678",
}
mock_siteconfig.return_value = SiteConfiguration(id=1, eherkenning_enabled=True)
mock_kvk.return_value = [
{"kvkNummer": "12345678"},
{"kvkNummer": "87654321"},
]

# initialize session with request
self.app.get("/")
session = self.app.session
session[KVK_BRANCH_SESSION_VARIABLE] = "1234"
session.save()
self.app.set_cookie(settings.SESSION_COOKIE_NAME, session.session_key)

self.assertEqual(User.objects.count(), 1)

redirect_url = reverse("profile:detail")

callback_response = perform_oidc_login(
self.app, "eherkenning", redirect_url=redirect_url
)

user = User.objects.get()

self.assertEqual(user.pk, int(self.app.session.get("_auth_user_id")))
self.assertEqual(user.kvk, "12345678")

self.assertRedirects(
callback_response, reverse("profile:detail"), fetch_redirect_response=False
)

response = self.app.get(callback_response.url)
self.assertEqual(response.status_code, 200)
19 changes: 19 additions & 0 deletions src/open_inwoner/kvk/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import patch

from django.conf import settings
from django.test import TestCase
from django.urls import reverse, reverse_lazy

Expand Down Expand Up @@ -183,6 +184,24 @@ def test_get_branches_page_one_branch_found_sets_branch_check_done(
# Following redirect should not result in endless redirect
self.assertEqual(response.status_code, 200)

def test_get_branches_page_skips_if_vestigingsnummer_present(self):
session = self.client.session
session[KVK_BRANCH_SESSION_VARIABLE] = "12345678"
session.save()

self.client.force_login(user=self.user)

response = self.client.get(self.url)

self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse("pages-root"))
self.assertEqual(kvk_branch_selected_done(self.client.session), True)

response = self.client.get(response.url)

# # Following redirect should not result in endless redirect
self.assertEqual(response.status_code, 200)

@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches")
@patch(
"open_inwoner.kvk.models.KvKConfig.get_solo",
Expand Down

0 comments on commit 2403f68

Please sign in to comment.