| 1 |
# Copyright (c) 2021 Florian Dambrine <[email protected]> |
| 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 unittest.mock import patch |
| 8 |
|
| 9 |
from ansible.module_utils.common.dict_transformations import dict_merge |
| 10 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 11 |
AnsibleExitJson, |
| 12 |
AnsibleFailJson, |
| 13 |
ModuleTestCase, |
| 14 |
set_module_args, |
| 15 |
) |
| 16 |
|
| 17 |
from ansible_collections.community.general.plugins.modules import ( |
| 18 |
pritunl_user, |
| 19 |
) |
| 20 |
from ansible_collections.community.general.tests.unit.plugins.module_utils.test__pritunl_api import ( |
| 21 |
PritunlDeleteUserMock, |
| 22 |
PritunlListOrganizationMock, |
| 23 |
PritunlListUserMock, |
| 24 |
PritunlPostUserMock, |
| 25 |
PritunlPutUserMock, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def mock_pritunl_api(func, **kwargs): |
| 30 |
def wrapped(self=None): |
| 31 |
with self.patch_get_pritunl_organizations(side_effect=PritunlListOrganizationMock): |
| 32 |
with self.patch_get_pritunl_users(side_effect=PritunlListUserMock): |
| 33 |
with self.patch_add_pritunl_users(side_effect=PritunlPostUserMock): |
| 34 |
with self.patch_delete_pritunl_users(side_effect=PritunlDeleteUserMock): |
| 35 |
func(self, **kwargs) |
| 36 |
|
| 37 |
return wrapped |
| 38 |
|
| 39 |
|
| 40 |
class TestPritunlUser(ModuleTestCase): |
| 41 |
def setUp(self): |
| 42 |
super().setUp() |
| 43 |
self.module = pritunl_user |
| 44 |
|
| 45 |
def tearDown(self): |
| 46 |
super().tearDown() |
| 47 |
|
| 48 |
def patch_get_pritunl_users(self, **kwds): |
| 49 |
return patch( |
| 50 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._get_pritunl_users", |
| 51 |
autospec=True, |
| 52 |
**kwds, |
| 53 |
) |
| 54 |
|
| 55 |
def patch_add_pritunl_users(self, **kwds): |
| 56 |
return patch( |
| 57 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._post_pritunl_user", |
| 58 |
autospec=True, |
| 59 |
**kwds, |
| 60 |
) |
| 61 |
|
| 62 |
def patch_update_pritunl_users(self, **kwds): |
| 63 |
return patch( |
| 64 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._put_pritunl_user", |
| 65 |
autospec=True, |
| 66 |
**kwds, |
| 67 |
) |
| 68 |
|
| 69 |
def patch_delete_pritunl_users(self, **kwds): |
| 70 |
return patch( |
| 71 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._delete_pritunl_user", |
| 72 |
autospec=True, |
| 73 |
**kwds, |
| 74 |
) |
| 75 |
|
| 76 |
def patch_get_pritunl_organizations(self, **kwds): |
| 77 |
return patch( |
| 78 |
"ansible_collections.community.general.plugins.module_utils._pritunl_api._get_pritunl_organizations", |
| 79 |
autospec=True, |
| 80 |
**kwds, |
| 81 |
) |
| 82 |
|
| 83 |
def test_without_parameters(self): |
| 84 |
"""Test without parameters""" |
| 85 |
with set_module_args({}): |
| 86 |
with self.assertRaises(AnsibleFailJson): |
| 87 |
self.module.main() |
| 88 |
|
| 89 |
@mock_pritunl_api |
| 90 |
def test_present(self): |
| 91 |
"""Test Pritunl user creation and update.""" |
| 92 |
user_params = { |
| 93 |
"user_name": "alice", |
| 94 |
"user_email": "[email protected]", |
| 95 |
} |
| 96 |
with set_module_args( |
| 97 |
dict_merge( |
| 98 |
{ |
| 99 |
"pritunl_api_token": "token", |
| 100 |
"pritunl_api_secret": "secret", |
| 101 |
"pritunl_url": "https://pritunl.domain.com", |
| 102 |
"organization": "GumGum", |
| 103 |
}, |
| 104 |
user_params, |
| 105 |
) |
| 106 |
): |
| 107 |
with self.patch_update_pritunl_users(side_effect=PritunlPostUserMock): |
| 108 |
with self.assertRaises(AnsibleExitJson) as create_result: |
| 109 |
self.module.main() |
| 110 |
|
| 111 |
create_exc = create_result.exception.args[0] |
| 112 |
|
| 113 |
self.assertTrue(create_exc["changed"]) |
| 114 |
self.assertEqual(create_exc["response"]["name"], user_params["user_name"]) |
| 115 |
self.assertEqual(create_exc["response"]["email"], user_params["user_email"]) |
| 116 |
self.assertFalse(create_exc["response"]["disabled"]) |
| 117 |
|
| 118 |
# Changing user from alice to bob should update certain fields only |
| 119 |
|
| 120 |
new_user_params = { |
| 121 |
"user_name": "bob", |
| 122 |
"user_email": "[email protected]", |
| 123 |
"user_disabled": True, |
| 124 |
} |
| 125 |
with set_module_args( |
| 126 |
dict_merge( |
| 127 |
{ |
| 128 |
"pritunl_api_token": "token", |
| 129 |
"pritunl_api_secret": "secret", |
| 130 |
"pritunl_url": "https://pritunl.domain.com", |
| 131 |
"organization": "GumGum", |
| 132 |
}, |
| 133 |
new_user_params, |
| 134 |
) |
| 135 |
): |
| 136 |
with self.patch_update_pritunl_users(side_effect=PritunlPutUserMock): |
| 137 |
with self.assertRaises(AnsibleExitJson) as update_result: |
| 138 |
self.module.main() |
| 139 |
|
| 140 |
update_exc = update_result.exception.args[0] |
| 141 |
|
| 142 |
# Ensure only certain settings changed and the rest remained untouched. |
| 143 |
for k, v in update_exc.items(): |
| 144 |
if k in new_user_params: |
| 145 |
assert update_exc[k] == v |
| 146 |
else: |
| 147 |
assert update_exc[k] == create_exc[k] |
| 148 |
|
| 149 |
@mock_pritunl_api |
| 150 |
def test_absent(self): |
| 151 |
"""Test user removal from Pritunl.""" |
| 152 |
with set_module_args( |
| 153 |
{ |
| 154 |
"state": "absent", |
| 155 |
"pritunl_api_token": "token", |
| 156 |
"pritunl_api_secret": "secret", |
| 157 |
"pritunl_url": "https://pritunl.domain.com", |
| 158 |
"organization": "GumGum", |
| 159 |
"user_name": "florian", |
| 160 |
} |
| 161 |
): |
| 162 |
with self.assertRaises(AnsibleExitJson) as result: |
| 163 |
self.module.main() |
| 164 |
|
| 165 |
exc = result.exception.args[0] |
| 166 |
|
| 167 |
self.assertTrue(exc["changed"]) |
| 168 |
self.assertEqual(exc["response"], {}) |
| 169 |
|
| 170 |
@mock_pritunl_api |
| 171 |
def test_absent_failure(self): |
| 172 |
"""Test user removal from a non existing organization.""" |
| 173 |
with set_module_args( |
| 174 |
{ |
| 175 |
"state": "absent", |
| 176 |
"pritunl_api_token": "token", |
| 177 |
"pritunl_api_secret": "secret", |
| 178 |
"pritunl_url": "https://pritunl.domain.com", |
| 179 |
"organization": "Unknown", |
| 180 |
"user_name": "[email protected]", |
| 181 |
} |
| 182 |
): |
| 183 |
with self.assertRaises(AnsibleFailJson) as result: |
| 184 |
self.module.main() |
| 185 |
|
| 186 |
exc = result.exception.args[0] |
| 187 |
|
| 188 |
self.assertRegex(exc["msg"], "Can not remove user") |