1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-28 14:39:07 +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

@ -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_user_federation
@ -18,43 +21,44 @@ from itertools import count
@contextmanager
def patch_keycloak_api(get_components=None, get_component=None, create_component=None, update_component=None, delete_component=None):
"""Mock context manager for patching the methods in KeycloakAPI
"""
def patch_keycloak_api(
get_components=None, get_component=None, create_component=None, update_component=None, delete_component=None
):
"""Mock context manager for patching the methods in KeycloakAPI"""
obj = keycloak_user_federation.KeycloakAPI
with patch.object(obj, 'get_components', side_effect=get_components) \
as mock_get_components:
with patch.object(obj, 'get_component', side_effect=get_component) \
as mock_get_component:
with patch.object(obj, 'create_component', side_effect=create_component) \
as mock_create_component:
with patch.object(obj, 'update_component', side_effect=update_component) \
as mock_update_component:
with patch.object(obj, 'delete_component', side_effect=delete_component) \
as mock_delete_component:
yield mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component
with patch.object(obj, "get_components", side_effect=get_components) as mock_get_components:
with patch.object(obj, "get_component", side_effect=get_component) as mock_get_component:
with patch.object(obj, "create_component", side_effect=create_component) as mock_create_component:
with patch.object(obj, "update_component", side_effect=update_component) as mock_update_component:
with patch.object(obj, "delete_component", side_effect=delete_component) as mock_delete_component:
yield (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
)
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
@ -62,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,
)
@ -86,24 +95,24 @@ class TestKeycloakUserFederation(ModuleTestCase):
"""Add a new user federation"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'auth_realm': 'master',
'auth_username': 'admin',
'auth_password': 'admin',
'realm': 'realm-name',
'name': 'kerberos',
'state': 'present',
'provider_id': 'kerberos',
'provider_type': 'org.keycloak.storage.UserStorageProvider',
'config': {
'priority': 0,
'enabled': True,
'cachePolicy': 'DEFAULT',
'kerberosRealm': 'REALM',
'serverPrincipal': 'princ',
'keyTab': 'keytab',
'allowPasswordAuthentication': False,
'updateProfileFirstLogin': False,
"auth_keycloak_url": "http://keycloak.url/auth",
"auth_realm": "master",
"auth_username": "admin",
"auth_password": "admin",
"realm": "realm-name",
"name": "kerberos",
"state": "present",
"provider_id": "kerberos",
"provider_type": "org.keycloak.storage.UserStorageProvider",
"config": {
"priority": 0,
"enabled": True,
"cachePolicy": "DEFAULT",
"kerberosRealm": "REALM",
"serverPrincipal": "princ",
"keyTab": "keytab",
"allowPasswordAuthentication": False,
"updateProfileFirstLogin": False,
},
}
return_value_component_create = [
@ -114,45 +123,34 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerType": "org.keycloak.storage.UserStorageProvider",
"parentId": "kerberos",
"config": {
"serverPrincipal": [
"princ"
],
"allowPasswordAuthentication": [
"false"
],
"keyTab": [
"keytab"
],
"cachePolicy": [
"DEFAULT"
],
"updateProfileFirstLogin": [
"false"
],
"kerberosRealm": [
"REALM"
],
"priority": [
"0"
],
"enabled": [
"true"
]
}
"serverPrincipal": ["princ"],
"allowPasswordAuthentication": ["false"],
"keyTab": ["keytab"],
"cachePolicy": ["DEFAULT"],
"updateProfileFirstLogin": ["false"],
"kerberosRealm": ["REALM"],
"priority": ["0"],
"enabled": ["true"],
},
}
]
# get before_comp, get default_mapper, get after_mapper
return_value_components_get = [
[], [], []
]
return_value_components_get = [[], [], []]
changed = True
# Run the module
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_components=return_value_components_get, create_component=return_value_component_create) \
as (mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component):
with patch_keycloak_api(
get_components=return_value_components_get, create_component=return_value_component_create
) as (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
@ -163,30 +161,30 @@ class TestKeycloakUserFederation(ModuleTestCase):
self.assertEqual(len(mock_delete_component.mock_calls), 0)
# Verify that the module's changed status matches what is expected
self.assertIs(exec_info.exception.args[0]['changed'], changed)
self.assertIs(exec_info.exception.args[0]["changed"], changed)
def test_create_when_present(self):
"""Update existing user federation"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'auth_realm': 'master',
'auth_username': 'admin',
'auth_password': 'admin',
'realm': 'realm-name',
'name': 'kerberos',
'state': 'present',
'provider_id': 'kerberos',
'provider_type': 'org.keycloak.storage.UserStorageProvider',
'config': {
'priority': 0,
'enabled': True,
'cachePolicy': 'DEFAULT',
'kerberosRealm': 'REALM',
'serverPrincipal': 'princ',
'keyTab': 'keytab',
'allowPasswordAuthentication': False,
'updateProfileFirstLogin': False,
"auth_keycloak_url": "http://keycloak.url/auth",
"auth_realm": "master",
"auth_username": "admin",
"auth_password": "admin",
"realm": "realm-name",
"name": "kerberos",
"state": "present",
"provider_id": "kerberos",
"provider_type": "org.keycloak.storage.UserStorageProvider",
"config": {
"priority": 0,
"enabled": True,
"cachePolicy": "DEFAULT",
"kerberosRealm": "REALM",
"serverPrincipal": "princ",
"keyTab": "keytab",
"allowPasswordAuthentication": False,
"updateProfileFirstLogin": False,
},
}
return_value_components_get = [
@ -198,35 +196,19 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerType": "org.keycloak.storage.UserStorageProvider",
"parentId": "kerberos",
"config": {
"serverPrincipal": [
"princ"
],
"allowPasswordAuthentication": [
"false"
],
"keyTab": [
"keytab"
],
"cachePolicy": [
"DEFAULT"
],
"updateProfileFirstLogin": [
"false"
],
"kerberosRealm": [
"REALM"
],
"priority": [
"0"
],
"enabled": [
"false"
]
}
"serverPrincipal": ["princ"],
"allowPasswordAuthentication": ["false"],
"keyTab": ["keytab"],
"cachePolicy": ["DEFAULT"],
"updateProfileFirstLogin": ["false"],
"kerberosRealm": ["REALM"],
"priority": ["0"],
"enabled": ["false"],
},
}
],
[],
[]
[],
]
return_value_component_get = [
{
@ -236,45 +218,35 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerType": "org.keycloak.storage.UserStorageProvider",
"parentId": "kerberos",
"config": {
"serverPrincipal": [
"princ"
],
"allowPasswordAuthentication": [
"false"
],
"keyTab": [
"keytab"
],
"cachePolicy": [
"DEFAULT"
],
"updateProfileFirstLogin": [
"false"
],
"kerberosRealm": [
"REALM"
],
"priority": [
"0"
],
"enabled": [
"true"
]
}
"serverPrincipal": ["princ"],
"allowPasswordAuthentication": ["false"],
"keyTab": ["keytab"],
"cachePolicy": ["DEFAULT"],
"updateProfileFirstLogin": ["false"],
"kerberosRealm": ["REALM"],
"priority": ["0"],
"enabled": ["true"],
},
}
]
return_value_component_update = [
None
]
return_value_component_update = [None]
changed = True
# Run the module
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_components=return_value_components_get, get_component=return_value_component_get,
update_component=return_value_component_update) \
as (mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component):
with patch_keycloak_api(
get_components=return_value_components_get,
get_component=return_value_component_get,
update_component=return_value_component_update,
) as (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
@ -285,60 +257,60 @@ class TestKeycloakUserFederation(ModuleTestCase):
self.assertEqual(len(mock_delete_component.mock_calls), 0)
# Verify that the module's changed status matches what is expected
self.assertIs(exec_info.exception.args[0]['changed'], changed)
self.assertIs(exec_info.exception.args[0]["changed"], changed)
def test_create_with_mappers(self):
"""Add a new user federation with mappers"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'auth_realm': 'master',
'auth_username': 'admin',
'auth_password': 'admin',
'realm': 'realm-name',
'name': 'ldap',
'state': 'present',
'provider_id': 'ldap',
'provider_type': 'org.keycloak.storage.UserStorageProvider',
'config': {
'priority': 0,
'enabled': True,
'cachePolicy': 'DEFAULT',
'batchSizeForSync': 1000,
'editMode': 'READ_ONLY',
'importEnabled': True,
'syncRegistrations': False,
'vendor': 'other',
'usernameLDAPAttribute': 'uid',
'rdnLDAPAttribute': 'uid',
'uuidLDAPAttribute': 'entryUUID',
'userObjectClasses': 'inetOrgPerson, organizationalPerson',
'connectionUrl': 'ldaps://ldap.example.com:636',
'usersDn': 'ou=Users,dc=example,dc=com',
'authType': 'none',
'searchScope': 1,
'validatePasswordPolicy': False,
'trustEmail': False,
'useTruststoreSpi': 'ldapsOnly',
'connectionPooling': True,
'pagination': True,
'allowKerberosAuthentication': False,
'krbPrincipalAttribute': 'krbPrincipalName',
'debug': False,
'useKerberosForPasswordAuthentication': False,
"auth_keycloak_url": "http://keycloak.url/auth",
"auth_realm": "master",
"auth_username": "admin",
"auth_password": "admin",
"realm": "realm-name",
"name": "ldap",
"state": "present",
"provider_id": "ldap",
"provider_type": "org.keycloak.storage.UserStorageProvider",
"config": {
"priority": 0,
"enabled": True,
"cachePolicy": "DEFAULT",
"batchSizeForSync": 1000,
"editMode": "READ_ONLY",
"importEnabled": True,
"syncRegistrations": False,
"vendor": "other",
"usernameLDAPAttribute": "uid",
"rdnLDAPAttribute": "uid",
"uuidLDAPAttribute": "entryUUID",
"userObjectClasses": "inetOrgPerson, organizationalPerson",
"connectionUrl": "ldaps://ldap.example.com:636",
"usersDn": "ou=Users,dc=example,dc=com",
"authType": "none",
"searchScope": 1,
"validatePasswordPolicy": False,
"trustEmail": False,
"useTruststoreSpi": "ldapsOnly",
"connectionPooling": True,
"pagination": True,
"allowKerberosAuthentication": False,
"krbPrincipalAttribute": "krbPrincipalName",
"debug": False,
"useKerberosForPasswordAuthentication": False,
},
'mappers': [
"mappers": [
{
'name': 'full name',
'providerId': 'full-name-ldap-mapper',
'providerType': 'org.keycloak.storage.ldap.mappers.LDAPStorageMapper',
'config': {
'ldap.full.name.attribute': 'cn',
'read.only': True,
'write.only': False,
}
"name": "full name",
"providerId": "full-name-ldap-mapper",
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper",
"config": {
"ldap.full.name.attribute": "cn",
"read.only": True,
"write.only": False,
},
}
]
],
}
return_value_components_get = [
[],
@ -350,13 +322,13 @@ class TestKeycloakUserFederation(ModuleTestCase):
"is.mandatory.in.ldap": "false",
"ldap.attribute": "mail",
"read.only": "true",
"user.model.attribute": "email"
"user.model.attribute": "email",
},
"id": "77e1763f-c51a-4286-bade-75577d64803c",
"name": "email",
"parentId": "e5f48aa3-b56b-4983-a8ad-2c7b8b5e77cb",
"providerId": "user-attribute-ldap-mapper",
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper"
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper",
},
],
[
@ -366,23 +338,11 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerId": "full-name-ldap-mapper",
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper",
"parentId": "eb691537-b73c-4cd8-b481-6031c26499d8",
"config": {
"ldap.full.name.attribute": [
"cn"
],
"read.only": [
"true"
],
"write.only": [
"false"
]
}
"config": {"ldap.full.name.attribute": ["cn"], "read.only": ["true"], "write.only": ["false"]},
}
]
]
return_value_component_delete = [
None
],
]
return_value_component_delete = [None]
return_value_component_create = [
{
"id": "eb691537-b73c-4cd8-b481-6031c26499d8",
@ -391,82 +351,32 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerType": "org.keycloak.storage.UserStorageProvider",
"parentId": "ldap",
"config": {
"pagination": [
"true"
],
"connectionPooling": [
"true"
],
"usersDn": [
"ou=Users,dc=example,dc=com"
],
"cachePolicy": [
"DEFAULT"
],
"useKerberosForPasswordAuthentication": [
"false"
],
"importEnabled": [
"true"
],
"enabled": [
"true"
],
"krbPrincipalAttribute": [
"krb5PrincipalName"
],
"usernameLDAPAttribute": [
"uid"
],
"vendor": [
"other"
],
"uuidLDAPAttribute": [
"entryUUID"
],
"connectionUrl": [
"ldaps://ldap.example.com:636"
],
"allowKerberosAuthentication": [
"false"
],
"syncRegistrations": [
"false"
],
"authType": [
"none"
],
"debug": [
"false"
],
"searchScope": [
"1"
],
"useTruststoreSpi": [
"ldapsOnly"
],
"trustEmail": [
"false"
],
"priority": [
"0"
],
"userObjectClasses": [
"inetOrgPerson, organizationalPerson"
],
"rdnLDAPAttribute": [
"uid"
],
"editMode": [
"READ_ONLY"
],
"validatePasswordPolicy": [
"false"
],
"batchSizeForSync": [
"1000"
]
}
"pagination": ["true"],
"connectionPooling": ["true"],
"usersDn": ["ou=Users,dc=example,dc=com"],
"cachePolicy": ["DEFAULT"],
"useKerberosForPasswordAuthentication": ["false"],
"importEnabled": ["true"],
"enabled": ["true"],
"krbPrincipalAttribute": ["krb5PrincipalName"],
"usernameLDAPAttribute": ["uid"],
"vendor": ["other"],
"uuidLDAPAttribute": ["entryUUID"],
"connectionUrl": ["ldaps://ldap.example.com:636"],
"allowKerberosAuthentication": ["false"],
"syncRegistrations": ["false"],
"authType": ["none"],
"debug": ["false"],
"searchScope": ["1"],
"useTruststoreSpi": ["ldapsOnly"],
"trustEmail": ["false"],
"priority": ["0"],
"userObjectClasses": ["inetOrgPerson, organizationalPerson"],
"rdnLDAPAttribute": ["uid"],
"editMode": ["READ_ONLY"],
"validatePasswordPolicy": ["false"],
"batchSizeForSync": ["1000"],
},
},
{
"id": "2dfadafd-8b34-495f-a98b-153e71a22311",
@ -474,18 +384,8 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerId": "full-name-ldap-mapper",
"providerType": "org.keycloak.storage.ldap.mappers.LDAPStorageMapper",
"parentId": "eb691537-b73c-4cd8-b481-6031c26499d8",
"config": {
"ldap.full.name.attribute": [
"cn"
],
"read.only": [
"true"
],
"write.only": [
"false"
]
}
}
"config": {"ldap.full.name.attribute": ["cn"], "read.only": ["true"], "write.only": ["false"]},
},
]
changed = True
@ -493,8 +393,15 @@ class TestKeycloakUserFederation(ModuleTestCase):
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_components=return_value_components_get, create_component=return_value_component_create) \
as (mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component):
with patch_keycloak_api(
get_components=return_value_components_get, create_component=return_value_component_create
) as (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
@ -505,31 +412,34 @@ class TestKeycloakUserFederation(ModuleTestCase):
self.assertEqual(len(mock_delete_component.mock_calls), 1)
# Verify that the module's changed status matches what is expected
self.assertIs(exec_info.exception.args[0]['changed'], changed)
self.assertIs(exec_info.exception.args[0]["changed"], changed)
def test_delete_when_absent(self):
"""Remove an absent user federation"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'auth_realm': 'master',
'auth_username': 'admin',
'auth_password': 'admin',
'realm': 'realm-name',
'name': 'kerberos',
'state': 'absent',
"auth_keycloak_url": "http://keycloak.url/auth",
"auth_realm": "master",
"auth_username": "admin",
"auth_password": "admin",
"realm": "realm-name",
"name": "kerberos",
"state": "absent",
}
return_value_components_get = [
[]
]
return_value_components_get = [[]]
changed = False
# Run the module
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_components=return_value_components_get) \
as (mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component):
with patch_keycloak_api(get_components=return_value_components_get) as (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
@ -540,19 +450,19 @@ class TestKeycloakUserFederation(ModuleTestCase):
self.assertEqual(len(mock_delete_component.mock_calls), 0)
# Verify that the module's changed status matches what is expected
self.assertIs(exec_info.exception.args[0]['changed'], changed)
self.assertIs(exec_info.exception.args[0]["changed"], changed)
def test_delete_when_present(self):
"""Remove an existing user federation"""
module_args = {
'auth_keycloak_url': 'http://keycloak.url/auth',
'auth_realm': 'master',
'auth_username': 'admin',
'auth_password': 'admin',
'realm': 'realm-name',
'name': 'kerberos',
'state': 'absent',
"auth_keycloak_url": "http://keycloak.url/auth",
"auth_realm": "master",
"auth_username": "admin",
"auth_password": "admin",
"realm": "realm-name",
"name": "kerberos",
"state": "absent",
}
return_value_components_get = [
[
@ -563,46 +473,35 @@ class TestKeycloakUserFederation(ModuleTestCase):
"providerType": "org.keycloak.storage.UserStorageProvider",
"parentId": "kerberos",
"config": {
"serverPrincipal": [
"princ"
],
"allowPasswordAuthentication": [
"false"
],
"keyTab": [
"keytab"
],
"cachePolicy": [
"DEFAULT"
],
"updateProfileFirstLogin": [
"false"
],
"kerberosRealm": [
"REALM"
],
"priority": [
"0"
],
"enabled": [
"false"
]
}
"serverPrincipal": ["princ"],
"allowPasswordAuthentication": ["false"],
"keyTab": ["keytab"],
"cachePolicy": ["DEFAULT"],
"updateProfileFirstLogin": ["false"],
"kerberosRealm": ["REALM"],
"priority": ["0"],
"enabled": ["false"],
},
}
],
[]
]
return_value_component_delete = [
None
[],
]
return_value_component_delete = [None]
changed = True
# Run the module
with set_module_args(module_args):
with mock_good_connection():
with patch_keycloak_api(get_components=return_value_components_get, delete_component=return_value_component_delete) \
as (mock_get_components, mock_get_component, mock_create_component, mock_update_component, mock_delete_component):
with patch_keycloak_api(
get_components=return_value_components_get, delete_component=return_value_component_delete
) as (
mock_get_components,
mock_get_component,
mock_create_component,
mock_update_component,
mock_delete_component,
):
with self.assertRaises(AnsibleExitJson) as exec_info:
self.module.main()
@ -613,8 +512,8 @@ class TestKeycloakUserFederation(ModuleTestCase):
self.assertEqual(len(mock_delete_component.mock_calls), 1)
# Verify that the module's changed status matches what is expected
self.assertIs(exec_info.exception.args[0]['changed'], changed)
self.assertIs(exec_info.exception.args[0]["changed"], changed)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()