| 1 |
# Copyright (c) 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 io import StringIO |
| 8 |
from itertools import count |
| 9 |
from urllib.error import HTTPError |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible_collections.community.general.plugins.module_utils._keycloak import ( |
| 14 |
KeycloakError, |
| 15 |
get_token, |
| 16 |
) |
| 17 |
|
| 18 |
module_params_creds = { |
| 19 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 20 |
"validate_certs": True, |
| 21 |
"auth_realm": "master", |
| 22 |
"client_id": "admin-cli", |
| 23 |
"auth_username": "admin", |
| 24 |
"auth_password": "admin", |
| 25 |
"client_secret": None, |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
def build_mocked_request(get_id_user_count, response_dict): |
| 30 |
def _mocked_requests(*args, **kwargs): |
| 31 |
url = args[0] |
| 32 |
method = kwargs["method"] |
| 33 |
future_response = response_dict.get(url, None) |
| 34 |
return get_response(future_response, method, get_id_user_count) |
| 35 |
|
| 36 |
return _mocked_requests |
| 37 |
|
| 38 |
|
| 39 |
def get_response(object_with_future_response, method, get_id_call_count): |
| 40 |
if callable(object_with_future_response): |
| 41 |
return object_with_future_response() |
| 42 |
if isinstance(object_with_future_response, dict): |
| 43 |
return get_response(object_with_future_response[method], method, get_id_call_count) |
| 44 |
if isinstance(object_with_future_response, list): |
| 45 |
try: |
| 46 |
call_number = get_id_call_count.__next__() |
| 47 |
except AttributeError: |
| 48 |
# manage python 2 versions. |
| 49 |
call_number = get_id_call_count.next() |
| 50 |
return get_response(object_with_future_response[call_number], method, get_id_call_count) |
| 51 |
return object_with_future_response |
| 52 |
|
| 53 |
|
| 54 |
def create_wrapper(text_as_string): |
| 55 |
"""Allow to mock many times a call to one address. |
| 56 |
Without this function, the StringIO is empty for the second call. |
| 57 |
""" |
| 58 |
|
| 59 |
def _create_wrapper(): |
| 60 |
return StringIO(text_as_string) |
| 61 |
|
| 62 |
return _create_wrapper |
| 63 |
|
| 64 |
|
| 65 |
@pytest.fixture() |
| 66 |
def mock_good_connection(mocker): |
| 67 |
token_response = { |
| 68 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 69 |
'{"access_token": "alongtoken"}' |
| 70 |
), |
| 71 |
} |
| 72 |
return mocker.patch( |
| 73 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 74 |
side_effect=build_mocked_request(count(), token_response), |
| 75 |
autospec=True, |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
def test_connect_to_keycloak_with_creds(mock_good_connection): |
| 80 |
keycloak_header = get_token(module_params_creds) |
| 81 |
assert keycloak_header == {"Authorization": "Bearer alongtoken", "Content-Type": "application/json"} |
| 82 |
|
| 83 |
|
| 84 |
def test_connect_to_keycloak_with_token(mock_good_connection): |
| 85 |
module_params_token = { |
| 86 |
"auth_keycloak_url": "http://keycloak.url/auth", |
| 87 |
"validate_certs": True, |
| 88 |
"client_id": "admin-cli", |
| 89 |
"token": "alongtoken", |
| 90 |
} |
| 91 |
keycloak_header = get_token(module_params_token) |
| 92 |
assert keycloak_header == {"Authorization": "Bearer alongtoken", "Content-Type": "application/json"} |
| 93 |
|
| 94 |
|
| 95 |
@pytest.fixture() |
| 96 |
def mock_bad_json_returned(mocker): |
| 97 |
token_response = { |
| 98 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper('{"access_token":'), |
| 99 |
} |
| 100 |
return mocker.patch( |
| 101 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 102 |
side_effect=build_mocked_request(count(), token_response), |
| 103 |
autospec=True, |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def test_bad_json_returned(mock_bad_json_returned): |
| 108 |
with pytest.raises(KeycloakError) as raised_error: |
| 109 |
get_token(module_params_creds) |
| 110 |
# cannot check all the message, different errors message for the value |
| 111 |
# error in python 2.6, 2.7 and 3.*. |
| 112 |
assert ( |
| 113 |
"API returned invalid JSON when trying to obtain access token from " |
| 114 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token: " |
| 115 |
) in str(raised_error.value) |
| 116 |
|
| 117 |
|
| 118 |
def raise_401(url): |
| 119 |
def _raise_401(): |
| 120 |
raise HTTPError(url=url, code=401, msg="Unauthorized", hdrs="", fp=StringIO("")) |
| 121 |
|
| 122 |
return _raise_401 |
| 123 |
|
| 124 |
|
| 125 |
@pytest.fixture() |
| 126 |
def mock_401_returned(mocker): |
| 127 |
token_response = { |
| 128 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": raise_401( |
| 129 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token" |
| 130 |
), |
| 131 |
} |
| 132 |
return mocker.patch( |
| 133 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 134 |
side_effect=build_mocked_request(count(), token_response), |
| 135 |
autospec=True, |
| 136 |
) |
| 137 |
|
| 138 |
|
| 139 |
def test_error_returned(mock_401_returned): |
| 140 |
with pytest.raises(KeycloakError) as raised_error: |
| 141 |
get_token(module_params_creds) |
| 142 |
assert str(raised_error.value) == ( |
| 143 |
"Could not obtain access token from http://keycloak.url" |
| 144 |
"/auth/realms/master/protocol/openid-connect/token: " |
| 145 |
"HTTP Error 401: Unauthorized" |
| 146 |
) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.fixture() |
| 150 |
def mock_json_without_token_returned(mocker): |
| 151 |
token_response = { |
| 152 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token": create_wrapper( |
| 153 |
'{"not_token": "It is not a token"}' |
| 154 |
), |
| 155 |
} |
| 156 |
return mocker.patch( |
| 157 |
"ansible_collections.community.general.plugins.module_utils._keycloak.open_url", |
| 158 |
side_effect=build_mocked_request(count(), token_response), |
| 159 |
autospec=True, |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
def test_json_without_token_returned(mock_json_without_token_returned): |
| 164 |
with pytest.raises(KeycloakError) as raised_error: |
| 165 |
get_token(module_params_creds) |
| 166 |
assert str(raised_error.value) == ( |
| 167 |
"API did not include access_token field in response from " |
| 168 |
"http://keycloak.url/auth/realms/master/protocol/openid-connect/token" |
| 169 |
) |