Skip to content

Commit

Permalink
refactor, google: rename _google_groups_for_user to _fetch_user_groups
Browse files Browse the repository at this point in the history
  • Loading branch information
consideRatio committed Jun 23, 2023
1 parent 3c62101 commit fd06b47
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
14 changes: 7 additions & 7 deletions oauthenticator/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ async def update_auth_model(self, auth_model):
user_info = auth_model["auth_state"][self.user_auth_state_key]
user_email = user_info["email"]
user_domain = user_email.split("@")[1]
user_groups = set(self._google_groups_for_user(user_email, user_domain))
user_groups = self._fetch_user_groups(user_email, user_domain)
admin_groups = self.admin_google_groups.get(user_domain, set())

if any(user_groups & admin_groups):
Expand Down Expand Up @@ -253,7 +253,7 @@ def _service_client(self, service_name, service_version, credentials, http=None)
http=http,
)

def _google_groups_for_user(self, user_email, user_email_domain, http=None):
def _fetch_user_groups(self, user_email, user_email_domain, http=None):
"""
Return a set with the google groups a given user is a member of
"""
Expand All @@ -273,12 +273,12 @@ def _google_groups_for_user(self, user_email, user_email_domain, http=None):
http=http,
)

results = service.groups().list(userKey=user_email).execute()
results = {
g['email'].split('@')[0] for g in results.get('groups', [{'email': None}])
resp = service.groups().list(userKey=user_email).execute()
user_groups = {
g['email'].split('@')[0] for g in resp.get('groups', [{'email': None}])
}
self.log.debug(f"user_email {user_email} is a member of {results}")
return results
self.log.debug(f"user_email {user_email} is a member of {user_groups}")
return user_groups


class LocalGoogleOAuthenticator(LocalAuthenticator, GoogleOAuthenticator):
Expand Down
50 changes: 25 additions & 25 deletions oauthenticator/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ async def test_google(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
user_info = await authenticator.get_authenticated_user(handler, None)
assert sorted(user_info) == ['admin', 'auth_state', 'name']
Expand All @@ -56,8 +56,8 @@ async def test_google_username_claim(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
user_info = await authenticator.get_authenticated_user(handler, None)
assert sorted(user_info) == ['admin', 'auth_state', 'name']
Expand All @@ -70,8 +70,8 @@ async def test_hosted_domain(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
user_info = await authenticator.get_authenticated_user(handler, None)
name = user_info['name']
Expand All @@ -88,8 +88,8 @@ async def test_multiple_hosted_domain(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
user_info = await authenticator.get_authenticated_user(handler, None)
name = user_info['name']
Expand All @@ -115,8 +115,8 @@ async def test_admin_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
admin_user_info = await authenticator.get_authenticated_user(handler, None)
# Make sure the user authenticated successfully
Expand All @@ -126,8 +126,8 @@ async def test_admin_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakegroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakegroup'},
):
allowed_user_info = await authenticator.get_authenticated_user(handler, None)
allowed_user_groups = allowed_user_info['auth_state']['google_user'][
Expand All @@ -139,8 +139,8 @@ async def test_admin_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakenonallowedgroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakenonallowedgroup'},
):
allowed_user_groups = await authenticator.get_authenticated_user(handler, None)
assert allowed_user_groups is None
Expand All @@ -155,8 +155,8 @@ async def test_admin_user_but_no_admin_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakegroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakegroup'},
):
admin_user_info = await authenticator.get_authenticated_user(handler, data=None)
# Make sure the user authenticated successfully
Expand All @@ -173,16 +173,16 @@ async def test_allowed_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
admin_user_info = await authenticator.get_authenticated_user(handler, None)
assert admin_user_info is None
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakegroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakegroup'},
):
allowed_user_info = await authenticator.get_authenticated_user(handler, None)
allowed_user_groups = allowed_user_info['auth_state']['google_user'][
Expand All @@ -194,14 +194,14 @@ async def test_allowed_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakenonallowedgroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakenonallowedgroup'},
):
allowed_user_groups = await authenticator.get_authenticated_user(handler, None)
assert allowed_user_groups is None
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator, '_google_groups_for_user', lambda *args: ['fakegroup']
authenticator, '_fetch_user_groups', lambda *args: {'fakegroup'}
):
allowed_user_groups = await authenticator.get_authenticated_user(handler, None)
assert allowed_user_groups is None
Expand All @@ -215,8 +215,8 @@ async def test_admin_only_google_groups(google_client):
handler = google_client.handler_for_user(user_model('[email protected]'))
with mock.patch.object(
authenticator,
'_google_groups_for_user',
lambda *args: ['anotherone', 'fakeadmingroup'],
'_fetch_user_groups',
lambda *args: {'anotherone', 'fakeadmingroup'},
):
admin_user_info = await authenticator.get_authenticated_user(handler, None)
admin_user = admin_user_info['admin']
Expand Down

0 comments on commit fd06b47

Please sign in to comment.