Reformat everything.

This commit is contained in:
Felix Fontein
2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View File

@@ -1,4 +1,3 @@
# Copyright (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
@@ -9,7 +8,11 @@ import unittest
from contextlib import contextmanager
from unittest.mock import patch
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import AnsibleExitJson, ModuleTestCase, set_module_args
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
AnsibleExitJson,
ModuleTestCase,
set_module_args,
)
from ansible_collections.community.general.plugins.modules import keycloak_realm_info
@@ -34,7 +37,7 @@ def patch_keycloak_api(get_realm_info_by_id):
"""
obj = keycloak_realm_info.KeycloakAPI
with patch.object(obj, 'get_realm_info_by_id', side_effect=get_realm_info_by_id) as mock_get_realm_info_by_id:
with patch.object(obj, "get_realm_info_by_id", side_effect=get_realm_info_by_id) as mock_get_realm_info_by_id:
yield mock_get_realm_info_by_id
@@ -42,21 +45,20 @@ def get_response(object_with_future_response, method, get_id_call_count):
if callable(object_with_future_response):
return object_with_future_response()
if isinstance(object_with_future_response, dict):
return get_response(
object_with_future_response[method], method, get_id_call_count)
return get_response(object_with_future_response[method], method, get_id_call_count)
if isinstance(object_with_future_response, list):
call_number = next(get_id_call_count)
return get_response(
object_with_future_response[call_number], method, get_id_call_count)
return get_response(object_with_future_response[call_number], method, get_id_call_count)
return object_with_future_response
def build_mocked_request(get_id_user_count, response_dict):
def _mocked_requests(*args, **kwargs):
url = args[0]
method = kwargs['method']
method = kwargs["method"]
future_response = response_dict.get(url, None)
return get_response(future_response, method, get_id_user_count)
return _mocked_requests
@@ -64,18 +66,23 @@ def create_wrapper(text_as_string):
"""Allow to mock many times a call to one address.
Without this function, the StringIO is empty for the second call.
"""
def _create_wrapper():
return StringIO(text_as_string)
return _create_wrapper
def mock_good_connection():
token_response = {
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token': create_wrapper('{"access_token": "alongtoken"}'), }
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper(
'{"access_token": "alongtoken"}'
),
}
return patch(
'ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak.open_url',
"ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak.open_url",
side_effect=build_mocked_request(count(), token_response),
autospec=True
autospec=True,
)
@@ -88,8 +95,8 @@ class TestKeycloakRealmRole(ModuleTestCase):
"""Get realm public info"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'realm': 'my-realm',
"auth_keycloak_url": "http://keycloak.url/auth",
"realm": "my-realm",
}
return_value = [
None,
@@ -99,20 +106,19 @@ class TestKeycloakRealmRole(ModuleTestCase):
"token-service": "https://auth.mock.com/auth/realms/my-realm/protocol/openid-connect",
"account-service": "https://auth.mock.com/auth/realms/my-realm/account",
"tokens-not-before": 0,
}
},
]
# Run the module
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_realm_info_by_id=return_value) \
as (mock_get_realm_info_by_id):
with patch_keycloak_api(get_realm_info_by_id=return_value) as (mock_get_realm_info_by_id):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
self.assertEqual(len(mock_get_realm_info_by_id.mock_calls), 1)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()