Debian Codegraph @

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

raw
1 #
2 # Copyright (c) 2025, teslamania <[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 Mock, patch
8
9 import pytest
10 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
11 ModuleTestCase,
12 set_module_args,
13 )
14
15 import ansible_collections.community.proxmox.plugins.module_utils.proxmox as proxmox_utils
16 from ansible_collections.community.proxmox.plugins.modules import (
17 proxmox_domain,
18 )
19
20 proxmoxer = pytest.importorskip("proxmoxer")
21
22 RAW_DOMAINS = [
23 {"type": "pve", "comment": "Proxmox VE authentication server", "realm": "pve"},
24 {"realm": "ad", "comment": "comment test", "type": "ad"},
25 {"realm": "openid", "type": "openid"},
26 {"type": "pam", "realm": "pam", "comment": "Linux PAM standard authentication"},
27 {"realm": "example.test", "default": 1, "type": "ldap"},
28 ]
29
30 RAW_LDAP = {
31 "mode": "ldaps",
32 "sync-defaults-options": "enable-new=1,remove-vanished=acl;properties;entry,scope=both",
33 "user_attr": "uid",
34 "group_name_attr": "cn",
35 "default": 1,
36 "type": "ldap",
37 "base_dn": "cn=accounts,dc=example,dc=test",
38 "digest": "c21f05ca10e3c1c4f0a69095072d2b8a060fe83d",
39 "bind_dn": "uid=sa-proxmox,cn=users,cn=accounts,dc=example,dc=test",
40 "group_filter": "cn=admins-proxmox",
41 "server1": "ipa.example.test",
42 "filter": "memberof=cn=admins-proxmox,cn=groups,cn=accounts,dc=example,dc=test",
43 "verify": 0,
44 }
45
46 RAW_OPENID = {
47 "type": "openid",
48 "client-key": "keyoftheclient",
49 "digest": "e21f05c910e5c5c6f082b81a072d2b8b9d9a147c",
50 "issuer-url": "https://example.test/openid-server",
51 "client-id": "idoftheclient",
52 }
53
54 RAW_AD = {
55 "group_filter": "cn=admins-proxmox",
56 "digest": "91ac19ca27eaccc4a057ca97072dab7b9603aa19",
57 "filter": "memberof=cn=admins-proxmox,cn=groups,cn=accounts,dc=example,dc=test",
58 "comment": "ad domain",
59 "server1": "srv-ad.example.test",
60 "group_name_attr": "cn",
61 "mode": "ldap",
62 "domain": "ADDOMAINNAME",
63 "sync-defaults-options": "enable-new=1,remove-vanished=acl;properties;entry,scope=both",
64 "bind_dn": "uid=sa-ad,cn=users,cn=accounts,dc=example,dc=test",
65 "case-sensitive": 1,
66 "verify": 0,
67 "type": "ad",
68 }
69
70
71 def exit_json(*args, **kwargs):
72 """function to patch over exit_json;
73 package return data into an exception"""
74 if "changed" not in kwargs:
75 kwargs["changed"] = False
76 raise SystemExit(kwargs)
77
78
79 def fail_json(*args, **kwargs):
80 """function to patch over fail_json;
81 package return data into an exception"""
82 kwargs["failed"] = True
83 raise SystemExit(kwargs)
84
85
86 def build_base_arg(state, realm, check=False):
87 args = {
88 "api_user": "root@pam",
89 "api_password": "secret",
90 "api_host": "192.168.1.21",
91 "state": state,
92 "realm": realm,
93 }
94 if check:
95 args["_ansible_check_mode"] = True
96 return args
97
98
99 def build_ldap_arg(state, realm, check=False):
100 args = build_base_arg(state, realm, check)
101 args["ldap_base_dn"] = "cn=accounts,dc=example,dc=test"
102 args["ldap_bind_dn"] = "uid=sa-proxmox,cn=users,cn=accounts,dc=example,dc=test"
103 args["default"] = True
104 args["ldap_filter"] = "memberof=cn=admins-proxmox,cn=groups,cn=accounts,dc=example,dc=test"
105 args["ldap_group_filter"] = "cn=admins-proxmox"
106 args["ldap_group_name_attr"] = "cn"
107 args["ldap_mode"] = "ldaps"
108 args["ldap_password"] = "password"
109 args["ldap_primary_server"] = "ipa.example.test"
110 args["type"] = "ldap"
111 args["ldap_user_attr"] = "uid"
112 args["ldap_validate_certs"] = False
113 args["ldap_sync_defaults_options"] = {
114 "scope": "both",
115 "enable_new": True,
116 "remove_vanished": "acl;properties;entry",
117 }
118 return args
119
120
121 def build_openid_arg(state, realm, check=False):
122 args = build_base_arg(state, realm, check)
123 args["type"] = "openid"
124 args["openid_client_id"] = "idoftheclient"
125 args["openid_client_key"] = "keyoftheclient"
126 args["openid_issuer_url"] = "https://example.test/openid-server"
127 return args
128
129
130 def build_ad_arg(state, realm, check=False):
131 args = build_base_arg(state, realm, check)
132 args["type"] = "ad"
133 args["ad_group_filter"] = "cn=admins-proxmox"
134 args["ad_filter"] = "memberof=cn=admins-proxmox,cn=groups,cn=accounts,dc=example,dc=test"
135 args["comment"] = "ad domain"
136 args["ad_primary_server"] = "srv-ad.example.test"
137 args["ad_group_name_attr"] = "cn"
138 args["ad_mode"] = "ldap"
139 args["ad_domain"] = "ADDOMAINNAME"
140 args["ad_sync_defaults_options"] = {"scope": "both", "enable_new": True, "remove_vanished": "acl;properties;entry"}
141 args["ad_bind_dn"] = "uid=sa-ad,cn=users,cn=accounts,dc=example,dc=test"
142 args["ad_case_sensitive"] = True
143 args["ad_validate_certs"] = False
144 args["type"] = "ad"
145 return args
146
147
148 class TestProxmoxDomain(ModuleTestCase):
149 def setUp(self):
150 super().setUp()
151 proxmox_utils.HAS_PROXMOXER = True
152 self.module = proxmox_domain
153
154 self.fail_json_patcher = patch(
155 "ansible.module_utils.basic.AnsibleModule.fail_json", new=Mock(side_effect=fail_json)
156 )
157 self.exit_json_patcher = patch("ansible.module_utils.basic.AnsibleModule.exit_json", new=exit_json)
158
159 self.fail_json_mock = self.fail_json_patcher.start()
160 self.exit_json_patcher.start()
161
162 self.connect_mock = patch(
163 "ansible_collections.community.proxmox.plugins.module_utils.proxmox.ProxmoxAnsible._connect",
164 ).start()
165
166 self.mock_obj = self.connect_mock.return_value
167
168 def tearDown(self):
169 self.connect_mock.stop()
170 self.exit_json_patcher.stop()
171 self.fail_json_patcher.stop()
172 super().tearDown()
173
174 def test_add_domain_check_mode(self):
175 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
176
177 args = build_ldap_arg("present", "other_realm", True)
178 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
179 proxmox_domain.main()
180
181 result = exc_info.value.args[0]
182
183 assert result["changed"] is True
184 assert result["msg"] == "Domain other_realm would be added."
185
186 def test_add_ldap_domain(self):
187 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
188
189 args = build_ldap_arg("present", "other_realm", False)
190 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
191 proxmox_domain.main()
192
193 result = exc_info.value.args[0]
194
195 assert result["changed"] is True
196 assert result["msg"] == "Domain other_realm added."
197
198 def test_add_ldap_domain_idempotent(self):
199 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
200
201 args = build_ldap_arg("present", "example.test", False)
202 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
203 proxmox_domain.main()
204
205 result = exc_info.value.args[0]
206
207 assert result["msg"] == "Domain example.test already exists."
208 assert result["changed"] is False
209
210 def test_edit_ldap_domain_check_mode(self):
211 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
212
213 args = build_ldap_arg("present", "example.test", True)
214 args["ad_mode"] = "ldap"
215 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
216 proxmox_domain.main()
217
218 result = exc_info.value.args[0]
219
220 assert result["msg"] == "Domain example.test would be edited."
221 assert result["changed"] is True
222
223 def test_edit_ldap_domain(self):
224 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
225
226 args = build_ldap_arg("present", "example.test", False)
227 args["ad_mode"] = "ldap"
228 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
229 proxmox_domain.main()
230
231 result = exc_info.value.args[0]
232
233 assert result["msg"] == "Domain example.test edited."
234 assert result["changed"] is True
235
236 def test_del_domain_check_mode(self):
237 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
238
239 args = build_base_arg("absent", "example.test", True)
240 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
241 proxmox_domain.main()
242
243 result = exc_info.value.args[0]
244
245 assert result["msg"] == "Domain example.test would be deleted."
246 assert result["changed"] is True
247
248 def test_del_domain(self):
249 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
250
251 args = build_base_arg("absent", "example.test", False)
252 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
253 proxmox_domain.main()
254
255 result = exc_info.value.args[0]
256
257 assert result["msg"] == "Domain example.test deleted."
258 assert result["changed"] is True
259
260 def test_del_domain_not_present(self):
261 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_LDAP]
262
263 args = build_base_arg("absent", "other_realm", False)
264 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
265 proxmox_domain.main()
266
267 result = exc_info.value.args[0]
268
269 assert result["msg"] == "Domain other_realm not present."
270 assert result["changed"] is False
271
272 def test_add_openid_domain(self):
273 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_OPENID]
274
275 args = build_openid_arg("present", "openid2", False)
276 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
277 proxmox_domain.main()
278
279 result = exc_info.value.args[0]
280
281 assert result["msg"] == "Domain openid2 added."
282 assert result["changed"] is True
283
284 def test_add_openid_domain_idempotent(self):
285 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_OPENID]
286
287 args = build_openid_arg("present", "openid", False)
288 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
289 proxmox_domain.main()
290
291 result = exc_info.value.args[0]
292
293 assert result["msg"] == "Domain openid already exists."
294 assert result["changed"] is False
295
296 def test_edit_openid_domain(self):
297 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_OPENID]
298
299 args = build_openid_arg("present", "openid", False)
300 args["openid_issuer_url"] = "https://example.test/openid-server2"
301 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
302 proxmox_domain.main()
303
304 result = exc_info.value.args[0]
305
306 assert result["msg"] == "Domain openid edited."
307 assert result["changed"] is True
308
309 def test_add_ad_domain(self):
310 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_AD]
311
312 args = build_ad_arg("present", "second_ad", False)
313 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
314 proxmox_domain.main()
315
316 result = exc_info.value.args[0]
317
318 assert result["msg"] == "Domain second_ad added."
319 assert result["changed"] is True
320
321 def test_add_ad_domain_idempotent(self):
322 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_AD]
323
324 args = build_ad_arg("present", "ad", False)
325 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
326 proxmox_domain.main()
327
328 result = exc_info.value.args[0]
329
330 assert result["msg"] == "Domain ad already exists."
331 assert result["changed"] is False
332
333 def test_edit_ad_domain(self):
334 self.mock_obj.access.domains.get.side_effect = [RAW_DOMAINS, RAW_AD]
335
336 args = build_ad_arg("present", "ad", False)
337 args["ad_case_sensitive"] = False
338 with set_module_args(args), pytest.raises(SystemExit) as exc_info:
339 proxmox_domain.main()
340
341 result = exc_info.value.args[0]
342
343 assert result["msg"] == "Domain ad edited."
344 assert result["changed"] is True