-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change user_is_authorised for check_allowed for google
- Loading branch information
1 parent
7de6b4e
commit d756f30
Showing
2 changed files
with
120 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,55 +35,75 @@ def google_client(client): | |
async def test_google(google_client): | ||
authenticator = GoogleOAuthenticator() | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
assert sorted(user_info) == ['admin', 'auth_state', 'name'] | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
auth_state = user_info['auth_state'] | ||
assert 'access_token' in auth_state | ||
assert 'google_user' in auth_state | ||
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
assert sorted(user_info) == ['admin', 'auth_state', 'name'] | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
auth_state = user_info['auth_state'] | ||
assert 'access_token' in auth_state | ||
assert 'google_user' in auth_state | ||
|
||
|
||
async def test_google_username_claim(google_client): | ||
cfg = Config() | ||
cfg.GoogleOAuthenticator.username_claim = "sub" | ||
authenticator = GoogleOAuthenticator(config=cfg) | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
assert sorted(user_info) == ['admin', 'auth_state', 'name'] | ||
name = user_info['name'] | ||
assert name == '724f95667e2fbe903ee1b4cffcae3b25' | ||
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
assert sorted(user_info) == ['admin', 'auth_state', 'name'] | ||
name = user_info['name'] | ||
assert name == '724f95667e2fbe903ee1b4cffcae3b25' | ||
|
||
|
||
async def test_hosted_domain(google_client): | ||
authenticator = GoogleOAuthenticator(hosted_domain=['email.com']) | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
|
||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
with raises(HTTPError) as exc: | ||
name = await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
with raises(HTTPError) as exc: | ||
name = await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
|
||
|
||
async def test_multiple_hosted_domain(google_client): | ||
authenticator = GoogleOAuthenticator(hosted_domain=['email.com', 'mycollege.edu']) | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
|
||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
user_info = await authenticator.get_authenticated_user(handler, None) | ||
name = user_info['name'] | ||
assert name == '[email protected]' | ||
|
||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
with raises(HTTPError) as exc: | ||
name = await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
with raises(HTTPError) as exc: | ||
name = await authenticator.get_authenticated_user(handler, None) | ||
assert exc.value.status_code == 403 | ||
|
||
|
||
async def test_admin_google_groups(google_client): | ||
|
@@ -96,7 +116,7 @@ async def test_admin_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakeadmingroup'], | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
admin_user_info = await authenticator.get_authenticated_user(handler, None) | ||
# Make sure the user authenticated successfully | ||
|
@@ -107,22 +127,20 @@ async def test_admin_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakegroup'], | ||
lambda *args: ['anotherone', 'fakegroup'], | ||
): | ||
allowed_user_info = await authenticator.authenticate( | ||
handler, | ||
) | ||
allowed_user_info = await authenticator.get_authenticated_user(handler, None) | ||
allowed_user_groups = allowed_user_info['auth_state']['google_user'][ | ||
'google_groups' | ||
] | ||
admin_user = allowed_user_info['admin'] | ||
assert 'fakegroup' in allowed_user_groups | ||
assert admin_user == False | ||
assert not admin_user | ||
handler = google_client.handler_for_user(user_model('[email protected]')) | ||
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakenonallowedgroup'], | ||
lambda *args: ['anotherone', 'fakenonallowedgroup'], | ||
): | ||
allowed_user_groups = await authenticator.get_authenticated_user(handler, None) | ||
assert allowed_user_groups is None | ||
|
@@ -138,7 +156,7 @@ async def test_admin_user_but_no_admin_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakegroup'], | ||
lambda *args: ['anotherone', 'fakegroup'], | ||
): | ||
admin_user_info = await authenticator.get_authenticated_user(handler, data=None) | ||
# Make sure the user authenticated successfully | ||
|
@@ -156,15 +174,15 @@ async def test_allowed_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakeadmingroup'], | ||
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', | ||
return_value=['anotherone', 'fakegroup'], | ||
lambda *args: ['anotherone', 'fakegroup'], | ||
): | ||
allowed_user_info = await authenticator.get_authenticated_user(handler, None) | ||
allowed_user_groups = allowed_user_info['auth_state']['google_user'][ | ||
|
@@ -177,13 +195,13 @@ async def test_allowed_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakenonallowedgroup'], | ||
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', return_value=['fakegroup'] | ||
authenticator, '_google_groups_for_user', lambda *args: ['fakegroup'] | ||
): | ||
allowed_user_groups = await authenticator.get_authenticated_user(handler, None) | ||
assert allowed_user_groups is None | ||
|
@@ -198,7 +216,7 @@ async def test_admin_only_google_groups(google_client): | |
with mock.patch.object( | ||
authenticator, | ||
'_google_groups_for_user', | ||
return_value=['anotherone', 'fakeadmingroup'], | ||
lambda *args: ['anotherone', 'fakeadmingroup'], | ||
): | ||
admin_user_info = await authenticator.get_authenticated_user(handler, None) | ||
admin_user = admin_user_info['admin'] | ||
|
@@ -219,5 +237,5 @@ def test_deprecated_config(caplog): | |
'GoogleOAuthenticator.allowed_google_groups instead', | ||
) in caplog.record_tuples | ||
|
||
assert authenticator.allowed_google_groups == {'email.com': ['group']} | ||
assert authenticator.allowed_google_groups == {'email.com': {'group'}} | ||
assert authenticator.allowed_users == {"user1"} |