| 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_client |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def patch_keycloak_api( |
| 23 |
get_client_by_clientid=None, get_client_by_id=None, update_client=None, create_client=None, delete_client=None |
| 24 |
): |
| 25 |
"""Mock context manager for patching the methods in PwPolicyIPAClient that contact the IPA server |
| 26 |
|
| 27 |
Patches the `login` and `_post_json` methods |
| 28 |
|
| 29 |
Keyword arguments are passed to the mock object that patches `_post_json` |
| 30 |
|
| 31 |
No arguments are passed to the mock object that patches `login` because no tests require it |
| 32 |
|
| 33 |
Example:: |
| 34 |
|
| 35 |
with patch_ipa(return_value={}) as (mock_login, mock_post): |
| 36 |
... |
| 37 |
""" |
| 38 |
|
| 39 |
obj = keycloak_client.KeycloakAPI |
| 40 |
with patch.object(obj, "get_client_by_clientid", side_effect=get_client_by_clientid) as mock_get_client_by_clientid: |
| 41 |
with patch.object(obj, "get_client_by_id", side_effect=get_client_by_id) as mock_get_client_by_id: |
| 42 |
with patch.object(obj, "create_client", side_effect=create_client) as mock_create_client: |
| 43 |
with patch.object(obj, "update_client", side_effect=update_client) as mock_update_client: |
| 44 |
with patch.object(obj, "delete_client", side_effect=delete_client) as mock_delete_client: |
| 45 |
yield ( |
| 46 |
mock_get_client_by_clientid, |
| 47 |
mock_get_client_by_id, |
| 48 |
mock_create_client, |
| 49 |
mock_update_client, |
| 50 |
mock_delete_client, |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def get_response(object_with_future_response, method, get_id_call_count): |
| 55 |
if callable(object_with_future_response): |
| 56 |
return object_with_future_response() |
| 57 |
if isinstance(object_with_future_response, dict): |
| 58 |
return get_response(object_with_future_response[method], method, get_id_call_count) |
| 59 |
if isinstance(object_with_future_response, list): |
| 60 |
call_number = next(get_id_call_count) |
| 61 |
return get_response(object_with_future_response[call_number], method, get_id_call_count) |
| 62 |
return object_with_future_response |
| 63 |
|
| 64 |
|
| 65 |
def build_mocked_request(get_id_user_count, response_dict): |
| 66 |
def _mocked_requests(*args, **kwargs): |
| 67 |
url = args[0] |
| 68 |
method = kwargs["method"] |
| 69 |
future_response = response_dict.get(url, None) |
| 70 |
return get_response(future_response, method, get_id_user_count) |
| 71 |
|
| 72 |
return _mocked_requests |
| 73 |
|
| 74 |
|
| 75 |
def create_wrapper(text_as_string): |
| 76 |
"""Allow to mock many times a call to one address. |
| 77 |
Without this function, the StringIO is empty for the second call. |
| 78 |
""" |
| 79 |
|
| 80 |
def _create_wrapper(): |
| 81 |
return StringIO(text_as_string) |
| 82 |
|
| 83 |
return _create_wrapper |
| 84 |
|
| 85 |
|
| 86 |
def mock_good_connection(): |
| 87 |
token_response = { |
| 88 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 89 |
'{"access_token": "alongtoken"}' |
| 90 |
), |
| 91 |
} |
| 92 |
return patch( |
| 93 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 94 |
side_effect=build_mocked_request(count(), token_response), |
| 95 |
autospec=True, |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
class TestKeycloakRealm(ModuleTestCase): |
| 100 |
def setUp(self): |
| 101 |
super().setUp() |
| 102 |
self.module = keycloak_client |
| 103 |
|
| 104 |
def test_authentication_flow_binding_overrides_feature(self): |
| 105 |
"""Add a new realm""" |
| 106 |
|
| 107 |
module_args = { |
| 108 |
"auth_keycloak_url": "https: // auth.example.com / auth", |
| 109 |
"token": "{{ access_token }}", |
| 110 |
"state": "present", |
| 111 |
"realm": "master", |
| 112 |
"client_id": "test", |
| 113 |
"authentication_flow_binding_overrides": {"browser": "4c90336b-bf1d-4b87-916d-3677ba4e5fbb"}, |
| 114 |
} |
| 115 |
return_value_get_client_by_clientid = [ |
| 116 |
None, |
| 117 |
{ |
| 118 |
"authenticationFlowBindingOverrides": {"browser": "f9502b6d-d76a-4efe-8331-2ddd853c9f9c"}, |
| 119 |
"clientId": "onboardingid", |
| 120 |
"enabled": "true", |
| 121 |
"protocol": "openid-connect", |
| 122 |
"redirectUris": ["*"], |
| 123 |
}, |
| 124 |
] |
| 125 |
changed = True |
| 126 |
|
| 127 |
# Run the module |
| 128 |
|
| 129 |
with set_module_args(module_args): |
| 130 |
with mock_good_connection(): |
| 131 |
with patch_keycloak_api(get_client_by_clientid=return_value_get_client_by_clientid) as ( |
| 132 |
mock_get_client_by_clientid, |
| 133 |
mock_get_client_by_id, |
| 134 |
mock_create_client, |
| 135 |
mock_update_client, |
| 136 |
mock_delete_client, |
| 137 |
): |
| 138 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 139 |
self.module.main() |
| 140 |
|
| 141 |
self.assertEqual(mock_get_client_by_clientid.call_count, 2) |
| 142 |
self.assertEqual(mock_get_client_by_id.call_count, 0) |
| 143 |
self.assertEqual(mock_create_client.call_count, 1) |
| 144 |
self.assertEqual(mock_update_client.call_count, 0) |
| 145 |
self.assertEqual(mock_delete_client.call_count, 0) |
| 146 |
|
| 147 |
# Verify that the module's changed status matches what is expected |
| 148 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |