Skip to content

Commit

Permalink
[#2808] Use KVK API to checkout legal status of compay at login
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Schilling committed Jan 10, 2025
1 parent fbae2b8 commit f003155
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 29 deletions.
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
34 changes: 26 additions & 8 deletions src/open_inwoner/accounts/tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,19 +598,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 @@ -693,6 +694,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 @@ -711,10 +713,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 @@ -758,11 +764,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 @@ -771,6 +780,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 @@ -1139,6 +1151,7 @@ def test_digid_user_success(self):
self.assertEqual(users.first().email, "[email protected]")
self.assertEqual(users.last().email, "[email protected]")

@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 @@ -1148,7 +1161,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 @@ -1163,6 +1178,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

0 comments on commit f003155

Please sign in to comment.