| 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_clientscope |
| 19 |
|
| 20 |
|
| 21 |
@contextmanager |
| 22 |
def patch_keycloak_api( |
| 23 |
get_clientscope_by_name=None, |
| 24 |
get_clientscope_by_clientscopeid=None, |
| 25 |
create_clientscope=None, |
| 26 |
update_clientscope=None, |
| 27 |
get_clientscope_protocolmapper_by_name=None, |
| 28 |
update_clientscope_protocolmappers=None, |
| 29 |
create_clientscope_protocolmapper=None, |
| 30 |
delete_clientscope=None, |
| 31 |
): |
| 32 |
"""Mock context manager for patching the methods in PwPolicyIPAClient that contact the IPA server |
| 33 |
|
| 34 |
Patches the `login` and `_post_json` methods |
| 35 |
|
| 36 |
Keyword arguments are passed to the mock object that patches `_post_json` |
| 37 |
|
| 38 |
No arguments are passed to the mock object that patches `login` because no tests require it |
| 39 |
|
| 40 |
Example:: |
| 41 |
|
| 42 |
with patch_ipa(return_value={}) as (mock_login, mock_post): |
| 43 |
... |
| 44 |
""" |
| 45 |
|
| 46 |
""" |
| 47 |
get_clientscope_by_clientscopeid |
| 48 |
delete_clientscope |
| 49 |
""" |
| 50 |
|
| 51 |
obj = keycloak_clientscope.KeycloakAPI |
| 52 |
with patch.object( |
| 53 |
obj, "get_clientscope_by_name", side_effect=get_clientscope_by_name |
| 54 |
) as mock_get_clientscope_by_name: |
| 55 |
with patch.object( |
| 56 |
obj, "get_clientscope_by_clientscopeid", side_effect=get_clientscope_by_clientscopeid |
| 57 |
) as mock_get_clientscope_by_clientscopeid: |
| 58 |
with patch.object(obj, "create_clientscope", side_effect=create_clientscope) as mock_create_clientscope: |
| 59 |
with patch.object( |
| 60 |
obj, "update_clientscope", return_value=update_clientscope |
| 61 |
) as mock_update_clientscope: |
| 62 |
with patch.object( |
| 63 |
obj, |
| 64 |
"get_clientscope_protocolmapper_by_name", |
| 65 |
side_effect=get_clientscope_protocolmapper_by_name, |
| 66 |
) as mock_get_clientscope_protocolmapper_by_name: |
| 67 |
with patch.object( |
| 68 |
obj, "update_clientscope_protocolmappers", side_effect=update_clientscope_protocolmappers |
| 69 |
) as mock_update_clientscope_protocolmappers: |
| 70 |
with patch.object( |
| 71 |
obj, "create_clientscope_protocolmapper", side_effect=create_clientscope_protocolmapper |
| 72 |
) as mock_create_clientscope_protocolmapper: |
| 73 |
with patch.object( |
| 74 |
obj, "delete_clientscope", side_effect=delete_clientscope |
| 75 |
) as mock_delete_clientscope: |
| 76 |
yield ( |
| 77 |
mock_get_clientscope_by_name, |
| 78 |
mock_get_clientscope_by_clientscopeid, |
| 79 |
mock_create_clientscope, |
| 80 |
mock_update_clientscope, |
| 81 |
mock_get_clientscope_protocolmapper_by_name, |
| 82 |
mock_update_clientscope_protocolmappers, |
| 83 |
mock_create_clientscope_protocolmapper, |
| 84 |
mock_delete_clientscope, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def get_response(object_with_future_response, method, get_id_call_count): |
| 89 |
if callable(object_with_future_response): |
| 90 |
return object_with_future_response() |
| 91 |
if isinstance(object_with_future_response, dict): |
| 92 |
return get_response(object_with_future_response[method], method, get_id_call_count) |
| 93 |
if isinstance(object_with_future_response, list): |
| 94 |
call_number = next(get_id_call_count) |
| 95 |
return get_response(object_with_future_response[call_number], method, get_id_call_count) |
| 96 |
return object_with_future_response |
| 97 |
|
| 98 |
|
| 99 |
def build_mocked_request(get_id_user_count, response_dict): |
| 100 |
def _mocked_requests(*args, **kwargs): |
| 101 |
url = args[0] |
| 102 |
method = kwargs["method"] |
| 103 |
future_response = response_dict.get(url, None) |
| 104 |
return get_response(future_response, method, get_id_user_count) |
| 105 |
|
| 106 |
return _mocked_requests |
| 107 |
|
| 108 |
|
| 109 |
def create_wrapper(text_as_string): |
| 110 |
"""Allow to mock many times a call to one address. |
| 111 |
Without this function, the StringIO is empty for the second call. |
| 112 |
""" |
| 113 |
|
| 114 |
def _create_wrapper(): |
| 115 |
return StringIO(text_as_string) |
| 116 |
|
| 117 |
return _create_wrapper |
| 118 |
|
| 119 |
|
| 120 |
def mock_good_connection(): |
| 121 |
token_response = { |
| 122 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 123 |
'{"access_token": "alongtoken"}' |
| 124 |
), |
| 125 |
} |
| 126 |
return patch( |
| 127 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 128 |
side_effect=build_mocked_request(count(), token_response), |
| 129 |
autospec=True, |
| 130 |
) |
| 131 |
|
| 132 |
|
| 133 |
class TestKeycloakAuthentication(ModuleTestCase): |
| 134 |
def setUp(self): |
| 135 |
super().setUp() |
| 136 |
self.module = keycloak_clientscope |
| 137 |
|
| 138 |
def test_create_clientscope(self): |
| 139 |
"""Add a new authentication flow from copy of an other flow""" |
| 140 |
|
| 141 |
module_args = { |
| 142 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 143 |
"auth_username": "admin", |
| 144 |
"auth_password": "admin", |
| 145 |
"auth_realm": "master", |
| 146 |
"realm": "realm-name", |
| 147 |
"state": "present", |
| 148 |
"name": "my-new-kc-clientscope", |
| 149 |
} |
| 150 |
return_value_get_clientscope_by_name = [ |
| 151 |
None, |
| 152 |
{"attributes": {}, "id": "73fec1d2-f032-410c-8177-583104d01305", "name": "my-new-kc-clientscope"}, |
| 153 |
] |
| 154 |
|
| 155 |
changed = True |
| 156 |
|
| 157 |
# Run the module |
| 158 |
|
| 159 |
with set_module_args(module_args): |
| 160 |
with mock_good_connection(): |
| 161 |
with patch_keycloak_api(get_clientscope_by_name=return_value_get_clientscope_by_name) as ( |
| 162 |
mock_get_clientscope_by_name, |
| 163 |
mock_get_clientscope_by_clientscopeid, |
| 164 |
mock_create_clientscope, |
| 165 |
mock_update_clientscope, |
| 166 |
mock_get_clientscope_protocolmapper_by_name, |
| 167 |
mock_update_clientscope_protocolmappers, |
| 168 |
mock_create_clientscope_protocolmapper, |
| 169 |
mock_delete_clientscope, |
| 170 |
): |
| 171 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 172 |
self.module.main() |
| 173 |
|
| 174 |
# Verify number of call on each mock |
| 175 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 2) |
| 176 |
self.assertEqual(mock_create_clientscope.call_count, 1) |
| 177 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 0) |
| 178 |
self.assertEqual(mock_update_clientscope.call_count, 0) |
| 179 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 0) |
| 180 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 0) |
| 181 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 182 |
self.assertEqual(mock_delete_clientscope.call_count, 0) |
| 183 |
|
| 184 |
# Verify that the module's changed status matches what is expected |
| 185 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 186 |
|
| 187 |
def test_create_clientscope_idempotency(self): |
| 188 |
"""Add a new authentication flow from copy of an other flow""" |
| 189 |
|
| 190 |
module_args = { |
| 191 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 192 |
"auth_username": "admin", |
| 193 |
"auth_password": "admin", |
| 194 |
"auth_realm": "master", |
| 195 |
"realm": "realm-name", |
| 196 |
"state": "present", |
| 197 |
"name": "my-new-kc-clientscope", |
| 198 |
} |
| 199 |
return_value_get_clientscope_by_name = [ |
| 200 |
{"attributes": {}, "id": "73fec1d2-f032-410c-8177-583104d01305", "name": "my-new-kc-clientscope"} |
| 201 |
] |
| 202 |
|
| 203 |
changed = False |
| 204 |
|
| 205 |
# Run the module |
| 206 |
|
| 207 |
with set_module_args(module_args): |
| 208 |
with mock_good_connection(): |
| 209 |
with patch_keycloak_api(get_clientscope_by_name=return_value_get_clientscope_by_name) as ( |
| 210 |
mock_get_clientscope_by_name, |
| 211 |
mock_get_clientscope_by_clientscopeid, |
| 212 |
mock_create_clientscope, |
| 213 |
mock_update_clientscope, |
| 214 |
mock_get_clientscope_protocolmapper_by_name, |
| 215 |
mock_update_clientscope_protocolmappers, |
| 216 |
mock_create_clientscope_protocolmapper, |
| 217 |
mock_delete_clientscope, |
| 218 |
): |
| 219 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 220 |
self.module.main() |
| 221 |
|
| 222 |
# Verify number of call on each mock |
| 223 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 1) |
| 224 |
self.assertEqual(mock_create_clientscope.call_count, 0) |
| 225 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 0) |
| 226 |
self.assertEqual(mock_update_clientscope.call_count, 0) |
| 227 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 0) |
| 228 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 0) |
| 229 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 230 |
self.assertEqual(mock_delete_clientscope.call_count, 0) |
| 231 |
|
| 232 |
# Verify that the module's changed status matches what is expected |
| 233 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 234 |
|
| 235 |
def test_delete_clientscope(self): |
| 236 |
"""Add a new authentication flow from copy of an other flow""" |
| 237 |
|
| 238 |
module_args = { |
| 239 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 240 |
"auth_username": "admin", |
| 241 |
"auth_password": "admin", |
| 242 |
"auth_realm": "master", |
| 243 |
"realm": "realm-name", |
| 244 |
"state": "absent", |
| 245 |
"name": "my-new-kc-clientscope", |
| 246 |
} |
| 247 |
return_value_get_clientscope_by_name = [ |
| 248 |
{"attributes": {}, "id": "73fec1d2-f032-410c-8177-583104d01305", "name": "my-new-kc-clientscope"} |
| 249 |
] |
| 250 |
|
| 251 |
changed = True |
| 252 |
|
| 253 |
# Run the module |
| 254 |
|
| 255 |
with set_module_args(module_args): |
| 256 |
with mock_good_connection(): |
| 257 |
with patch_keycloak_api(get_clientscope_by_name=return_value_get_clientscope_by_name) as ( |
| 258 |
mock_get_clientscope_by_name, |
| 259 |
mock_get_clientscope_by_clientscopeid, |
| 260 |
mock_create_clientscope, |
| 261 |
mock_update_clientscope, |
| 262 |
mock_get_clientscope_protocolmapper_by_name, |
| 263 |
mock_update_clientscope_protocolmappers, |
| 264 |
mock_create_clientscope_protocolmapper, |
| 265 |
mock_delete_clientscope, |
| 266 |
): |
| 267 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 268 |
self.module.main() |
| 269 |
|
| 270 |
# Verify number of call on each mock |
| 271 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 1) |
| 272 |
self.assertEqual(mock_create_clientscope.call_count, 0) |
| 273 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 0) |
| 274 |
self.assertEqual(mock_update_clientscope.call_count, 0) |
| 275 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 0) |
| 276 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 0) |
| 277 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 278 |
self.assertEqual(mock_delete_clientscope.call_count, 1) |
| 279 |
|
| 280 |
# Verify that the module's changed status matches what is expected |
| 281 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 282 |
|
| 283 |
def test_delete_clientscope_idempotency(self): |
| 284 |
"""Add a new authentication flow from copy of an other flow""" |
| 285 |
|
| 286 |
module_args = { |
| 287 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 288 |
"auth_username": "admin", |
| 289 |
"auth_password": "admin", |
| 290 |
"auth_realm": "master", |
| 291 |
"realm": "realm-name", |
| 292 |
"state": "absent", |
| 293 |
"name": "my-new-kc-clientscope", |
| 294 |
} |
| 295 |
return_value_get_clientscope_by_name = [None] |
| 296 |
|
| 297 |
changed = False |
| 298 |
|
| 299 |
# Run the module |
| 300 |
|
| 301 |
with set_module_args(module_args): |
| 302 |
with mock_good_connection(): |
| 303 |
with patch_keycloak_api(get_clientscope_by_name=return_value_get_clientscope_by_name) as ( |
| 304 |
mock_get_clientscope_by_name, |
| 305 |
mock_get_clientscope_by_clientscopeid, |
| 306 |
mock_create_clientscope, |
| 307 |
mock_update_clientscope, |
| 308 |
mock_get_clientscope_protocolmapper_by_name, |
| 309 |
mock_update_clientscope_protocolmappers, |
| 310 |
mock_create_clientscope_protocolmapper, |
| 311 |
mock_delete_clientscope, |
| 312 |
): |
| 313 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 314 |
self.module.main() |
| 315 |
|
| 316 |
# Verify number of call on each mock |
| 317 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 1) |
| 318 |
self.assertEqual(mock_create_clientscope.call_count, 0) |
| 319 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 0) |
| 320 |
self.assertEqual(mock_update_clientscope.call_count, 0) |
| 321 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 0) |
| 322 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 0) |
| 323 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 324 |
self.assertEqual(mock_delete_clientscope.call_count, 0) |
| 325 |
|
| 326 |
# Verify that the module's changed status matches what is expected |
| 327 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 328 |
|
| 329 |
def test_create_clientscope_with_protocolmappers(self): |
| 330 |
"""Add a new authentication flow from copy of an other flow""" |
| 331 |
|
| 332 |
module_args = { |
| 333 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 334 |
"auth_username": "admin", |
| 335 |
"auth_password": "admin", |
| 336 |
"auth_realm": "master", |
| 337 |
"realm": "realm-name", |
| 338 |
"state": "present", |
| 339 |
"name": "my-new-kc-clientscope", |
| 340 |
"protocolMappers": [ |
| 341 |
{ |
| 342 |
"protocol": "openid-connect", |
| 343 |
"config": { |
| 344 |
"full.path": "true", |
| 345 |
"id.token.claim": "true", |
| 346 |
"access.token.claim": "true", |
| 347 |
"userinfo.token.claim": "true", |
| 348 |
"claim.name": "protocol1", |
| 349 |
}, |
| 350 |
"name": "protocol1", |
| 351 |
"protocolMapper": "oidc-group-membership-mapper", |
| 352 |
}, |
| 353 |
{ |
| 354 |
"protocol": "openid-connect", |
| 355 |
"config": { |
| 356 |
"full.path": "false", |
| 357 |
"id.token.claim": "false", |
| 358 |
"access.token.claim": "false", |
| 359 |
"userinfo.token.claim": "false", |
| 360 |
"claim.name": "protocol2", |
| 361 |
}, |
| 362 |
"name": "protocol2", |
| 363 |
"protocolMapper": "oidc-group-membership-mapper", |
| 364 |
}, |
| 365 |
{ |
| 366 |
"protocol": "openid-connect", |
| 367 |
"config": { |
| 368 |
"full.path": "true", |
| 369 |
"id.token.claim": "false", |
| 370 |
"access.token.claim": "true", |
| 371 |
"userinfo.token.claim": "false", |
| 372 |
"claim.name": "protocol3", |
| 373 |
}, |
| 374 |
"name": "protocol3", |
| 375 |
"protocolMapper": "oidc-group-membership-mapper", |
| 376 |
}, |
| 377 |
], |
| 378 |
} |
| 379 |
return_value_get_clientscope_by_name = [ |
| 380 |
None, |
| 381 |
{ |
| 382 |
"attributes": {}, |
| 383 |
"id": "890ec72e-fe1d-4308-9f27-485ef7eaa182", |
| 384 |
"name": "my-new-kc-clientscope", |
| 385 |
"protocolMappers": [ |
| 386 |
{ |
| 387 |
"config": { |
| 388 |
"access.token.claim": "false", |
| 389 |
"claim.name": "protocol2", |
| 390 |
"full.path": "false", |
| 391 |
"id.token.claim": "false", |
| 392 |
"userinfo.token.claim": "false", |
| 393 |
}, |
| 394 |
"consentRequired": "false", |
| 395 |
"id": "a7f19adb-cc58-41b1-94ce-782dc255139b", |
| 396 |
"name": "protocol2", |
| 397 |
"protocol": "openid-connect", |
| 398 |
"protocolMapper": "oidc-group-membership-mapper", |
| 399 |
}, |
| 400 |
{ |
| 401 |
"config": { |
| 402 |
"access.token.claim": "true", |
| 403 |
"claim.name": "protocol3", |
| 404 |
"full.path": "true", |
| 405 |
"id.token.claim": "false", |
| 406 |
"userinfo.token.claim": "false", |
| 407 |
}, |
| 408 |
"consentRequired": "false", |
| 409 |
"id": "2103a559-185a-40f4-84ae-9ab311d5b812", |
| 410 |
"name": "protocol3", |
| 411 |
"protocol": "openid-connect", |
| 412 |
"protocolMapper": "oidc-group-membership-mapper", |
| 413 |
}, |
| 414 |
{ |
| 415 |
"config": { |
| 416 |
"access.token.claim": "true", |
| 417 |
"claim.name": "protocol1", |
| 418 |
"full.path": "true", |
| 419 |
"id.token.claim": "true", |
| 420 |
"userinfo.token.claim": "true", |
| 421 |
}, |
| 422 |
"consentRequired": "false", |
| 423 |
"id": "bbf6390f-e95f-4c20-882b-9dad328363b9", |
| 424 |
"name": "protocol1", |
| 425 |
"protocol": "openid-connect", |
| 426 |
"protocolMapper": "oidc-group-membership-mapper", |
| 427 |
}, |
| 428 |
], |
| 429 |
}, |
| 430 |
] |
| 431 |
|
| 432 |
changed = True |
| 433 |
|
| 434 |
# Run the module |
| 435 |
|
| 436 |
with set_module_args(module_args): |
| 437 |
with mock_good_connection(): |
| 438 |
with patch_keycloak_api(get_clientscope_by_name=return_value_get_clientscope_by_name) as ( |
| 439 |
mock_get_clientscope_by_name, |
| 440 |
mock_get_clientscope_by_clientscopeid, |
| 441 |
mock_create_clientscope, |
| 442 |
mock_update_clientscope, |
| 443 |
mock_get_clientscope_protocolmapper_by_name, |
| 444 |
mock_update_clientscope_protocolmappers, |
| 445 |
mock_create_clientscope_protocolmapper, |
| 446 |
mock_delete_clientscope, |
| 447 |
): |
| 448 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 449 |
self.module.main() |
| 450 |
|
| 451 |
# Verify number of call on each mock |
| 452 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 2) |
| 453 |
self.assertEqual(mock_create_clientscope.call_count, 1) |
| 454 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 0) |
| 455 |
self.assertEqual(mock_update_clientscope.call_count, 0) |
| 456 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 0) |
| 457 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 0) |
| 458 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 459 |
self.assertEqual(mock_delete_clientscope.call_count, 0) |
| 460 |
|
| 461 |
# Verify that the module's changed status matches what is expected |
| 462 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |
| 463 |
|
| 464 |
def test_update_clientscope_with_protocolmappers(self): |
| 465 |
"""Add a new authentication flow from copy of an other flow""" |
| 466 |
|
| 467 |
module_args = { |
| 468 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 469 |
"auth_username": "admin", |
| 470 |
"auth_password": "admin", |
| 471 |
"auth_realm": "master", |
| 472 |
"realm": "realm-name", |
| 473 |
"state": "present", |
| 474 |
"name": "my-new-kc-clientscope", |
| 475 |
"protocolMappers": [ |
| 476 |
{ |
| 477 |
"protocol": "openid-connect", |
| 478 |
"config": { |
| 479 |
"full.path": "false", |
| 480 |
"id.token.claim": "false", |
| 481 |
"access.token.claim": "false", |
| 482 |
"userinfo.token.claim": "false", |
| 483 |
"claim.name": "protocol1_updated", |
| 484 |
}, |
| 485 |
"name": "protocol1", |
| 486 |
"protocolMapper": "oidc-group-membership-mapper", |
| 487 |
}, |
| 488 |
{ |
| 489 |
"protocol": "openid-connect", |
| 490 |
"config": { |
| 491 |
"full.path": "true", |
| 492 |
"id.token.claim": "false", |
| 493 |
"access.token.claim": "false", |
| 494 |
"userinfo.token.claim": "false", |
| 495 |
"claim.name": "protocol2_updated", |
| 496 |
}, |
| 497 |
"name": "protocol2", |
| 498 |
"protocolMapper": "oidc-group-membership-mapper", |
| 499 |
}, |
| 500 |
{ |
| 501 |
"protocol": "openid-connect", |
| 502 |
"config": { |
| 503 |
"full.path": "true", |
| 504 |
"id.token.claim": "true", |
| 505 |
"access.token.claim": "true", |
| 506 |
"userinfo.token.claim": "true", |
| 507 |
"claim.name": "protocol3_updated", |
| 508 |
}, |
| 509 |
"name": "protocol3", |
| 510 |
"protocolMapper": "oidc-group-membership-mapper", |
| 511 |
}, |
| 512 |
], |
| 513 |
} |
| 514 |
return_value_get_clientscope_by_name = [ |
| 515 |
{ |
| 516 |
"attributes": {}, |
| 517 |
"id": "890ec72e-fe1d-4308-9f27-485ef7eaa182", |
| 518 |
"name": "my-new-kc-clientscope", |
| 519 |
"protocolMappers": [ |
| 520 |
{ |
| 521 |
"config": { |
| 522 |
"access.token.claim": "true", |
| 523 |
"claim.name": "groups", |
| 524 |
"full.path": "true", |
| 525 |
"id.token.claim": "true", |
| 526 |
"userinfo.token.claim": "true", |
| 527 |
}, |
| 528 |
"consentRequired": "false", |
| 529 |
"id": "e077007a-367a-444f-91ef-70277a1d868d", |
| 530 |
"name": "groups", |
| 531 |
"protocol": "saml", |
| 532 |
"protocolMapper": "oidc-group-membership-mapper", |
| 533 |
}, |
| 534 |
{ |
| 535 |
"config": { |
| 536 |
"access.token.claim": "true", |
| 537 |
"claim.name": "groups", |
| 538 |
"full.path": "true", |
| 539 |
"id.token.claim": "true", |
| 540 |
"userinfo.token.claim": "true", |
| 541 |
}, |
| 542 |
"consentRequired": "false", |
| 543 |
"id": "06c518aa-c627-43cc-9a82-d8467b508d34", |
| 544 |
"name": "groups", |
| 545 |
"protocol": "openid-connect", |
| 546 |
"protocolMapper": "oidc-group-membership-mapper", |
| 547 |
}, |
| 548 |
{ |
| 549 |
"config": { |
| 550 |
"access.token.claim": "true", |
| 551 |
"claim.name": "groups", |
| 552 |
"full.path": "true", |
| 553 |
"id.token.claim": "true", |
| 554 |
"userinfo.token.claim": "true", |
| 555 |
}, |
| 556 |
"consentRequired": "false", |
| 557 |
"id": "1d03c557-d97e-40f4-ac35-6cecd74ea70d", |
| 558 |
"name": "groups", |
| 559 |
"protocol": "wsfed", |
| 560 |
"protocolMapper": "oidc-group-membership-mapper", |
| 561 |
}, |
| 562 |
], |
| 563 |
} |
| 564 |
] |
| 565 |
return_value_get_clientscope_by_clientscopeid = [ |
| 566 |
{ |
| 567 |
"attributes": {}, |
| 568 |
"id": "2286032f-451e-44d5-8be6-e45aac7983a1", |
| 569 |
"name": "my-new-kc-clientscope", |
| 570 |
"protocolMappers": [ |
| 571 |
{ |
| 572 |
"config": { |
| 573 |
"access.token.claim": "true", |
| 574 |
"claim.name": "protocol1_updated", |
| 575 |
"full.path": "true", |
| 576 |
"id.token.claim": "false", |
| 577 |
"userinfo.token.claim": "false", |
| 578 |
}, |
| 579 |
"consentRequired": "false", |
| 580 |
"id": "a7f19adb-cc58-41b1-94ce-782dc255139b", |
| 581 |
"name": "protocol2", |
| 582 |
"protocol": "openid-connect", |
| 583 |
"protocolMapper": "oidc-group-membership-mapper", |
| 584 |
}, |
| 585 |
{ |
| 586 |
"config": { |
| 587 |
"access.token.claim": "true", |
| 588 |
"claim.name": "protocol1_updated", |
| 589 |
"full.path": "true", |
| 590 |
"id.token.claim": "false", |
| 591 |
"userinfo.token.claim": "false", |
| 592 |
}, |
| 593 |
"consentRequired": "false", |
| 594 |
"id": "2103a559-185a-40f4-84ae-9ab311d5b812", |
| 595 |
"name": "protocol3", |
| 596 |
"protocol": "openid-connect", |
| 597 |
"protocolMapper": "oidc-group-membership-mapper", |
| 598 |
}, |
| 599 |
{ |
| 600 |
"config": { |
| 601 |
"access.token.claim": "false", |
| 602 |
"claim.name": "protocol1_updated", |
| 603 |
"full.path": "false", |
| 604 |
"id.token.claim": "false", |
| 605 |
"userinfo.token.claim": "false", |
| 606 |
}, |
| 607 |
"consentRequired": "false", |
| 608 |
"id": "bbf6390f-e95f-4c20-882b-9dad328363b9", |
| 609 |
"name": "protocol1", |
| 610 |
"protocol": "openid-connect", |
| 611 |
"protocolMapper": "oidc-group-membership-mapper", |
| 612 |
}, |
| 613 |
], |
| 614 |
} |
| 615 |
] |
| 616 |
|
| 617 |
changed = True |
| 618 |
|
| 619 |
# Run the module |
| 620 |
|
| 621 |
with set_module_args(module_args): |
| 622 |
with mock_good_connection(): |
| 623 |
with patch_keycloak_api( |
| 624 |
get_clientscope_by_name=return_value_get_clientscope_by_name, |
| 625 |
get_clientscope_by_clientscopeid=return_value_get_clientscope_by_clientscopeid, |
| 626 |
) as ( |
| 627 |
mock_get_clientscope_by_name, |
| 628 |
mock_get_clientscope_by_clientscopeid, |
| 629 |
mock_create_clientscope, |
| 630 |
mock_update_clientscope, |
| 631 |
mock_get_clientscope_protocolmapper_by_name, |
| 632 |
mock_update_clientscope_protocolmappers, |
| 633 |
mock_create_clientscope_protocolmapper, |
| 634 |
mock_delete_clientscope, |
| 635 |
): |
| 636 |
with self.assertRaises(AnsibleExitJson) as exec_info: |
| 637 |
self.module.main() |
| 638 |
|
| 639 |
# Verify number of call on each mock |
| 640 |
self.assertEqual(mock_get_clientscope_by_name.call_count, 1) |
| 641 |
self.assertEqual(mock_create_clientscope.call_count, 0) |
| 642 |
self.assertEqual(mock_get_clientscope_by_clientscopeid.call_count, 1) |
| 643 |
self.assertEqual(mock_update_clientscope.call_count, 1) |
| 644 |
self.assertEqual(mock_get_clientscope_protocolmapper_by_name.call_count, 3) |
| 645 |
self.assertEqual(mock_update_clientscope_protocolmappers.call_count, 3) |
| 646 |
self.assertEqual(mock_create_clientscope_protocolmapper.call_count, 0) |
| 647 |
self.assertEqual(mock_delete_clientscope.call_count, 0) |
| 648 |
|
| 649 |
# Verify that the module's changed status matches what is expected |
| 650 |
self.assertIs(exec_info.exception.args[0]["changed"], changed) |