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

🔧 [#2101] Add OIDC admin config to admin index fixture #1021

Merged
merged 6 commits into from
Feb 23, 2024

Conversation

stevenbal
Copy link
Contributor

@stevenbal stevenbal commented Feb 12, 2024

issue: https://taiga.maykinmedia.nl/project/open-inwoner/issue/2101

Fixes two issues:

  • If an OIDC user logs in for the first time, they are now actually authenticated (previously they had to log in twice)
  • If OIDC is enabled for admins, the is_staff attribute is set for those users (according to the OIDC config) and they are redirected to the admin interface

@stevenbal stevenbal marked this pull request as draft February 12, 2024 15:00
@stevenbal stevenbal force-pushed the fix/2101-oidc-admin-issues branch 4 times, most recently from d6433bc to 65afe3d Compare February 15, 2024 11:00
@stevenbal stevenbal marked this pull request as ready for review February 15, 2024 11:01
@stevenbal stevenbal force-pushed the fix/2101-oidc-admin-issues branch from 65afe3d to 85e3e92 Compare February 15, 2024 11:50
@codecov-commenter
Copy link

codecov-commenter commented Feb 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (0f25981) 94.84% compared to head (8a68f4f) 94.85%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1021      +/-   ##
===========================================
+ Coverage    94.84%   94.85%   +0.01%     
===========================================
  Files          880      880              
  Lines        30725    30805      +80     
===========================================
+ Hits         29141    29221      +80     
  Misses        1584     1584              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stevenbal stevenbal force-pushed the fix/2101-oidc-admin-issues branch 3 times, most recently from 11f9df2 to 0efc77e Compare February 19, 2024 11:03
Copy link
Contributor

@Bartvaderkin Bartvaderkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put some notes, although it is mostly the same feedback about filter/first being sub-optimal for tests, and showing intent.

Comment on lines 78 to 81
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()
Copy link
Contributor

@Bartvaderkin Bartvaderkin Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general tests are stronger if instead of .filter()/.first() (or .exists()) for single records to just use a .get() so it will also communicate and test the record must exists and cannot be multiple.

Also the test itself seems weird? I don't quite get what it tries to prove: isn't user the same record as db_user?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the tests were a bit messy already and I was adding stuff around that 😅.

I cleaned it up, the test is supposed to verify that an existing admin user (that is not yet OIDC), is updated if a user with the same email logs in with OIDC

Comment on lines 132 to 141
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, not quite clear what is tested here.

Comment on lines 190 to 199
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

callback_response, reverse("admin:index"), fetch_redirect_response=True
)

new_user = User.objects.filter(email="[email protected]").first()
Copy link
Contributor

@Bartvaderkin Bartvaderkin Feb 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is more clear as new_user and the test title work together.

Nevertheless the filter()/first() is a bit floppy and should just be a .get() (note there is even a assertIsNotNone(new_user) there to work around .first() ☹️

Comment on lines 353 to 355
new_user = User.objects.filter(email="[email protected]").first()

self.assertIsNotNone(new_user)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beep boop.

@@ -34,10 +36,15 @@ class OIDCFlowTests(TestCase):
@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),
return_value=OpenIDConnectConfig(id=1, enabled=True, make_users_staff=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen this patch+return_value often without the id=1 not sure if it matters.

Copy link
Contributor Author

@stevenbal stevenbal Feb 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does appear to be necessary in some cases, when the user groups are updated:

default_groups = self.config.default_groups.all()
...
ValueError: "<OpenIDConnectConfig: OpenID Connect configuration>" needs to have a value for field "id" before this many-to-many relationship can be used.

I'll check in which cases I can remove it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry too much, it was just an observation. The error makes sense if something in the test needs a saved instance, like we have some ForeignKey's to solo objects so we can use inline-admins.

Comment on lines 132 to 131
user.refresh_from_db()

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter/exists line tests the same thing as the user.refresh_from_db() + self.assertEqual(user.oidc_id, "some_username")

issue: https://taiga.maykinmedia.nl/project/open-inwoner/issue/2101

previously, if a user logged in with credentials for which there was no User in django yet, the User object would be created but the user would not actually be authenticated (and thus have to log in a second time)
ensure OIDC users:
* are redirected to the admin if OIDC is enabled for admins
* get is_staff is true when the User instance is created
@stevenbal stevenbal force-pushed the fix/2101-oidc-admin-issues branch from 0efc77e to 4282281 Compare February 22, 2024 10:03
Copy link
Contributor

@Bartvaderkin Bartvaderkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I left a small note but it is not blocking.

src/open_inwoner/accounts/tests/test_oidc_views.py Outdated Show resolved Hide resolved
@stevenbal stevenbal force-pushed the fix/2101-oidc-admin-issues branch from 4282281 to b388419 Compare February 23, 2024 09:03
@stevenbal stevenbal merged commit 96c3676 into develop Feb 23, 2024
15 checks passed
@stevenbal stevenbal deleted the fix/2101-oidc-admin-issues branch February 23, 2024 09:44
stevenbal added a commit that referenced this pull request Feb 23, 2024
🔧 [#2101] Add OIDC admin config to admin index fixture
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants