Debian Codegraph @

ansible_collections/community/proxmox/tests/unit/plugins/modules/test_proxmox_user_info.py

raw
1 #
2 # Copyright (c) 2026, Emmanuel Jamet <[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 from unittest.mock import patch
8
9 import pytest
10
11 proxmoxer = pytest.importorskip("proxmoxer")
12
13 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
14 AnsibleExitJson,
15 ModuleTestCase,
16 set_module_args,
17 )
18
19 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
20 from ansible_collections.community.proxmox.plugins.modules import (
21 proxmox_user_info,
22 )
23
24
25 class TestProxmoxUserInfoModule(ModuleTestCase):
26 """Test cases for proxmox_user_info module using ModuleTestCase pattern."""
27
28 # Common test data
29 BASIC_MODULE_ARGS = {
30 "api_host": "test.proxmox.com",
31 "api_user": "root@pam",
32 "api_password": "secret",
33 }
34
35 SAMPLE_USER = {
36 "userid": "testuser@pam",
37 "comment": "Test User",
38 "email": "[email protected]",
39 "enable": 1,
40 "expire": 0,
41 "firstname": "John",
42 "lastname": "Doe",
43 "groups": ["admins"],
44 "tokens": [{"expire": 0, "privsep": 0, "tokenid": "test"}],
45 "keys": "",
46 }
47
48 EXPECTED_USER = {
49 "userid": "testuser@pam",
50 "user": "testuser",
51 "domain": "pam",
52 "comment": "Test User",
53 "email": "[email protected]",
54 "enabled": True,
55 "expire": 0,
56 "firstname": "John",
57 "lastname": "Doe",
58 "groups": ["admins"],
59 "tokens": [{"expire": 0, "privsep": False, "tokenid": "test"}],
60 "keys": "",
61 }
62
63 TEST_SCENARIOS = [
64 {"args": {"userid": "testuser@pam"}, "expected": EXPECTED_USER},
65 {"args": {"user": "testuser"}, "expected": EXPECTED_USER},
66 {"args": {"domain": "pam"}, "expected": EXPECTED_USER},
67 {"args": {"userid": "nobody@pam"}, "expected": []},
68 ]
69
70 def setUp(self):
71 super().setUp()
72 proxmox_utils.HAS_PROXMOXER = True
73 self.module = proxmox_user_info
74
75 self.connect_mock = patch(
76 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
77 ).start()
78 self.connect_mock.return_value.proxmox_api.access.users.get.return_value = self.SAMPLE_USER
79
80 def tearDown(self):
81 self.connect_mock.stop()
82 super().tearDown()
83
84 def _create_module_args(self, **kwargs):
85 """Helper to create module arguments with defaults."""
86 args = self.BASIC_MODULE_ARGS.copy()
87 args.update(kwargs)
88 return args
89
90 def test_user_info(self):
91 """Test user info retrieval."""
92 for scenario in self.TEST_SCENARIOS:
93 with self.subTest(scenario=scenario):
94 module_args = self._create_module_args(**scenario["args"])
95
96 with pytest.raises(AnsibleExitJson) as exc_info, set_module_args(module_args):
97 self.module.main()
98
99 result = exc_info.value.args[0]
100 assert not result["changed"]
101 assert not result["failed"]
102 assert result["proxmox_users"] == [scenario["expected"]]