Skip to content

Commit

Permalink
👌 [#2101] PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Feb 23, 2024
1 parent f5cee21 commit b388419
Showing 1 changed file with 27 additions and 45 deletions.
72 changes: 27 additions & 45 deletions src/open_inwoner/accounts/tests/test_oidc_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class OIDCFlowTests(TestCase):
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(id=1, openid_display=OpenIDDisplayChoices.admin),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.admin),
)
def test_existing_email_updates_admin_user(
self,
Expand Down Expand Up @@ -73,16 +73,13 @@ def test_existing_email_updates_admin_user(
callback_response, reverse("admin:index"), fetch_redirect_response=True
)

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

user.refresh_from_db()

self.assertTrue(User.objects.filter(oidc_id="some_username").exists())
self.assertEqual(user.oidc_id, "some_username")

db_user = User.objects.filter(oidc_id="some_username").first()

self.assertEqual(db_user.id, user.id)
self.assertEqual(db_user.login_type, LoginTypeChoices.oidc)
self.assertEqual(db_user.is_staff, True)
self.assertEqual(user.login_type, LoginTypeChoices.oidc)
self.assertEqual(user.is_staff, True)

@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_userinfo")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.store_tokens")
Expand All @@ -94,9 +91,7 @@ def test_existing_email_updates_admin_user(
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(
id=1, openid_display=OpenIDDisplayChoices.regular
),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.regular),
)
def test_existing_email_updates_regular_user(
self,
Expand Down Expand Up @@ -129,30 +124,30 @@ def test_existing_email_updates_regular_user(
callback_response, reverse("pages-root"), fetch_redirect_response=True
)

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

user.refresh_from_db()

self.assertTrue(User.objects.filter(oidc_id="some_username").exists())
self.assertEqual(user.oidc_id, "some_username")

db_user = User.objects.filter(oidc_id="some_username").first()

self.assertEqual(db_user.id, user.id)
self.assertEqual(db_user.login_type, LoginTypeChoices.oidc)
self.assertEqual(db_user.is_staff, False)
self.assertEqual(user.login_type, LoginTypeChoices.oidc)
self.assertEqual(user.is_staff, False)

@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(
"mozilla_django_oidc_db.mixins.OpenIDConnectConfig.get_solo",
return_value=OpenIDConnectConfig(id=1, enabled=True, make_users_staff=False),
return_value=OpenIDConnectConfig(
id=1,
enabled=True,
make_users_staff=False,
claim_mapping={"first_name": "first_name"},
),
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(
id=1, openid_display=OpenIDDisplayChoices.regular
),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.regular),
)
def test_existing_oidc_id_updates_regular_user(
self,
Expand Down Expand Up @@ -187,16 +182,13 @@ def test_existing_oidc_id_updates_regular_user(
callback_response, reverse("pages-root"), fetch_redirect_response=True
)

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

user.refresh_from_db()

self.assertTrue(User.objects.filter(oidc_id="some_username").exists())
self.assertEqual(user.oidc_id, "some_username")

db_user = User.objects.filter(oidc_id="some_username").first()

self.assertEqual(db_user.id, user.id)
self.assertEqual(db_user.login_type, LoginTypeChoices.oidc)
self.assertEqual(db_user.is_staff, False)
self.assertEqual(user.first_name, "bar")
self.assertEqual(user.is_staff, False)

@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.get_userinfo")
@patch("mozilla_django_oidc_db.backends.OIDCAuthenticationBackend.store_tokens")
Expand All @@ -208,9 +200,7 @@ def test_existing_oidc_id_updates_regular_user(
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(
id=1, openid_display=OpenIDDisplayChoices.regular
),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.regular),
)
def test_existing_case_sensitive_email_updates_user(
self,
Expand Down Expand Up @@ -265,7 +255,7 @@ def test_existing_case_sensitive_email_updates_user(
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(id=1, openid_display=OpenIDDisplayChoices.admin),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.admin),
)
def test_new_admin_user_is_created_when_new_email(
self,
Expand Down Expand Up @@ -298,9 +288,8 @@ def test_new_admin_user_is_created_when_new_email(
callback_response, reverse("admin:index"), fetch_redirect_response=True
)

new_user = User.objects.filter(email="[email protected]").first()
new_user = User.objects.get(email="[email protected]")

self.assertIsNotNone(new_user)
self.assertEqual(new_user.oidc_id, "some_username")
self.assertEqual(new_user.login_type, LoginTypeChoices.oidc)
self.assertEqual(new_user.is_staff, True)
Expand All @@ -315,9 +304,7 @@ def test_new_admin_user_is_created_when_new_email(
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(
id=1, openid_display=OpenIDDisplayChoices.regular
),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.regular),
)
def test_new_regular_user_is_created_when_new_email(
self,
Expand All @@ -339,8 +326,6 @@ def test_new_regular_user_is_created_when_new_email(
session.save()
callback_url = reverse("oidc_authentication_callback")

self.assertFalse(User.objects.filter(email="[email protected]").exists())

# enter the login flow
callback_response = self.client.get(
callback_url, {"code": "mock", "state": "mock"}
Expand All @@ -350,9 +335,8 @@ def test_new_regular_user_is_created_when_new_email(
callback_response, reverse("pages-root"), fetch_redirect_response=True
)

new_user = User.objects.filter(email="[email protected]").first()
new_user = User.objects.get(email="[email protected]")

self.assertIsNotNone(new_user)
self.assertEqual(new_user.oidc_id, "some_username")
self.assertEqual(new_user.login_type, LoginTypeChoices.oidc)
self.assertEqual(new_user.is_staff, False)
Expand All @@ -374,9 +358,7 @@ def test_error_page_direct_access_forbidden(self):
)
@patch(
"open_inwoner.configurations.models.SiteConfiguration.get_solo",
return_value=SiteConfiguration(
id=1, openid_display=OpenIDDisplayChoices.regular
),
return_value=SiteConfiguration(openid_display=OpenIDDisplayChoices.regular),
)
def test_error_first_cleared_after_succesful_login(
self,
Expand Down

0 comments on commit b388419

Please sign in to comment.