| 1 |
# (c) 2018-2025, NetApp, Inc |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
''' unit tests ONTAP Ansible module: na_ontap_user ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
import pytest |
| 9 |
|
| 10 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 11 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 12 |
# pylint: disable=unused-import |
| 13 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\ |
| 14 |
patch_request_and_invoke, register_responses |
| 15 |
from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_error_message, rest_responses |
| 16 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_error, build_zapi_response, zapi_error_message, zapi_responses |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\ |
| 18 |
call_main, create_module, expect_and_capture_ansible_exception, patch_ansible |
| 19 |
|
| 20 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_user import NetAppOntapUser as my_module, main as my_main # module under test |
| 21 |
|
| 22 |
# if not netapp_utils.has_netapp_lib(): |
| 23 |
# pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') |
| 24 |
|
| 25 |
# REST API canned responses when mocking send_request |
| 26 |
SRR = rest_responses({ |
| 27 |
'repeated_password': (400, None, {'message': "New password must be different than the old password."}), |
| 28 |
'get_uuid': (200, {'owner': {'uuid': 'ansible'}}, None), |
| 29 |
'get_user_rest': (200, |
| 30 |
{'num_records': 1, |
| 31 |
'records': [{'owner': {'uuid': 'ansible_vserver'}, |
| 32 |
'name': 'abcd'}]}, None), |
| 33 |
'get_user_rest_multiple': (200, |
| 34 |
{'num_records': 2, |
| 35 |
'records': [{'owner': {'uuid': 'ansible_vserver'}, |
| 36 |
'name': 'abcd'}, |
| 37 |
{}]}, None), |
| 38 |
'get_user_details_rest': (200, |
| 39 |
{'role': {'name': 'vsadmin'}, |
| 40 |
'applications': [{'application': 'http'}], |
| 41 |
'locked': False}, None), |
| 42 |
'get_user_details_rest_no_pwd': (200, # locked is absent if no password was set |
| 43 |
{'role': {'name': 'vsadmin'}, |
| 44 |
'applications': [{'application': 'http'}], |
| 45 |
}, None) |
| 46 |
}, True) |
| 47 |
|
| 48 |
|
| 49 |
def login_info(locked, role_name, apps): |
| 50 |
attributes_list = [] |
| 51 |
for app in apps: |
| 52 |
if app in ('console', 'service-processor'): |
| 53 |
attributes_list.append( |
| 54 |
{'security-login-account-info': { |
| 55 |
'is-locked': locked, 'role-name': role_name, 'application': app, 'authentication-method': 'password'}} |
| 56 |
) |
| 57 |
if app in ('ssh',): |
| 58 |
attributes_list.append( |
| 59 |
{'security-login-account-info': { |
| 60 |
'is-locked': locked, 'role-name': role_name, 'application': 'ssh', 'authentication-method': 'publickey', |
| 61 |
'second-authentication-method': 'password'}}, |
| 62 |
) |
| 63 |
if app in ('http',): |
| 64 |
attributes_list.extend([ |
| 65 |
{'security-login-account-info': { |
| 66 |
'is-locked': locked, 'role-name': role_name, 'application': 'http', 'authentication-method': 'password'}}, |
| 67 |
]) |
| 68 |
return { |
| 69 |
'num-records': len(attributes_list), |
| 70 |
'attributes-list': attributes_list |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
ZRR = zapi_responses({ |
| 75 |
'login_locked_user': build_zapi_response(login_info("true", 'user', ['console', 'ssh'])), |
| 76 |
'login_unlocked_user': build_zapi_response(login_info("False", 'user', ['console', 'ssh'])), |
| 77 |
'login_unlocked_user_http': build_zapi_response(login_info("False", 'user', ['http'])), |
| 78 |
'login_unlocked_user_service_processor': build_zapi_response(login_info("False", 'user', ['service-processor'])), |
| 79 |
'user_not_found': build_zapi_error('16034', "This exception should not be seen"), |
| 80 |
'internal_error': build_zapi_error('13114', "Forcing an internal error"), |
| 81 |
'reused_password': build_zapi_error('13214', "New password must be different than last 6 passwords."), |
| 82 |
}, True) |
| 83 |
|
| 84 |
|
| 85 |
DEFAULT_ARGS = { |
| 86 |
'hostname': 'hostname', |
| 87 |
'username': 'username', |
| 88 |
'password': 'password', |
| 89 |
'name': 'user_name', |
| 90 |
'vserver': 'vserver', |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 95 |
def test_module_fail_when_required_args_missing(): |
| 96 |
''' required arguments are reported as errors ''' |
| 97 |
register_responses([ |
| 98 |
]) |
| 99 |
module_args = { |
| 100 |
'use_rest': 'never', |
| 101 |
} |
| 102 |
print('Info: %s' % call_main(my_main, {}, module_args, fail=True)['msg']) |
| 103 |
|
| 104 |
|
| 105 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 106 |
def test_module_fail_when_vserver_missing(): |
| 107 |
''' required arguments are reported as errors ''' |
| 108 |
register_responses([ |
| 109 |
]) |
| 110 |
module_args = { |
| 111 |
'use_rest': 'never', |
| 112 |
'hostname': 'hostname', |
| 113 |
'username': 'username', |
| 114 |
'password': 'password', |
| 115 |
'name': 'user_name', |
| 116 |
} |
| 117 |
assert 'Error: vserver is required' in call_main(my_main, {}, module_args, fail=True)['msg'] |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 121 |
def test_ensure_user_get_called(): |
| 122 |
''' a more interesting test ''' |
| 123 |
register_responses([ |
| 124 |
('ZAPI', 'security-login-get-iter', ZRR['success']), |
| 125 |
]) |
| 126 |
module_args = { |
| 127 |
'use_rest': 'never', |
| 128 |
'role_name': 'test', |
| 129 |
'applications': 'http', |
| 130 |
'authentication_method': 'password', |
| 131 |
} |
| 132 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 133 |
# app = dict(application='testapp', authentication_methods=['testam']) |
| 134 |
user_info = my_obj.get_user() |
| 135 |
print('Info: test_user_get: %s' % repr(user_info)) |
| 136 |
assert user_info is None |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 140 |
def test_ensure_user_get_called_not_found(): |
| 141 |
''' a more interesting test ''' |
| 142 |
register_responses([ |
| 143 |
('ZAPI', 'security-login-get-iter', ZRR['user_not_found']), |
| 144 |
]) |
| 145 |
module_args = { |
| 146 |
'use_rest': 'never', |
| 147 |
'role_name': 'test', |
| 148 |
'applications': 'http', |
| 149 |
'authentication_method': 'password', |
| 150 |
} |
| 151 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 152 |
# app = dict(application='testapp', authentication_methods=['testam']) |
| 153 |
user_info = my_obj.get_user() |
| 154 |
print('Info: test_user_get: %s' % repr(user_info)) |
| 155 |
assert user_info is None |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 159 |
def test_ensure_user_apply_called(): |
| 160 |
''' creating user and checking idempotency ''' |
| 161 |
register_responses([ |
| 162 |
('ZAPI', 'security-login-get-iter', ZRR['success']), |
| 163 |
('ZAPI', 'security-login-create', ZRR['success']), |
| 164 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user_http']), |
| 165 |
]) |
| 166 |
module_args = { |
| 167 |
'use_rest': 'never', |
| 168 |
'name': 'create', |
| 169 |
'role_name': 'user', |
| 170 |
'applications': 'http', |
| 171 |
'authentication_method': 'password', |
| 172 |
} |
| 173 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 174 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 175 |
|
| 176 |
|
| 177 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 178 |
def test_ensure_user_sp_apply_called(): |
| 179 |
''' creating user with service_processor application and idempotency ''' |
| 180 |
register_responses([ |
| 181 |
('ZAPI', 'security-login-get-iter', ZRR['no_records']), |
| 182 |
('ZAPI', 'security-login-create', ZRR['success']), |
| 183 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user_service_processor']), |
| 184 |
('ZAPI', 'security-login-get-iter', ZRR['no_records']), |
| 185 |
('ZAPI', 'security-login-create', ZRR['success']), |
| 186 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user_service_processor']), |
| 187 |
]) |
| 188 |
module_args = { |
| 189 |
'use_rest': 'never', |
| 190 |
'name': 'create', |
| 191 |
'role_name': 'user', |
| 192 |
'applications': 'service-processor', |
| 193 |
'authentication_method': 'password', |
| 194 |
} |
| 195 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 196 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 197 |
module_args['applications'] = 'service_processor' |
| 198 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 199 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 200 |
|
| 201 |
|
| 202 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 203 |
def test_ensure_user_apply_for_delete_called(): |
| 204 |
''' deleting user and checking idempotency ''' |
| 205 |
register_responses([ |
| 206 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 207 |
('ZAPI', 'security-login-delete', ZRR['success']), |
| 208 |
('ZAPI', 'security-login-get-iter', ZRR['no_records']), |
| 209 |
]) |
| 210 |
module_args = { |
| 211 |
"use_rest": "never", |
| 212 |
"state": "absent", |
| 213 |
'name': 'create', |
| 214 |
'role_name': 'user', |
| 215 |
'applications': 'console', |
| 216 |
'authentication_method': 'password', |
| 217 |
} |
| 218 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 219 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 220 |
|
| 221 |
|
| 222 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 223 |
def test_ensure_user_lock_called(): |
| 224 |
''' changing user_lock to True and checking idempotency''' |
| 225 |
register_responses([ |
| 226 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 227 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 228 |
('ZAPI', 'security-login-lock', ZRR['success']), |
| 229 |
]) |
| 230 |
module_args = { |
| 231 |
"use_rest": "never", |
| 232 |
"lock_user": False, |
| 233 |
'name': 'create', |
| 234 |
'role_name': 'user', |
| 235 |
'applications': 'console', |
| 236 |
'authentication_method': 'password', |
| 237 |
} |
| 238 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 239 |
module_args['lock_user'] = 'true' |
| 240 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 241 |
|
| 242 |
|
| 243 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 244 |
def test_ensure_user_unlock_called(): |
| 245 |
''' changing user_lock to False and checking idempotency''' |
| 246 |
register_responses([ |
| 247 |
('ZAPI', 'security-login-get-iter', ZRR['login_locked_user']), |
| 248 |
('ZAPI', 'security-login-get-iter', ZRR['login_locked_user']), |
| 249 |
('ZAPI', 'security-login-unlock', ZRR['success']), |
| 250 |
]) |
| 251 |
module_args = { |
| 252 |
"use_rest": "never", |
| 253 |
"lock_user": True, |
| 254 |
'name': 'create', |
| 255 |
'role_name': 'user', |
| 256 |
'applications': 'console', |
| 257 |
'authentication_method': 'password', |
| 258 |
} |
| 259 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 260 |
module_args['lock_user'] = 'false' |
| 261 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 262 |
|
| 263 |
|
| 264 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 265 |
def test_ensure_user_set_password_called(): |
| 266 |
''' set password ''' |
| 267 |
register_responses([ |
| 268 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 269 |
('ZAPI', 'security-login-modify-password', ZRR['success']), |
| 270 |
]) |
| 271 |
module_args = { |
| 272 |
'use_rest': 'never', |
| 273 |
'name': 'create', |
| 274 |
'role_name': 'user', |
| 275 |
'applications': 'console', |
| 276 |
'authentication_method': 'password', |
| 277 |
'set_password': '123456', |
| 278 |
} |
| 279 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 280 |
|
| 281 |
|
| 282 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 283 |
def test_set_password_internal_error(): |
| 284 |
''' set password ''' |
| 285 |
register_responses([ |
| 286 |
('ZAPI', 'security-login-modify-password', ZRR['internal_error']), |
| 287 |
]) |
| 288 |
module_args = { |
| 289 |
'use_rest': 'never', |
| 290 |
'name': 'create', |
| 291 |
'role_name': 'user', |
| 292 |
'applications': 'console', |
| 293 |
'authentication_method': 'password', |
| 294 |
'set_password': '123456', |
| 295 |
} |
| 296 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 297 |
assert not my_obj.change_password() |
| 298 |
|
| 299 |
|
| 300 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 301 |
def test_set_password_reused(): |
| 302 |
''' set password ''' |
| 303 |
register_responses([ |
| 304 |
('ZAPI', 'security-login-modify-password', ZRR['reused_password']) |
| 305 |
]) |
| 306 |
module_args = { |
| 307 |
'use_rest': 'never', |
| 308 |
'name': 'create', |
| 309 |
'role_name': 'user', |
| 310 |
'applications': 'console', |
| 311 |
'authentication_method': 'password', |
| 312 |
'set_password': '123456', |
| 313 |
} |
| 314 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 315 |
assert not my_obj.change_password() |
| 316 |
|
| 317 |
|
| 318 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 319 |
def test_ensure_user_role_update_called(): |
| 320 |
''' set password ''' |
| 321 |
register_responses([ |
| 322 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 323 |
('ZAPI', 'security-login-modify', ZRR['success']), |
| 324 |
('ZAPI', 'security-login-modify-password', ZRR['success']), |
| 325 |
]) |
| 326 |
module_args = { |
| 327 |
'use_rest': 'never', |
| 328 |
'name': 'create', |
| 329 |
'role_name': 'test123', |
| 330 |
'applications': 'console', |
| 331 |
'authentication_method': 'password', |
| 332 |
'set_password': '123456', |
| 333 |
} |
| 334 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 335 |
|
| 336 |
|
| 337 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 338 |
def test_ensure_user_role_update_additional_application_called(): |
| 339 |
''' set password ''' |
| 340 |
register_responses([ |
| 341 |
('ZAPI', 'security-login-get-iter', ZRR['login_unlocked_user']), |
| 342 |
('ZAPI', 'security-login-create', ZRR['success']), |
| 343 |
('ZAPI', 'security-login-delete', ZRR['success']), |
| 344 |
('ZAPI', 'security-login-delete', ZRR['success']), |
| 345 |
('ZAPI', 'security-login-modify-password', ZRR['success']), |
| 346 |
]) |
| 347 |
module_args = { |
| 348 |
'use_rest': 'never', |
| 349 |
'name': 'create', |
| 350 |
'role_name': 'test123', |
| 351 |
'applications': 'http', |
| 352 |
'authentication_method': 'password', |
| 353 |
'set_password': '123456', |
| 354 |
'replace_existing_apps_and_methods': 'always' |
| 355 |
} |
| 356 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 357 |
|
| 358 |
|
| 359 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 360 |
def test_if_all_methods_catch_exception(): |
| 361 |
register_responses([ |
| 362 |
('ZAPI', 'security-login-get-iter', ZRR['error']), |
| 363 |
('ZAPI', 'security-login-create', ZRR['error']), |
| 364 |
('ZAPI', 'security-login-lock', ZRR['error']), |
| 365 |
('ZAPI', 'security-login-unlock', ZRR['error']), |
| 366 |
('ZAPI', 'security-login-delete', ZRR['error']), |
| 367 |
('ZAPI', 'security-login-modify-password', ZRR['error']), |
| 368 |
('ZAPI', 'security-login-modify', ZRR['error']), |
| 369 |
]) |
| 370 |
module_args = { |
| 371 |
'use_rest': 'never', |
| 372 |
'role_name': 'test', |
| 373 |
'applications': 'console', |
| 374 |
'authentication_method': 'password', |
| 375 |
} |
| 376 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 377 |
app = dict(application='console', authentication_methods=['password']) |
| 378 |
assert zapi_error_message('Error getting user user_name') in expect_and_capture_ansible_exception(my_obj.get_user, 'fail')['msg'] |
| 379 |
assert zapi_error_message('Error creating user user_name') in expect_and_capture_ansible_exception(my_obj.create_user, 'fail', app)['msg'] |
| 380 |
assert zapi_error_message('Error locking user user_name') in expect_and_capture_ansible_exception(my_obj.lock_given_user, 'fail')['msg'] |
| 381 |
assert zapi_error_message('Error unlocking user user_name') in expect_and_capture_ansible_exception(my_obj.unlock_given_user, 'fail')['msg'] |
| 382 |
assert zapi_error_message('Error removing user user_name') in expect_and_capture_ansible_exception(my_obj.delete_user, 'fail', app)['msg'] |
| 383 |
assert zapi_error_message('Error setting password for user user_name') in expect_and_capture_ansible_exception(my_obj.change_password, 'fail')['msg'] |
| 384 |
assert zapi_error_message('Error modifying user user_name') in expect_and_capture_ansible_exception(my_obj.modify_user, 'fail', app, ['password'])['msg'] |
| 385 |
err_msg = 'vserver is required with ZAPI' |
| 386 |
assert err_msg in create_module(my_module, DEFAULT_ARGS, {'use_rest': 'never', 'svm': None}, fail=True)['msg'] |
| 387 |
|
| 388 |
|
| 389 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 390 |
def test_create_user_with_usm_auth(): |
| 391 |
''' switching back to ZAPI ''' |
| 392 |
register_responses([ |
| 393 |
('GET', 'cluster', SRR['is_rest']), |
| 394 |
('ZAPI', 'security-login-get-iter', ZRR['no_records']), |
| 395 |
('ZAPI', 'security-login-create', ZRR['success']), |
| 396 |
]) |
| 397 |
module_args = { |
| 398 |
'use_rest': 'auto', |
| 399 |
'applications': 'snmp', |
| 400 |
'authentication_method': 'usm', |
| 401 |
'name': 'create', |
| 402 |
'role_name': 'test123', |
| 403 |
'set_password': '123456', |
| 404 |
'remote_switch_ipaddress': '12.34.56.78', |
| 405 |
'authentication_password': 'auth_pwd', |
| 406 |
'authentication_protocol': 'md5', |
| 407 |
'privacy_password': 'auth_pwd', |
| 408 |
'privacy_protocol': 'des', |
| 409 |
'engine_id': 'engine_123', |
| 410 |
} |
| 411 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 412 |
|
| 413 |
|
| 414 |
def test_rest_error_applications_snmp(): |
| 415 |
register_responses([ |
| 416 |
('GET', 'cluster', SRR['is_rest']), |
| 417 |
]) |
| 418 |
module_args = { |
| 419 |
'use_rest': 'always', |
| 420 |
'applications': 'snmp', |
| 421 |
'authentication_method': 'usm', |
| 422 |
'name': 'create', |
| 423 |
'role_name': 'test123', |
| 424 |
'set_password': '123456', |
| 425 |
} |
| 426 |
assert call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] == "snmp as application is not supported in REST." |
| 427 |
|
| 428 |
|
| 429 |
def test_ensure_user_get_rest_called(): |
| 430 |
register_responses([ |
| 431 |
('GET', 'cluster', SRR['is_rest']), |
| 432 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 433 |
]) |
| 434 |
module_args = { |
| 435 |
"use_rest": "always", |
| 436 |
'role_name': 'vsadmin', |
| 437 |
'applications': ['http', 'ontapi'], |
| 438 |
'authentication_method': 'password', |
| 439 |
} |
| 440 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 441 |
assert my_obj.get_user_rest() is not None |
| 442 |
|
| 443 |
|
| 444 |
def test_ensure_create_user_rest_called(): |
| 445 |
register_responses([ |
| 446 |
('GET', 'cluster', SRR['is_rest']), |
| 447 |
('GET', 'security/accounts', SRR['zero_records']), |
| 448 |
('POST', 'security/accounts', SRR['empty_good']), |
| 449 |
]) |
| 450 |
module_args = { |
| 451 |
"use_rest": "always", |
| 452 |
'role_name': 'vsadmin', |
| 453 |
'applications': ['http', 'ontapi'], |
| 454 |
'authentication_method': 'password', |
| 455 |
'set_password': 'xfjjttjwll`1', |
| 456 |
'lock_user': True |
| 457 |
} |
| 458 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 459 |
|
| 460 |
|
| 461 |
def test_ensure_create_cluster_user_rest_called(): |
| 462 |
register_responses([ |
| 463 |
('GET', 'cluster', SRR['is_rest']), |
| 464 |
('GET', 'security/accounts', SRR['zero_records']), |
| 465 |
('POST', 'security/accounts', SRR['empty_good']), |
| 466 |
]) |
| 467 |
module_args = { |
| 468 |
"hostname": "hostname", |
| 469 |
"username": "username", |
| 470 |
"password": "password", |
| 471 |
"name": "user_name", |
| 472 |
"use_rest": "always", |
| 473 |
'role_name': 'vsadmin', |
| 474 |
'applications': ['http', 'ontapi'], |
| 475 |
'authentication_method': 'password', |
| 476 |
'set_password': 'xfjjttjwll`1', |
| 477 |
'lock_user': True |
| 478 |
} |
| 479 |
assert call_main(my_main, module_args)['changed'] |
| 480 |
|
| 481 |
|
| 482 |
def test_ensure_delete_user_rest_called(): |
| 483 |
register_responses([ |
| 484 |
('GET', 'cluster', SRR['is_rest']), |
| 485 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 486 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 487 |
('DELETE', 'security/accounts/ansible_vserver/abcd', SRR['empty_good']), |
| 488 |
]) |
| 489 |
module_args = { |
| 490 |
"use_rest": "always", |
| 491 |
'state': 'absent', |
| 492 |
'role_name': 'vsadmin', |
| 493 |
'applications': ['http', 'ontapi'], |
| 494 |
'authentication_method': 'password', |
| 495 |
} |
| 496 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 497 |
|
| 498 |
|
| 499 |
def test_ensure_modify_user_rest_called(): |
| 500 |
register_responses([ |
| 501 |
('GET', 'cluster', SRR['is_rest']), |
| 502 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 503 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 504 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['empty_good']), |
| 505 |
]) |
| 506 |
module_args = { |
| 507 |
"use_rest": "always", |
| 508 |
'role_name': 'vsadmin', |
| 509 |
'application': 'ssh', |
| 510 |
'authentication_method': 'password', |
| 511 |
} |
| 512 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 513 |
|
| 514 |
|
| 515 |
def test_ensure_lock_unlock_user_rest_called(): |
| 516 |
register_responses([ |
| 517 |
('GET', 'cluster', SRR['is_rest']), |
| 518 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 519 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 520 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['empty_good']), |
| 521 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['empty_good']), |
| 522 |
]) |
| 523 |
module_args = { |
| 524 |
"use_rest": "always", |
| 525 |
'role_name': 'vsadmin', |
| 526 |
'applications': 'http', |
| 527 |
'authentication_method': 'password', |
| 528 |
'lock_user': True, |
| 529 |
} |
| 530 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 531 |
|
| 532 |
|
| 533 |
def test_ensure_change_password_user_rest_called(): |
| 534 |
register_responses([ |
| 535 |
('GET', 'cluster', SRR['is_rest']), |
| 536 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 537 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 538 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['empty_good']), |
| 539 |
]) |
| 540 |
module_args = { |
| 541 |
'set_password': 'newvalue', |
| 542 |
'use_rest': 'always', |
| 543 |
} |
| 544 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 545 |
|
| 546 |
|
| 547 |
def test_change_password_user_rest_check_mode(): |
| 548 |
register_responses([ |
| 549 |
('GET', 'cluster', SRR['is_rest']), |
| 550 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 551 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 552 |
]) |
| 553 |
module_args = { |
| 554 |
'set_password': 'newvalue', |
| 555 |
'use_rest': 'always', |
| 556 |
} |
| 557 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 558 |
my_obj.module.check_mode = True |
| 559 |
assert expect_and_capture_ansible_exception(my_obj.apply, 'exit')['changed'] |
| 560 |
|
| 561 |
|
| 562 |
def test_existing_password(): |
| 563 |
register_responses([ |
| 564 |
('GET', 'cluster', SRR['is_rest']), |
| 565 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 566 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 567 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['repeated_password']), # password |
| 568 |
]) |
| 569 |
module_args = { |
| 570 |
'set_password': 'newvalue', |
| 571 |
'use_rest': 'always', |
| 572 |
} |
| 573 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 574 |
|
| 575 |
|
| 576 |
def test_negative_rest_unsupported_property(): |
| 577 |
register_responses([ |
| 578 |
]) |
| 579 |
module_args = { |
| 580 |
'privacy_password': 'value', |
| 581 |
'use_rest': 'always', |
| 582 |
} |
| 583 |
msg = "REST API currently does not support 'privacy_password'" |
| 584 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 585 |
|
| 586 |
|
| 587 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.has_netapp_lib') |
| 588 |
def test_negative_zapi_missing_netapp_lib(mock_has): |
| 589 |
register_responses([ |
| 590 |
]) |
| 591 |
mock_has.return_value = False |
| 592 |
module_args = { |
| 593 |
'use_rest': 'never', |
| 594 |
} |
| 595 |
msg = "Error: the python NetApp-Lib module is required." |
| 596 |
assert msg in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 597 |
|
| 598 |
|
| 599 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 600 |
def test_negative_zapi_missing_apps(): |
| 601 |
register_responses([ |
| 602 |
]) |
| 603 |
module_args = { |
| 604 |
'use_rest': 'never', |
| 605 |
} |
| 606 |
msg = "application_dicts or application_strs is a required parameter with ZAPI" |
| 607 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 608 |
|
| 609 |
|
| 610 |
def test_negative_rest_error_on_get_user(): |
| 611 |
register_responses([ |
| 612 |
('GET', 'cluster', SRR['is_rest']), |
| 613 |
('GET', 'security/accounts', SRR['generic_error']), |
| 614 |
]) |
| 615 |
module_args = { |
| 616 |
'use_rest': 'always', |
| 617 |
} |
| 618 |
msg = "Error while fetching user info: Expected error" |
| 619 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 620 |
|
| 621 |
|
| 622 |
def test_negative_rest_error_on_get_user_multiple(): |
| 623 |
register_responses([ |
| 624 |
('GET', 'cluster', SRR['is_rest']), |
| 625 |
('GET', 'security/accounts', SRR['get_user_rest_multiple']), |
| 626 |
]) |
| 627 |
module_args = { |
| 628 |
'use_rest': 'always', |
| 629 |
} |
| 630 |
msg = "Error while fetching user info, found multiple entries:" |
| 631 |
assert msg in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 632 |
|
| 633 |
|
| 634 |
def test_negative_rest_error_on_get_user_details(): |
| 635 |
register_responses([ |
| 636 |
('GET', 'cluster', SRR['is_rest']), |
| 637 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 638 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['generic_error']), |
| 639 |
]) |
| 640 |
module_args = { |
| 641 |
'use_rest': 'always', |
| 642 |
} |
| 643 |
msg = "Error while fetching user details: Expected error" |
| 644 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 645 |
|
| 646 |
|
| 647 |
def test_negative_rest_error_on_delete(): |
| 648 |
register_responses([ |
| 649 |
('GET', 'cluster', SRR['is_rest']), |
| 650 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 651 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 652 |
('DELETE', 'security/accounts/ansible_vserver/abcd', SRR['generic_error']), |
| 653 |
]) |
| 654 |
module_args = { |
| 655 |
"use_rest": "always", |
| 656 |
'state': 'absent', |
| 657 |
'role_name': 'vsadmin', |
| 658 |
} |
| 659 |
msg = "Error while deleting user: Expected error" |
| 660 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 661 |
|
| 662 |
|
| 663 |
def test_negative_rest_error_on_unlocking(): |
| 664 |
register_responses([ |
| 665 |
('GET', 'cluster', SRR['is_rest']), |
| 666 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 667 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 668 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['generic_error']), |
| 669 |
]) |
| 670 |
module_args = { |
| 671 |
'use_rest': 'always', |
| 672 |
'lock_user': True |
| 673 |
} |
| 674 |
msg = "Error while locking/unlocking user: Expected error" |
| 675 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 676 |
|
| 677 |
|
| 678 |
def test_negative_rest_error_on_unlocking_no_password(): |
| 679 |
register_responses([ |
| 680 |
('GET', 'cluster', SRR['is_rest']), |
| 681 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 682 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest_no_pwd']), |
| 683 |
]) |
| 684 |
module_args = { |
| 685 |
'use_rest': 'always', |
| 686 |
'lock_user': True |
| 687 |
} |
| 688 |
msg = "Error: cannot modify lock state if password is not set." |
| 689 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 690 |
|
| 691 |
|
| 692 |
def test_negative_rest_error_on_changing_password(): |
| 693 |
register_responses([ |
| 694 |
('GET', 'cluster', SRR['is_rest']), |
| 695 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 696 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 697 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['generic_error']), |
| 698 |
]) |
| 699 |
module_args = { |
| 700 |
'set_password': '12345', |
| 701 |
'use_rest': 'always', |
| 702 |
} |
| 703 |
msg = "Error while updating user password: Expected error" |
| 704 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 705 |
|
| 706 |
|
| 707 |
def test_negative_rest_error_on_modify(): |
| 708 |
register_responses([ |
| 709 |
('GET', 'cluster', SRR['is_rest']), |
| 710 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 711 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest']), |
| 712 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['generic_error']), |
| 713 |
]) |
| 714 |
module_args = { |
| 715 |
'role_name': 'vsadmin2', |
| 716 |
'use_rest': 'always', |
| 717 |
'applications': ['http', 'ontapi'], |
| 718 |
'authentication_method': 'password', |
| 719 |
} |
| 720 |
msg = "Error while modifying user details: Expected error" |
| 721 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 722 |
|
| 723 |
|
| 724 |
def test_rest_unlocking_with_password(): |
| 725 |
register_responses([ |
| 726 |
('GET', 'cluster', SRR['is_rest']), |
| 727 |
('GET', 'security/accounts', SRR['get_user_rest']), |
| 728 |
('GET', 'security/accounts/ansible_vserver/abcd', SRR['get_user_details_rest_no_pwd']), |
| 729 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['success']), |
| 730 |
('PATCH', 'security/accounts/ansible_vserver/abcd', SRR['success']), |
| 731 |
]) |
| 732 |
module_args = { |
| 733 |
'set_password': 'ansnssnajj12%', |
| 734 |
'use_rest': 'always', |
| 735 |
'lock_user': True |
| 736 |
} |
| 737 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 738 |
|
| 739 |
|
| 740 |
def test_negative_create_validations(): |
| 741 |
register_responses([ |
| 742 |
('GET', 'cluster', SRR['is_rest']), |
| 743 |
('GET', 'security/accounts', SRR['zero_records']), |
| 744 |
('GET', 'cluster', SRR['is_rest']), |
| 745 |
('GET', 'security/accounts', SRR['zero_records']), |
| 746 |
('GET', 'cluster', SRR['is_rest']), |
| 747 |
('GET', 'security/accounts', SRR['zero_records']), |
| 748 |
]) |
| 749 |
module_args = { |
| 750 |
'use_rest': 'always', |
| 751 |
} |
| 752 |
msg = 'Error: missing required parameters for create: role_name and: application_dicts or application_strs.' |
| 753 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 754 |
module_args['role_name'] = 'role' |
| 755 |
msg = 'Error: missing required parameter for create: application_dicts or application_strs.' |
| 756 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 757 |
module_args.pop('role_name') |
| 758 |
module_args['applications'] = 'http' |
| 759 |
module_args['authentication_method'] = 'password' |
| 760 |
msg = 'Error: missing required parameter for create: role_name.' |
| 761 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |