| 1 |
# |
| 2 |
# Copyright (c) 2025, Kevin Quick <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
|
| 7 |
import sys |
| 8 |
from unittest.mock import MagicMock as MagicMike |
| 9 |
from unittest.mock import patch |
| 10 |
|
| 11 |
import pytest |
| 12 |
from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import ( |
| 13 |
AnsibleExitJson, |
| 14 |
AnsibleFailJson, |
| 15 |
ModuleTestCase, |
| 16 |
set_module_args, |
| 17 |
) |
| 18 |
|
| 19 |
# Skip tests if proxmoxer is not available |
| 20 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 21 |
|
| 22 |
# Handle different import paths for different test environments |
| 23 |
try: |
| 24 |
import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils |
| 25 |
from ansible_collections.community.proxmox.plugins.modules import proxmox_user |
| 26 |
except ImportError: |
| 27 |
sys.path.insert(0, "plugins/modules") |
| 28 |
import proxmox_user |
| 29 |
|
| 30 |
sys.path.insert(0, "plugins/module_utils") |
| 31 |
import proxmox as proxmox_utils |
| 32 |
|
| 33 |
|
| 34 |
class TestProxmoxUserModule(ModuleTestCase): |
| 35 |
"""Test cases for proxmox_user module using ModuleTestCase pattern.""" |
| 36 |
|
| 37 |
# Common test data |
| 38 |
BASIC_MODULE_ARGS = { |
| 39 |
"api_host": "test.proxmox.com", |
| 40 |
"api_user": "root@pam", |
| 41 |
"api_password": "secret", |
| 42 |
} |
| 43 |
|
| 44 |
SAMPLE_USER = { |
| 45 |
"userid": "testuser@pam", |
| 46 |
"comment": "Test User", |
| 47 |
"email": "[email protected]", |
| 48 |
"enable": 1, |
| 49 |
"expire": 0, |
| 50 |
"firstname": "John", |
| 51 |
"lastname": "Doe", |
| 52 |
"groups": ["admins"], |
| 53 |
"keys": "", |
| 54 |
} |
| 55 |
|
| 56 |
def setUp(self): |
| 57 |
super().setUp() |
| 58 |
proxmox_utils.HAS_PROXMOXER = True |
| 59 |
self.module = proxmox_user |
| 60 |
self.connect_mock = patch( |
| 61 |
"ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect" |
| 62 |
) |
| 63 |
self.connect_mock.start() |
| 64 |
|
| 65 |
def tearDown(self): |
| 66 |
self.connect_mock.stop() |
| 67 |
super().tearDown() |
| 68 |
|
| 69 |
def _create_module_args(self, **kwargs): |
| 70 |
"""Helper to create module arguments with defaults.""" |
| 71 |
args = self.BASIC_MODULE_ARGS.copy() |
| 72 |
args.update(kwargs) |
| 73 |
return args |
| 74 |
|
| 75 |
def test_module_fail_when_required_args_missing(self): |
| 76 |
"""Test module fails with missing required arguments""" |
| 77 |
with set_module_args({}), pytest.raises(AnsibleFailJson): |
| 78 |
proxmox_user.main() |
| 79 |
|
| 80 |
def test_user_creation_check_mode(self): |
| 81 |
"""Test user creation in check mode""" |
| 82 |
module_args = self._create_module_args( |
| 83 |
userid="testuser@pam", comment="Test User", state="present", _ansible_check_mode=True |
| 84 |
) |
| 85 |
|
| 86 |
with set_module_args(module_args), patch.object( |
| 87 |
proxmox_user.ProxmoxUserAnsible, "is_user_existing", return_value=False |
| 88 |
): |
| 89 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 90 |
proxmox_user.main() |
| 91 |
|
| 92 |
result = exc_info.value.args[0] |
| 93 |
assert result["changed"] is True |
| 94 |
assert "check mode" in result["msg"] |
| 95 |
|
| 96 |
def test_user_update_no_changes_needed(self): |
| 97 |
"""Test user update when no changes needed""" |
| 98 |
module_args = self._create_module_args(userid="testuser@pam", comment="Test User", state="present") |
| 99 |
existing_user = { |
| 100 |
"userid": "testuser@pam", |
| 101 |
"comment": "Test User", |
| 102 |
"email": "", |
| 103 |
"enable": 1, |
| 104 |
"expire": 0, |
| 105 |
"firstname": "", |
| 106 |
"lastname": "", |
| 107 |
"groups": [], |
| 108 |
"keys": "", |
| 109 |
} |
| 110 |
|
| 111 |
with set_module_args(module_args): |
| 112 |
mock_user_exists = patch.object( |
| 113 |
proxmox_user.ProxmoxUserAnsible, "is_user_existing", return_value=existing_user |
| 114 |
) |
| 115 |
mock_needs_update = patch.object(proxmox_user.ProxmoxUserAnsible, "_user_needs_update", return_value=False) |
| 116 |
|
| 117 |
with mock_user_exists, mock_needs_update: |
| 118 |
with pytest.raises(AnsibleExitJson) as exc_info: |
| 119 |
proxmox_user.main() |
| 120 |
|
| 121 |
result = exc_info.value.args[0] |
| 122 |
assert result["changed"] is False |
| 123 |
assert "already up to date" in result["msg"] |
| 124 |
|
| 125 |
|
| 126 |
# Tests for internal methods and business logic |
| 127 |
class TestProxmoxUserInternals: |
| 128 |
"""Test internal methods and business logic of ProxmoxUserAnsible class.""" |
| 129 |
|
| 130 |
# Test data for internal method testing |
| 131 |
SAMPLE_EXISTING_USER = { |
| 132 |
"userid": "testuser@pam", |
| 133 |
"comment": "Old comment", |
| 134 |
"email": "[email protected]", |
| 135 |
"enable": 1, |
| 136 |
"expire": 0, |
| 137 |
"firstname": "John", |
| 138 |
"lastname": "Doe", |
| 139 |
"groups": ["admins"], |
| 140 |
"tokens": { |
| 141 |
"token1": {"comment": "Token 1", "expire": 0, "privsep": False}, |
| 142 |
"token2": {"comment": "Token 2", "expire": 0, "privsep": True}, |
| 143 |
}, |
| 144 |
"keys": "", |
| 145 |
} |
| 146 |
|
| 147 |
@pytest.fixture |
| 148 |
def user_manager(self): |
| 149 |
"""Create a ProxmoxUserAnsible instance for internal testing.""" |
| 150 |
module = MagicMike() |
| 151 |
module.check_mode = False |
| 152 |
module.exit_json = MagicMike() |
| 153 |
module.fail_json = MagicMike() |
| 154 |
|
| 155 |
with patch.object(proxmox_utils.ProxmoxAnsible, "__init__", return_value=None): |
| 156 |
manager = proxmox_user.ProxmoxUserAnsible(module) |
| 157 |
manager.module = module |
| 158 |
manager.proxmox_api = MagicMike() |
| 159 |
return manager |
| 160 |
|
| 161 |
def test_user_needs_update_logic(self, user_manager): |
| 162 |
"""Test the _user_needs_update comparison logic for various scenarios.""" |
| 163 |
existing_user = self.SAMPLE_EXISTING_USER.copy() |
| 164 |
|
| 165 |
# Test case: No update needed - identical data |
| 166 |
no_update_needed = user_manager._user_needs_update( |
| 167 |
existing_user, "Old comment", "[email protected]", 1, 0, "John", "Doe", "admins", "" |
| 168 |
) |
| 169 |
assert no_update_needed is False |
| 170 |
|
| 171 |
# Test case: Update needed - different comment |
| 172 |
update_needed = user_manager._user_needs_update( |
| 173 |
existing_user, "New comment", "[email protected]", 1, 0, "John", "Doe", "admins", "" |
| 174 |
) |
| 175 |
assert update_needed is True |
| 176 |
|
| 177 |
def test_tokens_need_update_logic(self, user_manager): |
| 178 |
"""Test the _tokens_need_update comparison logic for various scenarios.""" |
| 179 |
existing_tokens = self.SAMPLE_EXISTING_USER.get("tokens", []).copy() |
| 180 |
TEST_TOKENS = [ |
| 181 |
{"tokenid": "token1", "comment": "Token 1", "expire": 0, "privsep": False}, |
| 182 |
{"tokenid": "token2", "comment": "Token 2", "expire": 0, "privsep": True}, |
| 183 |
{"tokenid": "token3", "comment": "Token 3", "expire": 0, "privsep": False}, |
| 184 |
] |
| 185 |
|
| 186 |
# Test case: No update needed - identical tokens |
| 187 |
assert user_manager._tokens_need_update(existing_tokens, TEST_TOKENS[0:2]) is False |
| 188 |
|
| 189 |
# Test case: Update needed - missing tokenid |
| 190 |
assert user_manager._tokens_need_update(existing_tokens, TEST_TOKENS[0]) is True |
| 191 |
|
| 192 |
# Test case: Update needed - additional tokenid |
| 193 |
assert user_manager._tokens_need_update(existing_tokens, TEST_TOKENS[0:3]) is True |
| 194 |
|
| 195 |
# Test case: Update needed - different token comment |
| 196 |
modified_token = TEST_TOKENS[0].copy() |
| 197 |
modified_token["comment"] = "Modified Comment" |
| 198 |
assert user_manager._tokens_need_update(existing_tokens, [modified_token, TEST_TOKENS[1]]) is True |
| 199 |
|
| 200 |
# Test case: Update needed - different token expire |
| 201 |
modified_token = TEST_TOKENS[0].copy() |
| 202 |
modified_token["expire"] = 1767222000 |
| 203 |
assert user_manager._tokens_need_update(existing_tokens, [modified_token, TEST_TOKENS[1]]) is True |
| 204 |
|
| 205 |
# Test case: Update needed - different token privsep |
| 206 |
modified_token = TEST_TOKENS[0].copy() |
| 207 |
modified_token["privsep"] = True |
| 208 |
assert user_manager._tokens_need_update(existing_tokens, [modified_token, TEST_TOKENS[1]]) is True |
| 209 |
|
| 210 |
def test_groups_format_handling(self, user_manager): |
| 211 |
"""Test groups comparison between API format (list) and module input format (string).""" |
| 212 |
existing_user_with_groups = {"userid": "testuser@pam", "groups": ["admins", "users"]} |
| 213 |
|
| 214 |
# Test case: Same groups in different formats - no update needed |
| 215 |
same_groups = user_manager._user_needs_update( |
| 216 |
existing_user_with_groups, None, None, 1, None, None, None, "admins,users", None |
| 217 |
) |
| 218 |
assert same_groups is False |
| 219 |
|
| 220 |
# Test case: Different groups - update needed |
| 221 |
different_groups = user_manager._user_needs_update( |
| 222 |
existing_user_with_groups, None, None, 1, None, None, None, "admins", None |
| 223 |
) |
| 224 |
assert different_groups is True |