| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import (absolute_import, division, print_function) |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
from ansible_collections.infoblox.nios_modules.plugins.modules import nios_adminuser |
| 24 |
from ansible_collections.infoblox.nios_modules.plugins.module_utils import api |
| 25 |
from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock |
| 26 |
from .test_nios_module import TestNiosModule, load_fixture |
| 27 |
|
| 28 |
|
| 29 |
class TestNiosAdminUserModule(TestNiosModule): |
| 30 |
|
| 31 |
module = nios_adminuser |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
super(TestNiosAdminUserModule, self).setUp() |
| 35 |
self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_adminuser.WapiModule') |
| 36 |
self.module.check_mode = False |
| 37 |
self.module.params = {'provider': None} |
| 38 |
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_adminuser.WapiModule') |
| 39 |
self.exec_command = self.mock_wapi.start() |
| 40 |
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_adminuser.WapiModule.run') |
| 41 |
self.load_config = self.mock_wapi_run.start() |
| 42 |
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict') |
| 43 |
self.mock_check_type_dict_obj = self.mock_check_type_dict.start() |
| 44 |
|
| 45 |
def tearDown(self): |
| 46 |
super(TestNiosAdminUserModule, self).tearDown() |
| 47 |
self.mock_wapi.stop() |
| 48 |
self.mock_wapi_run.stop() |
| 49 |
self.mock_check_type_dict.stop() |
| 50 |
|
| 51 |
def _get_wapi(self, test_object): |
| 52 |
wapi = api.WapiModule(self.module) |
| 53 |
wapi.get_object = Mock(name='get_object', return_value=test_object) |
| 54 |
wapi.create_object = Mock(name='create_object') |
| 55 |
wapi.update_object = Mock(name='update_object') |
| 56 |
wapi.delete_object = Mock(name='delete_object') |
| 57 |
return wapi |
| 58 |
|
| 59 |
def load_fixtures(self, commands=None): |
| 60 |
self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) |
| 61 |
self.load_config.return_value = dict(diff=None, session='session') |
| 62 |
|
| 63 |
def test_nios_adminuser_create(self): |
| 64 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_user', |
| 65 |
'admin_groups': ['admin-group'], 'password': 'Pwd@1234', |
| 66 |
'comment': None, 'extattrs': None} |
| 67 |
|
| 68 |
test_object = None |
| 69 |
|
| 70 |
test_spec = { |
| 71 |
"name": {"ib_req": True}, |
| 72 |
"admin_groups": {"ib_req": True}, |
| 73 |
"password": {}, |
| 74 |
"comment": {}, |
| 75 |
"extattrs": {} |
| 76 |
} |
| 77 |
|
| 78 |
wapi = self._get_wapi(test_object) |
| 79 |
print("WAPI: ", wapi.__dict__) |
| 80 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 81 |
|
| 82 |
self.assertTrue(res['changed']) |
| 83 |
wapi.create_object.assert_called_once_with( |
| 84 |
'NIOS_ADMINUSER', |
| 85 |
{ |
| 86 |
'name': 'ansible_user', |
| 87 |
'admin_groups': ['admin-group'], |
| 88 |
'password': 'Pwd@1234' |
| 89 |
} |
| 90 |
) |
| 91 |
|
| 92 |
def test_nios_adminuser_update_comment(self): |
| 93 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_user', |
| 94 |
'admin_groups': ['admin-group'], 'comment': 'updated comment', |
| 95 |
'extattrs': None} |
| 96 |
|
| 97 |
ref = "adminuser/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_user" |
| 98 |
test_object = [ |
| 99 |
{ |
| 100 |
"comment": "test comment", |
| 101 |
"_ref": ref, |
| 102 |
"name": "ansible_user", |
| 103 |
'admin_groups': ['admin-group'], |
| 104 |
"extattrs": {} |
| 105 |
} |
| 106 |
] |
| 107 |
|
| 108 |
test_spec = { |
| 109 |
"name": {"ib_req": True}, |
| 110 |
"admin_groups": {"ib_req": True}, |
| 111 |
"comment": {}, |
| 112 |
"extattrs": {} |
| 113 |
} |
| 114 |
|
| 115 |
wapi = self._get_wapi(test_object) |
| 116 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 117 |
self.assertTrue(res['changed']) |
| 118 |
wapi.update_object.assert_called_once_with( |
| 119 |
ref, {'comment': 'updated comment', 'admin_groups': ['admin-group'], 'name': 'ansible_user'} |
| 120 |
) |
| 121 |
|
| 122 |
def test_nios_adminuser_remove(self): |
| 123 |
self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible_user', |
| 124 |
'admin_groups': ['admin-group'], 'comment': None, 'extattrs': None} |
| 125 |
|
| 126 |
ref = "adminuser/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_user" |
| 127 |
|
| 128 |
test_object = [{ |
| 129 |
"comment": "test comment", |
| 130 |
"_ref": ref, |
| 131 |
"name": "ansible_user", |
| 132 |
"admin_groups": ['admin-group'], |
| 133 |
"extattrs": {'Site': {'value': 'test'}} |
| 134 |
}] |
| 135 |
|
| 136 |
test_spec = { |
| 137 |
"name": {"ib_req": True}, |
| 138 |
"admin_groups": {"ib_req": True}, |
| 139 |
"comment": {}, |
| 140 |
"extattrs": {} |
| 141 |
} |
| 142 |
|
| 143 |
wapi = self._get_wapi(test_object) |
| 144 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 145 |
|
| 146 |
self.assertTrue(res['changed']) |
| 147 |
wapi.delete_object.assert_called_once_with(ref) |
| 148 |
|
| 149 |
def test_nios_adminuser_update_record_name(self): |
| 150 |
self.module.params = {'provider': None, 'state': 'present', 'name': {'new_name': 'ansible_new_user', 'old_name': 'ansible_user'}, |
| 151 |
'admin_groups': ['admin-group'], 'comment': 'comment', 'extattrs': None} |
| 152 |
|
| 153 |
ref = "adminuser/ZG5zLm5ldHdvcmtfdmlldyQw:ansible_new_user" |
| 154 |
test_object = [ |
| 155 |
{ |
| 156 |
"comment": "test comment", |
| 157 |
"_ref": ref, |
| 158 |
"name": "ansible_user", |
| 159 |
"extattrs": {} |
| 160 |
} |
| 161 |
] |
| 162 |
|
| 163 |
test_spec = { |
| 164 |
"name": {"ib_req": True}, |
| 165 |
"admin_groups": {"ib_req": True}, |
| 166 |
"comment": {}, |
| 167 |
"extattrs": {} |
| 168 |
} |
| 169 |
|
| 170 |
wapi = self._get_wapi(test_object) |
| 171 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 172 |
|
| 173 |
self.assertTrue(res['changed']) |
| 174 |
wapi.update_object.assert_called_once_with(ref, {'name': 'ansible_new_user', 'admin_groups': ['admin-group'], |
| 175 |
'comment': 'comment'}) |
| 176 |
|
| 177 |
def test_nios_adminuser_create_with_ssh_keys(self): |
| 178 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_user', 'admin_groups': ['admin-group'], |
| 179 |
'password': 'Pwd@1234', 'auth_method': 'KEYPAIR_PASSWORD', 'use_ssh_keys': True, |
| 180 |
'ssh_keys': [{"key_name": "ansiblekey1", "key_type": "RSA", "key_value": "ssh-rsa AAAAB"}], |
| 181 |
'comment': None, 'extattrs': None, 'disable': False, 'email': '[email protected]'} |
| 182 |
|
| 183 |
test_object = None |
| 184 |
|
| 185 |
test_spec = { |
| 186 |
"name": {"ib_req": True}, |
| 187 |
"admin_groups": {"ib_req": True}, |
| 188 |
"password": {}, |
| 189 |
"auth_method": {}, |
| 190 |
"ssh_keys": {}, |
| 191 |
"use_ssh_keys": {}, |
| 192 |
"comment": {}, |
| 193 |
"extattrs": {}, |
| 194 |
"disable": {}, |
| 195 |
"email": {} |
| 196 |
} |
| 197 |
|
| 198 |
wapi = self._get_wapi(test_object) |
| 199 |
print("WAPI: ", wapi.__dict__) |
| 200 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 201 |
|
| 202 |
self.assertTrue(res['changed']) |
| 203 |
wapi.create_object.assert_called_once_with( |
| 204 |
'NIOS_ADMINUSER', |
| 205 |
{ |
| 206 |
'name': 'ansible_user', |
| 207 |
'admin_groups': ['admin-group'], |
| 208 |
'password': 'Pwd@1234', |
| 209 |
'auth_method': 'KEYPAIR_PASSWORD', |
| 210 |
'use_ssh_keys': True, |
| 211 |
'ssh_keys': [ |
| 212 |
{ |
| 213 |
'key_name': 'ansiblekey1', |
| 214 |
'key_type': 'RSA', |
| 215 |
'key_value': 'ssh-rsa AAAAB' |
| 216 |
} |
| 217 |
], |
| 218 |
'email': '[email protected]', |
| 219 |
'disable': False |
| 220 |
} |
| 221 |
) |
| 222 |
|
| 223 |
def test_nios_adminuser_create_with_ca_cert(self): |
| 224 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible_user', 'admin_groups': ['admin-group'], |
| 225 |
'password': 'Pwd@1234', 'enable_certificate_authentication': True, 'ca_certificate_issuer': 'CN="ib-root-ca"', |
| 226 |
'client_certificate_serial_number': '123456789', 'use_time_zone': True, 'time_zone': 'UTC', 'comment': None, |
| 227 |
'extattrs': None} |
| 228 |
|
| 229 |
test_object = None |
| 230 |
|
| 231 |
test_spec = { |
| 232 |
"name": {"ib_req": True}, |
| 233 |
"admin_groups": {"ib_req": True}, |
| 234 |
"password": {}, |
| 235 |
"enable_certificate_authentication": {}, |
| 236 |
"ca_certificate_issuer": {}, |
| 237 |
"client_certificate_serial_number": {}, |
| 238 |
"use_time_zone": {}, |
| 239 |
"time_zone": {}, |
| 240 |
"comment": {}, |
| 241 |
"extattrs": {}, |
| 242 |
} |
| 243 |
|
| 244 |
wapi = self._get_wapi(test_object) |
| 245 |
print("WAPI: ", wapi.__dict__) |
| 246 |
res = wapi.run('NIOS_ADMINUSER', test_spec) |
| 247 |
|
| 248 |
self.assertTrue(res['changed']) |
| 249 |
wapi.create_object.assert_called_once_with( |
| 250 |
'NIOS_ADMINUSER', |
| 251 |
{ |
| 252 |
'name': 'ansible_user', |
| 253 |
'admin_groups': ['admin-group'], |
| 254 |
'password': 'Pwd@1234', |
| 255 |
'enable_certificate_authentication': True, |
| 256 |
'ca_certificate_issuer': 'CN="ib-root-ca"', |
| 257 |
'client_certificate_serial_number': '123456789', |
| 258 |
'use_time_zone': True, |
| 259 |
'time_zone': 'UTC' |
| 260 |
} |
| 261 |
) |