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

[#2808] Use KVK API to check legal status of compay at login #1544

Merged
merged 1 commit into from
Jan 22, 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
20 changes: 17 additions & 3 deletions src/eherkenning/tests/test_mock_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,23 @@ def test_get_returns_valid_response(self):
self.assertContains(response, reverse("login"))
self.assertNoEHerkenningURLS(response)

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch(
"open_inwoner.kvk.signals.KvKClient.retrieve_rsin_with_kvk",
return_value="123456789",
autospec=True,
)
@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches", autospec=True)
def test_post_redirects_and_authenticates(
self, mock_kvk, mock_retrieve_rsin_with_kvk
self, mock_kvk, mock_retrieve_rsin_with_kvk, mock_get_basisprofiel
):
mock_kvk.return_value = [
{"kvkNummer": "29664887", "vestigingsnummer": "1234"},
{"kvkNummer": "29664887", "vestigingsnummer": "5678"},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

url = reverse("eherkenning-mock:password")
params = {
Expand Down Expand Up @@ -164,8 +168,9 @@ def test_post_redirects_and_authenticates(
# check company branch number in session
self.assertEqual(get_kvk_branch_number(self.client.session), "1234")

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches")
def test_redirect_flow_with_single_company(self, mock_kvk):
def test_redirect_flow_with_single_company(self, mock_kvk, mock_get_basisprofiel):
"""
Assert that if the KvK API returns only a single company:
1. the redirect flow passes automatically through `KvKLoginMiddleware`
Expand All @@ -174,6 +179,9 @@ def test_redirect_flow_with_single_company(self, mock_kvk):
mock_kvk.return_value = [
{"kvkNummer": "29664887", "vestigingsnummer": "1234"},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

url = reverse("eherkenning-mock:password")
params = {
Expand Down Expand Up @@ -201,8 +209,11 @@ def test_redirect_flow_with_single_company(self, mock_kvk):
# check company branch number in session
self.assertEqual(get_kvk_branch_number(self.client.session), None)

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches")
def test_redirect_flow_with_no_vestigingsnummer(self, mock_kvk):
def test_redirect_flow_with_no_vestigingsnummer(
self, mock_kvk, mock_get_basisprofiel
):
"""
Assert that if the KvK API returns only a single company without vestigingsnummer:
1. the redirect flow passes automatically through `KvKLoginMiddleware`
Expand All @@ -211,6 +222,9 @@ def test_redirect_flow_with_no_vestigingsnummer(self, mock_kvk):
mock_kvk.return_value = [
{"kvkNummer": "29664887"},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

url = reverse("eherkenning-mock:password")
params = {
Expand Down
35 changes: 26 additions & 9 deletions src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from open_inwoner.kvk.branches import get_kvk_branch_number
from open_inwoner.kvk.tests.factories import CertificateFactory
from open_inwoner.openklant.tests.data import MockAPIReadPatchData
from open_inwoner.openzaak.models import OpenZaakConfig
from open_inwoner.utils.tests.helpers import AssertTimelineLogMixin

from ...cms.collaborate.cms_apps import CollaborateApphook
Expand Down Expand Up @@ -666,19 +665,20 @@ def test_eherkenning_fail_without_invite_redirects_to_login_page(self, m):

self.assertRedirectsLogin(response, with_host=True)

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch(
"open_inwoner.kvk.signals.KvKClient.retrieve_rsin_with_kvk",
return_value="",
autospec=True,
)
@patch(
"open_inwoner.accounts.views.auth.OpenZaakConfig.get_solo",
return_value=OpenZaakConfig(fetch_eherkenning_zaken_with_rsin=True),
autospec=True,
)
def test_login_as_eenmanszaak_blocked(
self, mock_oz_config, mock_retrieve_rsin_with_kvk
self,
mock_retrieve_rsin_with_kvk,
mock_get_basisprofiel,
):
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Eenmanszaak"}}
}
url = reverse("eherkenning-mock:password")
params = {
"acs": f"http://testserver{reverse('eherkenning:acs')}",
Expand Down Expand Up @@ -761,6 +761,7 @@ def test_eherkenning_fail_with_invite_redirects_to_register_page(self, m):
f"http://testserver{reverse('django_registration_register')}?invite={invite.key}",
)

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch(
"open_inwoner.kvk.signals.KvKClient.retrieve_rsin_with_kvk",
return_value="123456789",
Expand All @@ -779,10 +780,14 @@ def test_invite_url_not_in_session_after_successful_login(
mock_solo,
mock_kvk,
mock_retrieve_rsin_with_kvk,
mock_get_basisprofiel,
):
mock_kvk.return_value = [
{"kvkNummer": "12345678", "vestigingsnummer": "1234"},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

mock_solo.return_value.api_key = "123"
mock_solo.return_value.api_root = "http://foo.bar/api/v1/"
Expand Down Expand Up @@ -826,11 +831,14 @@ def test_invite_url_not_in_session_after_successful_login(
# check company branch number in session
self.assertEqual(get_kvk_branch_number(self.client.session), None)

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches")
@patch(
"open_inwoner.kvk.models.KvKConfig.get_solo",
)
def test_redirect_flow_with_no_vestigingsnummer(self, mock_solo, mock_kvk):
def test_redirect_flow_with_no_vestigingsnummer(
self, mock_solo, mock_kvk, mock_get_basisprofiel
):
"""
Assert that if the KvK API returns only a single company without vestigingsnummer:
1. the redirect flow passes automatically through `KvKLoginMiddleware`
Expand All @@ -839,6 +847,9 @@ def test_redirect_flow_with_no_vestigingsnummer(self, mock_solo, mock_kvk):
mock_kvk.return_value = [
{"kvkNummer": "12345678"},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

mock_solo.return_value.api_key = "123"
mock_solo.return_value.api_root = "http://foo.bar/api/v1/"
Expand Down Expand Up @@ -1207,6 +1218,7 @@ def test_digid_user_success(self):
self.assertEqual(users.first().email, "test@example.com")
self.assertEqual(users.last().email, "test@example.com")

@patch("open_inwoner.kvk.signals.KvKClient.get_basisprofiel", autospec=True)
@patch(
"open_inwoner.kvk.signals.KvKClient.retrieve_rsin_with_kvk",
return_value="123456789",
Expand All @@ -1216,7 +1228,9 @@ def test_digid_user_success(self):
"open_inwoner.kvk.client.KvKClient.get_all_company_branches",
autospec=True,
)
def test_eherkenning_user_success(self, mock_kvk, mock_retrieve_rsin_with_kvk):
def test_eherkenning_user_success(
self, mock_kvk, mock_retrieve_rsin_with_kvk, mock_get_basisprofiel
):
"""Assert that eHerkenning users can register with duplicate emails"""

mock_kvk.return_value = [
Expand All @@ -1231,6 +1245,9 @@ def test_eherkenning_user_success(self, mock_kvk, mock_retrieve_rsin_with_kvk):
"naam": "Mijn bedrijf",
},
]
mock_get_basisprofiel.return_value = {
"_embedded": {"eigenaar": {"rechtsvorm": "Stichting"}}
}

test_user = eHerkenningUserFactory.create(
email="test@localhost",
Expand Down
Loading
Loading