| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
from __future__ import (absolute_import, division, print_function) |
| 12 |
__metaclass__ = type |
| 13 |
|
| 14 |
import pytest |
| 15 |
import json |
| 16 |
from ansible_collections.dellemc.openmanage.plugins.modules import idrac_user_info |
| 17 |
from ansible.module_utils.six.moves.urllib.error import HTTPError, URLError |
| 18 |
from ansible.module_utils.urls import ConnectionError, SSLValidationError |
| 19 |
from ansible_collections.dellemc.openmanage.tests.unit.plugins.modules.common import FakeAnsibleModule |
| 20 |
from unittest.mock import MagicMock |
| 21 |
from ansible.module_utils._text import to_text |
| 22 |
from io import StringIO |
| 23 |
|
| 24 |
MODULE_PATH = 'ansible_collections.dellemc.openmanage.plugins.modules.' |
| 25 |
HTTPS_ADDRESS = 'https://testhost.com' |
| 26 |
|
| 27 |
|
| 28 |
class TestIDRACUserInfo(FakeAnsibleModule): |
| 29 |
module = idrac_user_info |
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def idrac_user_info_mock(self): |
| 33 |
idrac_obj = MagicMock() |
| 34 |
return idrac_obj |
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def idrac_connection_user_info_mock(self, mocker, idrac_user_info_mock): |
| 38 |
idrac_conn_mock = mocker.patch(MODULE_PATH + 'idrac_user_info.iDRACRedfishAPI', |
| 39 |
return_value=idrac_user_info_mock) |
| 40 |
idrac_conn_mock.return_value.__enter__.return_value = idrac_user_info_mock |
| 41 |
return idrac_conn_mock |
| 42 |
|
| 43 |
def test_fetch_all_accounts_success_case(self, idrac_default_args, idrac_connection_user_info_mock, |
| 44 |
idrac_user_info_mock, mocker): |
| 45 |
obj = MagicMock() |
| 46 |
obj.json_data = {"Members": [ |
| 47 |
{"UserName": "test", "Oem": {"Dell": "test"}}]} |
| 48 |
mocker.patch(MODULE_PATH + "idrac_user_info.iDRACRedfishAPI.invoke_request", |
| 49 |
return_value=(obj)) |
| 50 |
resp = self.module.fetch_all_accounts(idrac_connection_user_info_mock, "/acounts/accdetails") |
| 51 |
assert resp[0].get("UserName") == "test" |
| 52 |
|
| 53 |
def test_get_user_id_accounts(self, idrac_default_args, idrac_connection_user_info_mock, |
| 54 |
idrac_user_info_mock, mocker): |
| 55 |
json_str = to_text(json.dumps({"data": "out"})) |
| 56 |
idrac_default_args.update({"username": "test"}) |
| 57 |
obj = MagicMock() |
| 58 |
obj.json_data = {"UserName": "test"} |
| 59 |
f_module = self.get_module_mock( |
| 60 |
params=idrac_default_args, check_mode=False) |
| 61 |
mocker.patch(MODULE_PATH + "idrac_user_info.iDRACRedfishAPI.invoke_request", |
| 62 |
return_value=(obj)) |
| 63 |
mocker.patch(MODULE_PATH + "idrac_user_info.strip_substr_dict", |
| 64 |
return_value=({"UserName": "test"})) |
| 65 |
resp = self.module.get_user_id_accounts( |
| 66 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", 1) |
| 67 |
assert resp.get("UserName") == "test" |
| 68 |
|
| 69 |
obj = MagicMock() |
| 70 |
obj.json_data = {"UserName": "test", "Oem": {"Dell": "test"}} |
| 71 |
mocker.patch(MODULE_PATH + "idrac_user_info.iDRACRedfishAPI.invoke_request", |
| 72 |
return_value=(obj)) |
| 73 |
mocker.patch(MODULE_PATH + "idrac_user_info.strip_substr_dict", |
| 74 |
return_value=({"UserName": "test", "Oem": {"Dell": "test"}})) |
| 75 |
resp = self.module.get_user_id_accounts( |
| 76 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", 1) |
| 77 |
assert resp.get("UserName") == "test" |
| 78 |
|
| 79 |
idrac_connection_user_info_mock.invoke_request.side_effect = HTTPError( |
| 80 |
HTTPS_ADDRESS, 400, |
| 81 |
'http error message', |
| 82 |
{"accept-type": "application/json"}, |
| 83 |
StringIO(json_str)) |
| 84 |
with pytest.raises(Exception) as exc: |
| 85 |
self.module.get_user_id_accounts( |
| 86 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", 1) |
| 87 |
assert exc.value.args[0] == "'user_id' is not valid." |
| 88 |
|
| 89 |
def test_get_user_name_accounts(self, idrac_default_args, idrac_connection_user_info_mock, |
| 90 |
idrac_user_info_mock, mocker): |
| 91 |
idrac_default_args.update({"username": "test"}) |
| 92 |
mocker.patch(MODULE_PATH + "idrac_user_info.fetch_all_accounts", |
| 93 |
return_value=([{"UserName": "test"}])) |
| 94 |
mocker.patch(MODULE_PATH + "idrac_user_info.strip_substr_dict", |
| 95 |
return_value=({"UserName": "test"})) |
| 96 |
f_module = self.get_module_mock( |
| 97 |
params=idrac_default_args, check_mode=False) |
| 98 |
resp = self.module.get_user_name_accounts( |
| 99 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", "test") |
| 100 |
assert resp.get("UserName") == "test" |
| 101 |
|
| 102 |
mocker.patch(MODULE_PATH + "idrac_user_info.strip_substr_dict", |
| 103 |
return_value=({"UserName": "test", "Oem": {"Dell": "test"}})) |
| 104 |
resp = self.module.get_user_name_accounts( |
| 105 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", "test") |
| 106 |
assert resp.get("UserName") == "test" |
| 107 |
|
| 108 |
with pytest.raises(Exception) as exc: |
| 109 |
self.module.get_user_name_accounts( |
| 110 |
idrac_connection_user_info_mock, f_module, "/acounts/accdetails", "test1") |
| 111 |
assert exc.value.args[0] == "'username' is not valid." |
| 112 |
|
| 113 |
def test_get_all_accounts_single(self, idrac_default_args, idrac_connection_user_info_mock, |
| 114 |
idrac_user_info_mock, mocker): |
| 115 |
idrac_default_args.update({"username": "test"}) |
| 116 |
mocker.patch(MODULE_PATH + "idrac_user_info.fetch_all_accounts", |
| 117 |
return_value=([{"UserName": "test", "Oem": {"Dell": "test"}}])) |
| 118 |
mocker.patch(MODULE_PATH + "idrac_user_info.strip_substr_dict", |
| 119 |
return_value=({"UserName": "test", "Oem": {"Dell": "test"}})) |
| 120 |
resp = self.module.get_all_accounts( |
| 121 |
idrac_connection_user_info_mock, "/acounts/accdetails") |
| 122 |
assert resp[0].get("UserName") == "test" |
| 123 |
|
| 124 |
mocker.patch(MODULE_PATH + "idrac_user_info.fetch_all_accounts", |
| 125 |
return_value=([{"UserName": ""}])) |
| 126 |
resp = self.module.get_all_accounts( |
| 127 |
idrac_connection_user_info_mock, "/acounts/accdetails") |
| 128 |
assert resp == [] |
| 129 |
|
| 130 |
mocker.patch(MODULE_PATH + "idrac_user_info.fetch_all_accounts", |
| 131 |
return_value=([])) |
| 132 |
resp = self.module.get_all_accounts( |
| 133 |
idrac_connection_user_info_mock, "/acounts/accdetails") |
| 134 |
assert resp == [] |
| 135 |
|
| 136 |
def test_get_all_accounts_multiple(self, idrac_default_args, idrac_connection_user_info_mock, |
| 137 |
idrac_user_info_mock, mocker): |
| 138 |
def strip_substr_dict_mock(acc): |
| 139 |
if acc.get("UserName") == "test": |
| 140 |
return {"UserName": "test"} |
| 141 |
else: |
| 142 |
return {"UserName": "test1"} |
| 143 |
mocker.side_effect = strip_substr_dict_mock |
| 144 |
|
| 145 |
mocker.patch(MODULE_PATH + "idrac_user_info.fetch_all_accounts", |
| 146 |
return_value=([{"UserName": "test"}, {"UserName": "test1"}])) |
| 147 |
resp = self.module.get_all_accounts( |
| 148 |
idrac_connection_user_info_mock, "/acounts/accdetails") |
| 149 |
assert resp[0].get("UserName") == "test" |
| 150 |
assert resp[1].get("UserName") == "test1" |
| 151 |
|
| 152 |
def test_get_accounts_uri(self, idrac_default_args, idrac_connection_user_info_mock, |
| 153 |
idrac_user_info_mock, mocker): |
| 154 |
acc_service_uri = MagicMock() |
| 155 |
acc_service_uri.json_data = {"AccountService": { |
| 156 |
"@odata.id": "/account"}, "Accounts": {"@odata.id": "/account/accountdetails"}} |
| 157 |
acc_service = MagicMock() |
| 158 |
acc_service.json_data = {"Accounts": { |
| 159 |
"@odata.id": "/account/accountdetails"}} |
| 160 |
|
| 161 |
mocker.patch(MODULE_PATH + "idrac_user_info.iDRACRedfishAPI.invoke_request", |
| 162 |
return_value=(acc_service_uri)) |
| 163 |
resp = self.module.get_accounts_uri(idrac_connection_user_info_mock) |
| 164 |
assert resp == "/account/accountdetails" |
| 165 |
|
| 166 |
json_str = to_text(json.dumps({"data": "out"})) |
| 167 |
idrac_connection_user_info_mock.invoke_request.side_effect = HTTPError( |
| 168 |
HTTPS_ADDRESS, 400, |
| 169 |
'http error message', |
| 170 |
{"accept-type": "application/json"}, |
| 171 |
StringIO(json_str)) |
| 172 |
|
| 173 |
resp = self.module.get_accounts_uri(idrac_connection_user_info_mock) |
| 174 |
assert resp == "/redfish/v1/AccountService/Accounts" |
| 175 |
|
| 176 |
def test_user_info_main_success_case_all(self, idrac_default_args, idrac_connection_user_info_mock, |
| 177 |
idrac_user_info_mock, mocker): |
| 178 |
idrac_default_args.update({"username": "test"}) |
| 179 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_accounts_uri", |
| 180 |
return_value=("/acounts/accdetails")) |
| 181 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_user_name_accounts", |
| 182 |
return_value=({"UserName": "test"})) |
| 183 |
idrac_user_info_mock.status_code = 200 |
| 184 |
idrac_user_info_mock.success = True |
| 185 |
resp = self._run_module(idrac_default_args) |
| 186 |
assert resp['msg'] == "Successfully retrieved the user information." |
| 187 |
assert resp['user_info'][0].get("UserName") == "test" |
| 188 |
|
| 189 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_user_id_accounts", |
| 190 |
return_value=({"UserName": "test"})) |
| 191 |
idrac_default_args.update({"user_id": "1234"}) |
| 192 |
idrac_default_args.pop("username") |
| 193 |
resp = self._run_module(idrac_default_args) |
| 194 |
assert resp['msg'] == "Successfully retrieved the user information." |
| 195 |
assert resp['user_info'][0].get("UserName") == "test" |
| 196 |
|
| 197 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_all_accounts", |
| 198 |
return_value=([{"UserName": "test"}])) |
| 199 |
idrac_default_args.pop("user_id") |
| 200 |
resp = self._run_module(idrac_default_args) |
| 201 |
assert resp['msg'] == "Successfully retrieved the information of 1 user(s)." |
| 202 |
assert resp['user_info'][0].get("UserName") == "test" |
| 203 |
|
| 204 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_all_accounts", |
| 205 |
return_value=([])) |
| 206 |
resp = self._run_module_with_fail_json(idrac_default_args) |
| 207 |
assert resp['failed'] is True |
| 208 |
assert resp['msg'] == "Unable to retrieve the user information." |
| 209 |
|
| 210 |
@pytest.mark.parametrize("exc_type", |
| 211 |
[URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError]) |
| 212 |
def test_idrac_user_info_main_exception_handling_case(self, exc_type, mocker, idrac_default_args, |
| 213 |
idrac_connection_user_info_mock, idrac_user_info_mock): |
| 214 |
idrac_user_info_mock.status_code = 400 |
| 215 |
idrac_user_info_mock.success = False |
| 216 |
json_str = to_text(json.dumps({"data": "out"})) |
| 217 |
if exc_type not in [HTTPError, SSLValidationError]: |
| 218 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_accounts_uri", |
| 219 |
side_effect=exc_type('test')) |
| 220 |
else: |
| 221 |
mocker.patch(MODULE_PATH + "idrac_user_info.get_accounts_uri", |
| 222 |
side_effect=exc_type(HTTPS_ADDRESS, 400, |
| 223 |
'http error message', |
| 224 |
{"accept-type": "application/json"}, |
| 225 |
StringIO(json_str))) |
| 226 |
if exc_type != URLError: |
| 227 |
result = self._run_module_with_fail_json(idrac_default_args) |
| 228 |
assert result['failed'] is True |
| 229 |
else: |
| 230 |
result = self._run_module(idrac_default_args) |
| 231 |
assert 'msg' in result |