|
3 | 3 | import pytest
|
4 | 4 | from django.test import override_settings
|
5 | 5 |
|
| 6 | +from ansible_base.authentication.authenticator_plugins.utils import get_authenticator_plugin |
6 | 7 | from ansible_base.authentication.session import SessionAuthentication
|
| 8 | +from ansible_base.authentication.social_auth import SocialAuthMixin |
7 | 9 | from ansible_base.lib.utils.response import get_fully_qualified_url, get_relative_url
|
8 | 10 |
|
9 | 11 | authenticated_test_page = "authenticator-list"
|
@@ -79,3 +81,31 @@ def test_azuread_auth_failed(authenticate, unauthenticated_api_client, azuread_a
|
79 | 81 | url = get_relative_url(authenticated_test_page)
|
80 | 82 | response = client.get(url)
|
81 | 83 | assert response.status_code == 401
|
| 84 | + |
| 85 | + |
| 86 | +def test_groups_setting_and_user_groups(keycloak_authenticator): |
| 87 | + custom_groups_claim = "some_groups" |
| 88 | + |
| 89 | + class MockedDb: |
| 90 | + def __init__(self, group_claim): |
| 91 | + self.slug = "fake" |
| 92 | + self.configuration = {"GROUPS_CLAIM": group_claim} |
| 93 | + |
| 94 | + class MockBackend(SocialAuthMixin): |
| 95 | + database_instance = MockedDb(custom_groups_claim) |
| 96 | + |
| 97 | + def __init__(self): |
| 98 | + pass |
| 99 | + |
| 100 | + backend = MockBackend() |
| 101 | + |
| 102 | + ad = get_authenticator_plugin("ansible_base.authentication.authenticator_plugins.azuread") |
| 103 | + ad.database_instance = MockedDb(custom_groups_claim) |
| 104 | + |
| 105 | + # assert that groups claim setting is there and AD has the expected groups claim |
| 106 | + assert ad.strategy.get_setting('GROUPS_CLAIM', backend) == custom_groups_claim |
| 107 | + assert ad.groups_claim == custom_groups_claim |
| 108 | + |
| 109 | + # assert that AD returns expected user groups |
| 110 | + assert ad.get_user_groups() == [] |
| 111 | + assert ad.get_user_groups(["a", "b"]) == ["a", "b"] |
0 commit comments