|
2 | 2 | from typing import Literal
|
3 | 3 | from unittest.mock import patch
|
4 | 4 |
|
| 5 | +from django.conf import settings |
5 | 6 | from django.contrib.auth import get_user_model
|
6 | 7 | from django.core.exceptions import ValidationError
|
7 | 8 | from django.test import TestCase, modify_settings, override_settings
|
@@ -1828,3 +1829,69 @@ def test_redirect_after_login_no_registration_and_no_branch_selection(
|
1828 | 1829 | profile_response = self.app.get(profile_response.url)
|
1829 | 1830 |
|
1830 | 1831 | self.assertEqual(profile_response.status_code, 200)
|
| 1832 | + |
| 1833 | + @patch("open_inwoner.kvk.client.KvKClient.get_all_company_branches") |
| 1834 | + @patch("open_inwoner.utils.context_processors.SiteConfiguration") |
| 1835 | + @patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_userinfo") |
| 1836 | + @patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.store_tokens") |
| 1837 | + @patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.verify_token") |
| 1838 | + @patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_token") |
| 1839 | + @patch( |
| 1840 | + "open_inwoner.accounts.models.OpenIDEHerkenningConfig.get_solo", |
| 1841 | + return_value=OpenIDEHerkenningConfig( |
| 1842 | + id=1, |
| 1843 | + enabled=True, |
| 1844 | + legal_subject_claim=["kvk"], |
| 1845 | + oidc_op_authorization_endpoint="http://idp.local/auth", |
| 1846 | + ), |
| 1847 | + ) |
| 1848 | + def test_redirect_after_login_branch_already_selected( |
| 1849 | + self, |
| 1850 | + mock_get_solo, |
| 1851 | + mock_get_token, |
| 1852 | + mock_verify_token, |
| 1853 | + mock_store_tokens, |
| 1854 | + mock_get_userinfo, |
| 1855 | + mock_siteconfig, |
| 1856 | + mock_kvk, |
| 1857 | + ): |
| 1858 | + """ |
| 1859 | + KVK branch selection should be skipped if KVK_BRANCH_SESSION_VARIABLE is present in session |
| 1860 | + """ |
| 1861 | + user = eHerkenningUserFactory.create(kvk="12345678", rsin="123456789") |
| 1862 | + mock_get_userinfo.return_value = { |
| 1863 | + "sub": "some_username", |
| 1864 | + "kvk": "12345678", |
| 1865 | + } |
| 1866 | + mock_siteconfig.return_value = SiteConfiguration(id=1, eherkenning_enabled=True) |
| 1867 | + mock_kvk.return_value = [ |
| 1868 | + {"kvkNummer": "12345678"}, |
| 1869 | + {"kvkNummer": "87654321"}, |
| 1870 | + ] |
| 1871 | + |
| 1872 | + # initialize session with request |
| 1873 | + self.app.get("/") |
| 1874 | + session = self.app.session |
| 1875 | + session[KVK_BRANCH_SESSION_VARIABLE] = "1234" |
| 1876 | + session.save() |
| 1877 | + self.app.set_cookie(settings.SESSION_COOKIE_NAME, session.session_key) |
| 1878 | + |
| 1879 | + self.assertEqual(User.objects.count(), 1) |
| 1880 | + |
| 1881 | + redirect_url = reverse("profile:detail") |
| 1882 | + |
| 1883 | + callback_response = perform_oidc_login( |
| 1884 | + self.app, "eherkenning", redirect_url=redirect_url |
| 1885 | + ) |
| 1886 | + |
| 1887 | + user = User.objects.get() |
| 1888 | + |
| 1889 | + self.assertEqual(user.pk, int(self.app.session.get("_auth_user_id"))) |
| 1890 | + self.assertEqual(user.kvk, "12345678") |
| 1891 | + |
| 1892 | + self.assertRedirects( |
| 1893 | + callback_response, reverse("profile:detail"), fetch_redirect_response=False |
| 1894 | + ) |
| 1895 | + |
| 1896 | + response = self.app.get(callback_response.url) |
| 1897 | + self.assertEqual(response.status_code, 200) |
0 commit comments