| 1 |
# Copyright (c) 2021, Ansible Project |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from contextlib import contextmanager |
| 8 |
from io import StringIO |
| 9 |
from itertools import count |
| 10 |
from unittest.mock import patch |
| 11 |
|
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
AnsibleExitJson, |
| 14 |
ModuleTestCase, |
| 15 |
set_module_args, |
| 16 |
) |
| 17 |
|
| 18 |
from ansible_collections.community.general.plugins.modules import keycloak_realm_info |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def patch_keycloak_api(get_realm_info_by_id): |
| 23 |
"""Mock context manager for patching the methods in PwPolicyIPAClient that contact the IPA server |
| 24 |
|
| 25 |
Patches the `login` and `_post_json` methods |
| 26 |
|
| 27 |
Keyword arguments are passed to the mock object that patches `_post_json` |
| 28 |
|
| 29 |
No arguments are passed to the mock object that patches `login` because no tests require it |
| 30 |
|
| 31 |
Example:: |
| 32 |
|
| 33 |
with patch_ipa(return_value={}) as (mock_login, mock_post): |
| 34 |
... |
| 35 |
""" |
| 36 |
|
| 37 |
obj = keycloak_realm_info.KeycloakAPI |
| 38 |
with patch.object(obj, "get_realm_info_by_id", side_effect=get_realm_info_by_id) as mock_get_realm_info_by_id: |
| 39 |
yield mock_get_realm_info_by_id |
| 40 |
|
| 41 |
|
| 42 |
def get_response(object_with_future_response, method, get_id_call_count): |
| 43 |
if callable(object_with_future_response): |
| 44 |
return object_with_future_response() |
| 45 |
if isinstance(object_with_future_response, dict): |
| 46 |
return get_response(object_with_future_response[method], method, get_id_call_count) |
| 47 |
if isinstance(object_with_future_response, list): |
| 48 |
call_number = next(get_id_call_count) |
| 49 |
return get_response(object_with_future_response[call_number], method, get_id_call_count) |
| 50 |
return object_with_future_response |
| 51 |
|
| 52 |
|
| 53 |
def build_mocked_request(get_id_user_count, response_dict): |
| 54 |
def _mocked_requests(*args, **kwargs): |
| 55 |
url = args[0] |
| 56 |
method = kwargs["method"] |
| 57 |
future_response = response_dict.get(url, None) |
| 58 |
return get_response(future_response, method, get_id_user_count) |
| 59 |
|
| 60 |
return _mocked_requests |
| 61 |
|
| 62 |
|
| 63 |
def create_wrapper(text_as_string): |
| 64 |
"""Allow to mock many times a call to one address. |
| 65 |
Without this function, the StringIO is empty for the second call. |
| 66 |
""" |
| 67 |
|
| 68 |
def _create_wrapper(): |
| 69 |
return StringIO(text_as_string) |
| 70 |
|
| 71 |
return _create_wrapper |
| 72 |
|
| 73 |
|
| 74 |
def mock_good_connection(): |
| 75 |
token_response = { |
| 76 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 77 |
'{"access_token": "alongtoken"}' |
| 78 |
), |
| 79 |
} |
| 80 |
return patch( |
| 81 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 82 |
side_effect=build_mocked_request(count(), token_response), |
| 83 |
autospec=True, |
| 84 |
) |
| 85 |
|
| 86 |
|
| 87 |
class TestKeycloakRealmRole(ModuleTestCase): |
| 88 |
def setUp(self): |
| 89 |
super().setUp() |
| 90 |
self.module = keycloak_realm_info |
| 91 |
|
| 92 |
def test_get_public_info(self): |
| 93 |
"""Get realm public info""" |
| 94 |
|
| 95 |
module_args = { |
| 96 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 97 |
"realm": "my-realm", |
| 98 |
} |
| 99 |
return_value = [ |
| 100 |
None, |
| 101 |
{ |
| 102 |
"realm": "my-realm", |
| 103 |
"public_key": "MIIBIjANBgkqhkiG9w0BAQEF...", |
| 104 |
"token-service": "https://auth.mock.com/auth/realms/my-realm/protocol/openid-connect", |
| 105 |
"account-service": "https://auth.mock.com/auth/realms/my-realm/account", |
| 106 |
"tokens-not-before": 0, |
| 107 |
}, |
| 108 |
] |
| 109 |
|
| 110 |
# Run the module |
| 111 |
|
| 112 |
with set_module_args(module_args): |
| 113 |
with mock_good_connection(): |
| 114 |
with patch_keycloak_api(get_realm_info_by_id=return_value) as (mock_get_realm_info_by_id): |
| 115 |
with self.assertRaises(AnsibleExitJson): |
| 116 |
self.module.main() |
| 117 |
|
| 118 |
self.assertEqual(len(mock_get_realm_info_by_id.mock_calls), 1) |