1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-15 16:31:30 +00:00

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

@ -15,22 +15,23 @@ from ansible_collections.community.general.plugins.module_utils.identity.keycloa
)
module_params_creds = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'validate_certs': True,
'auth_realm': 'master',
'client_id': 'admin-cli',
'auth_username': 'admin',
'auth_password': 'admin',
'client_secret': None,
"auth_keycloak_url": "http://keycloak.url/auth",
"validate_certs": True,
"auth_realm": "master",
"client_id": "admin-cli",
"auth_username": "admin",
"auth_password": "admin",
"client_secret": None,
}
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
@ -38,16 +39,14 @@ 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):
try:
call_number = get_id_call_count.__next__()
except AttributeError:
# manage python 2 versions.
call_number = get_id_call_count.next()
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
@ -55,52 +54,52 @@ 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
@pytest.fixture()
def mock_good_connection(mocker):
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 mocker.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,
)
def test_connect_to_keycloak_with_creds(mock_good_connection):
keycloak_header = get_token(module_params_creds)
assert keycloak_header == {
'Authorization': 'Bearer alongtoken',
'Content-Type': 'application/json'
}
assert keycloak_header == {"Authorization": "Bearer alongtoken", "Content-Type": "application/json"}
def test_connect_to_keycloak_with_token(mock_good_connection):
module_params_token = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'validate_certs': True,
'client_id': 'admin-cli',
'token': "alongtoken"
"auth_keycloak_url": "http://keycloak.url/auth",
"validate_certs": True,
"client_id": "admin-cli",
"token": "alongtoken",
}
keycloak_header = get_token(module_params_token)
assert keycloak_header == {
'Authorization': 'Bearer alongtoken',
'Content-Type': 'application/json'
}
assert keycloak_header == {"Authorization": "Bearer alongtoken", "Content-Type": "application/json"}
@pytest.fixture()
def mock_bad_json_returned(mocker):
token_response = {
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token': create_wrapper('{"access_token":'), }
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper('{"access_token":'),
}
return mocker.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,
)
@ -110,27 +109,29 @@ def test_bad_json_returned(mock_bad_json_returned):
# cannot check all the message, different errors message for the value
# error in python 2.6, 2.7 and 3.*.
assert (
'API returned invalid JSON when trying to obtain access token from '
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token: '
"API returned invalid JSON when trying to obtain access token from "
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token: "
) in str(raised_error.value)
def raise_401(url):
def _raise_401():
raise HTTPError(url=url, code=401, msg='Unauthorized', hdrs='', fp=StringIO(''))
raise HTTPError(url=url, code=401, msg="Unauthorized", hdrs="", fp=StringIO(""))
return _raise_401
@pytest.fixture()
def mock_401_returned(mocker):
token_response = {
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token': raise_401(
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token'),
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": raise_401(
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token"
),
}
return mocker.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,
)
@ -138,20 +139,23 @@ def test_error_returned(mock_401_returned):
with pytest.raises(KeycloakError) as raised_error:
get_token(module_params_creds)
assert str(raised_error.value) == (
'Could not obtain access token from http://keycloak.url'
'/auth/realms/master/protocol/openid-connect/token: '
'HTTP Error 401: Unauthorized'
"Could not obtain access token from http://keycloak.url"
"/auth/realms/master/protocol/openid-connect/token: "
"HTTP Error 401: Unauthorized"
)
@pytest.fixture()
def mock_json_without_token_returned(mocker):
token_response = {
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token': create_wrapper('{"not_token": "It is not a token"}'), }
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper(
'{"not_token": "It is not a token"}'
),
}
return mocker.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,
)
@ -159,6 +163,6 @@ def test_json_without_token_returned(mock_json_without_token_returned):
with pytest.raises(KeycloakError) as raised_error:
get_token(module_params_creds)
assert str(raised_error.value) == (
'API did not include access_token field in response from '
'http://keycloak.url/auth/realms/master/protocol/openid-connect/token'
"API did not include access_token field in response from "
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token"
)

View file

@ -11,74 +11,44 @@ from ansible_collections.community.general.plugins.module_utils.identity.keycloa
class KeycloakIsStructIncludedTestCase(unittest.TestCase):
dict1 = dict(
test1='test1',
test2=dict(
test1='test1',
test2='test2'
),
test3=['test1', dict(test='test1', test2='test2')]
test1="test1", test2=dict(test1="test1", test2="test2"), test3=["test1", dict(test="test1", test2="test2")]
)
dict2 = dict(
test1='test1',
test2=dict(
test1='test1',
test2='test2',
test3='test3'
),
test3=['test1', dict(test='test1', test2='test2'), 'test3'],
test4='test4'
test1="test1",
test2=dict(test1="test1", test2="test2", test3="test3"),
test3=["test1", dict(test="test1", test2="test2"), "test3"],
test4="test4",
)
dict3 = dict(
test1='test1',
test2=dict(
test1='test1',
test2='test23',
test3='test3'
),
test3=['test1', dict(test='test1', test2='test23'), 'test3'],
test4='test4'
test1="test1",
test2=dict(test1="test1", test2="test23", test3="test3"),
test3=["test1", dict(test="test1", test2="test23"), "test3"],
test4="test4",
)
dict5 = dict(
test1='test1',
test2=dict(
test1=True,
test2='test23',
test3='test3'
),
test3=['test1', dict(test='test1', test2='test23'), 'test3'],
test4='test4'
test1="test1",
test2=dict(test1=True, test2="test23", test3="test3"),
test3=["test1", dict(test="test1", test2="test23"), "test3"],
test4="test4",
)
dict6 = dict(
test1='test1',
test2=dict(
test1='true',
test2='test23',
test3='test3'
),
test3=['test1', dict(test='test1', test2='test23'), 'test3'],
test4='test4'
test1="test1",
test2=dict(test1="true", test2="test23", test3="test3"),
test3=["test1", dict(test="test1", test2="test23"), "test3"],
test4="test4",
)
dict7 = [
{
'roles': ['view-clients', 'view-identity-providers', 'view-users', 'query-realms', 'manage-users'],
'clientid': 'master-realm'
"roles": ["view-clients", "view-identity-providers", "view-users", "query-realms", "manage-users"],
"clientid": "master-realm",
},
{
'roles': ['manage-account', 'view-profile', 'manage-account-links'],
'clientid': 'account'
}
{"roles": ["manage-account", "view-profile", "manage-account-links"], "clientid": "account"},
]
dict8 = [
{
'roles': ['view-clients', 'query-realms', 'view-users'],
'clientid': 'master-realm'
},
{
'roles': ['manage-account-links', 'view-profile', 'manage-account'],
'clientid': 'account'
}
{"roles": ["view-clients", "query-realms", "view-users"], "clientid": "master-realm"},
{"roles": ["manage-account-links", "view-profile", "manage-account"], "clientid": "account"},
]
def test_trivial(self):