Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_ipa_pwpolicy.py

raw
1 # Copyright (c) 2020, Ansible Project
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 contextlib import contextmanager
8 from unittest.mock import call, patch
9
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 ipa_pwpolicy
18
19
20 @contextmanager
21 def patch_ipa(**kwargs):
22 """Mock context manager for patching the methods in PwPolicyIPAClient that contact the IPA server
23
24 Patches the `login` and `_post_json` methods
25
26 Keyword arguments are passed to the mock object that patches `_post_json`
27
28 No arguments are passed to the mock object that patches `login` because no tests require it
29
30 Example::
31
32 with patch_ipa(return_value={}) as (mock_login, mock_post):
33 ...
34 """
35 obj = ipa_pwpolicy.PwPolicyIPAClient
36 with patch.object(obj, "login") as mock_login:
37 with patch.object(obj, "_post_json", **kwargs) as mock_post:
38 yield mock_login, mock_post
39
40
41 class TestIPAPwPolicy(ModuleTestCase):
42 def setUp(self):
43 super().setUp()
44 self.module = ipa_pwpolicy
45
46 def _test_base(self, module_args, return_value, mock_calls, changed):
47 """Base function that's called by all the other test functions
48
49 module_args (dict):
50 Arguments passed to the module
51
52 return_value (dict):
53 Mocked return value of PwPolicyIPAClient.pwpolicy_find, as returned by the IPA API.
54 This should be set to the current state. It will be changed to the desired state using the above arguments.
55 (Technically, this is the return value of _post_json, but it's only checked by pwpolicy_find).
56 An empty dict means that the policy doesn't exist.
57
58 mock_calls (list/tuple of dicts):
59 List of calls made to PwPolicyIPAClient._post_json, in order.
60 _post_json is called by all of the pwpolicy_* methods of the class.
61 Pass an empty list if no calls are expected.
62
63 changed (bool):
64 Whether or not the module is supposed to be marked as changed
65 """
66 with set_module_args(module_args):
67 # Run the module
68 with patch_ipa(return_value=return_value) as (mock_login, mock_post):
69 with self.assertRaises(AnsibleExitJson) as exec_info:
70 self.module.main()
71
72 # Verify that the calls to _post_json match what is expected
73 expected_call_count = len(mock_calls)
74 if expected_call_count > 1:
75 # Convert the call dicts to unittest.mock.call instances because `assert_has_calls` only accepts them
76 converted_calls = []
77 for call_dict in mock_calls:
78 converted_calls.append(call(**call_dict))
79
80 mock_post.assert_has_calls(converted_calls)
81 self.assertEqual(len(mock_post.mock_calls), expected_call_count)
82 elif expected_call_count == 1:
83 mock_post.assert_called_once_with(**mock_calls[0])
84 else: # expected_call_count is 0
85 mock_post.assert_not_called()
86
87 # Verify that the module's changed status matches what is expected
88 self.assertIs(exec_info.exception.args[0]["changed"], changed)
89
90 def test_add(self):
91 """Add a new policy"""
92 module_args = {
93 "group": "admins",
94 "state": "present",
95 "priority": "10",
96 "maxpwdlife": "90",
97 "minpwdlife": "1",
98 "historylength": "8",
99 "minclasses": "3",
100 "minlength": "16",
101 "maxfailcount": "6",
102 "failinterval": "60",
103 "lockouttime": "600",
104 "gracelimit": 3,
105 "maxrepeat": 3,
106 "maxsequence": 3,
107 "dictcheck": True,
108 "usercheck": True,
109 }
110 return_value = {}
111 mock_calls = (
112 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "admins"}},
113 {
114 "method": "pwpolicy_add",
115 "name": "admins",
116 "item": {
117 "cospriority": "10",
118 "krbmaxpwdlife": "90",
119 "krbminpwdlife": "1",
120 "krbpwdhistorylength": "8",
121 "krbpwdmindiffchars": "3",
122 "krbpwdminlength": "16",
123 "krbpwdmaxfailure": "6",
124 "krbpwdfailurecountinterval": "60",
125 "krbpwdlockoutduration": "600",
126 "passwordgracelimit": "3",
127 "ipapwdmaxrepeat": "3",
128 "ipapwdmaxsequence": "3",
129 "ipapwddictcheck": True,
130 "ipapwdusercheck": True,
131 },
132 },
133 )
134 changed = True
135
136 self._test_base(module_args, return_value, mock_calls, changed)
137
138 def test_aliases(self):
139 """Same as test_add, but uses the `name` alias for the `group` option"""
140 module_args = {
141 "name": "admins",
142 "state": "present",
143 "priority": "10",
144 "maxpwdlife": "90",
145 "minpwdlife": "1",
146 "historylength": "8",
147 "minclasses": "3",
148 "minlength": "16",
149 "maxfailcount": "6",
150 "failinterval": "60",
151 "lockouttime": "600",
152 "gracelimit": 3,
153 "maxrepeat": 3,
154 "maxsequence": 3,
155 "dictcheck": True,
156 "usercheck": True,
157 }
158 return_value = {}
159 mock_calls = (
160 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "admins"}},
161 {
162 "method": "pwpolicy_add",
163 "name": "admins",
164 "item": {
165 "cospriority": "10",
166 "krbmaxpwdlife": "90",
167 "krbminpwdlife": "1",
168 "krbpwdhistorylength": "8",
169 "krbpwdmindiffchars": "3",
170 "krbpwdminlength": "16",
171 "krbpwdmaxfailure": "6",
172 "krbpwdfailurecountinterval": "60",
173 "krbpwdlockoutduration": "600",
174 "passwordgracelimit": "3",
175 "ipapwdmaxrepeat": "3",
176 "ipapwdmaxsequence": "3",
177 "ipapwddictcheck": True,
178 "ipapwdusercheck": True,
179 },
180 },
181 )
182 changed = True
183
184 self._test_base(module_args, return_value, mock_calls, changed)
185
186 def test_mod_different_args(self):
187 """Policy exists, but some of the args are different and need to be modified"""
188 module_args = {
189 "group": "sysops",
190 "state": "present",
191 "priority": "10",
192 "maxpwdlife": "60",
193 "minpwdlife": "24",
194 "historylength": "8",
195 "minclasses": "3",
196 "minlength": "12",
197 "maxfailcount": "8",
198 "failinterval": "60",
199 "lockouttime": "600",
200 "gracelimit": 3,
201 "maxrepeat": 3,
202 "maxsequence": 3,
203 "dictcheck": True,
204 "usercheck": True,
205 }
206 return_value = {
207 "cn": ["sysops"],
208 "cospriority": ["10"],
209 "krbmaxpwdlife": ["90"],
210 "krbminpwdlife": ["1"],
211 "krbpwdhistorylength": ["8"],
212 "krbpwdmindiffchars": ["3"],
213 "krbpwdminlength": ["16"],
214 "krbpwdmaxfailure": ["6"],
215 "krbpwdfailurecountinterval": ["60"],
216 "krbpwdlockoutduration": ["600"],
217 "passwordgracelimit": ["3"],
218 "ipapwdmaxrepeat": ["3"],
219 "ipapwdmaxsequence": ["3"],
220 "ipapwddictcheck": [True],
221 "ipapwdusercheck": [True],
222 "dn": "cn=sysops,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
223 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
224 }
225 mock_calls = (
226 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}},
227 {
228 "method": "pwpolicy_mod",
229 "name": "sysops",
230 "item": {
231 "cospriority": "10",
232 "krbmaxpwdlife": "60",
233 "krbminpwdlife": "24",
234 "krbpwdhistorylength": "8",
235 "krbpwdmindiffchars": "3",
236 "krbpwdminlength": "12",
237 "krbpwdmaxfailure": "8",
238 "krbpwdfailurecountinterval": "60",
239 "krbpwdlockoutduration": "600",
240 "passwordgracelimit": "3",
241 "ipapwdmaxrepeat": "3",
242 "ipapwdmaxsequence": "3",
243 "ipapwddictcheck": True,
244 "ipapwdusercheck": True,
245 },
246 },
247 )
248 changed = True
249
250 self._test_base(module_args, return_value, mock_calls, changed)
251
252 def test_mod_missing_args(self):
253 """Policy exists, but some of the args aren't set, so need to be added"""
254 module_args = {
255 "group": "sysops",
256 "state": "present",
257 "priority": "10",
258 "maxpwdlife": "90",
259 "minpwdlife": "1",
260 "historylength": "8",
261 "minclasses": "3",
262 "minlength": "16",
263 "maxfailcount": "6",
264 "failinterval": "60",
265 "lockouttime": "600",
266 "gracelimit": 3,
267 "maxrepeat": 3,
268 "maxsequence": 3,
269 "dictcheck": True,
270 "usercheck": True,
271 }
272 return_value = {
273 "cn": ["sysops"],
274 "cospriority": ["10"],
275 "krbmaxpwdlife": ["90"],
276 "krbpwdhistorylength": ["8"],
277 "krbpwdminlength": ["16"],
278 "krbpwdmaxfailure": ["6"],
279 "dn": "cn=sysops,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
280 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
281 }
282 mock_calls = (
283 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}},
284 {
285 "method": "pwpolicy_mod",
286 "name": "sysops",
287 "item": {
288 "cospriority": "10",
289 "krbmaxpwdlife": "90",
290 "krbminpwdlife": "1",
291 "krbpwdhistorylength": "8",
292 "krbpwdmindiffchars": "3",
293 "krbpwdminlength": "16",
294 "krbpwdmaxfailure": "6",
295 "krbpwdfailurecountinterval": "60",
296 "krbpwdlockoutduration": "600",
297 "passwordgracelimit": "3",
298 "ipapwdmaxrepeat": "3",
299 "ipapwdmaxsequence": "3",
300 "ipapwddictcheck": True,
301 "ipapwdusercheck": True,
302 },
303 },
304 )
305 changed = True
306
307 self._test_base(module_args, return_value, mock_calls, changed)
308
309 def test_del(self):
310 """Policy exists, and state is absent. Needs to be deleted"""
311 module_args = {
312 "group": "sysops",
313 "state": "absent",
314 # other arguments are ignored when state is `absent`
315 "priority": "10",
316 "maxpwdlife": "90",
317 "historylength": "8",
318 "minlength": "16",
319 "maxfailcount": "6",
320 }
321 return_value = {
322 "cn": ["sysops"],
323 "cospriority": ["10"],
324 "krbmaxpwdlife": ["90"],
325 "krbpwdhistorylength": ["8"],
326 "krbpwdminlength": ["16"],
327 "krbpwdmaxfailure": ["6"],
328 "dn": "cn=sysops,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
329 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
330 }
331 mock_calls = (
332 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}},
333 {
334 "method": "pwpolicy_del",
335 "name": "sysops",
336 },
337 )
338 changed = True
339
340 self._test_base(module_args, return_value, mock_calls, changed)
341
342 def test_no_change(self):
343 """Policy already exists. No changes needed"""
344 module_args = {
345 "group": "admins",
346 "state": "present",
347 "priority": "10",
348 "maxpwdlife": "90",
349 "minpwdlife": "1",
350 "historylength": "8",
351 "minclasses": "3",
352 "minlength": "16",
353 "maxfailcount": "6",
354 "failinterval": "60",
355 "lockouttime": "600",
356 "gracelimit": 3,
357 "maxrepeat": 3,
358 "maxsequence": 3,
359 "dictcheck": True,
360 "usercheck": True,
361 }
362 return_value = {
363 "cn": ["admins"],
364 "cospriority": ["10"],
365 "krbmaxpwdlife": ["90"],
366 "krbminpwdlife": ["1"],
367 "krbpwdhistorylength": ["8"],
368 "krbpwdmindiffchars": ["3"],
369 "krbpwdminlength": ["16"],
370 "krbpwdmaxfailure": ["6"],
371 "krbpwdfailurecountinterval": ["60"],
372 "krbpwdlockoutduration": ["600"],
373 "passwordgracelimit": ["3"],
374 "ipapwdmaxrepeat": ["3"],
375 "ipapwdmaxsequence": ["3"],
376 "ipapwddictcheck": [True],
377 "ipapwdusercheck": [True],
378 "dn": "cn=admins,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
379 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
380 }
381 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "admins"}}]
382 changed = False
383
384 self._test_base(module_args, return_value, mock_calls, changed)
385
386 def test_del_no_change(self):
387 """Policy doesn't exist, and state is absent. No change needed"""
388 module_args = {
389 "group": "sysops",
390 "state": "absent",
391 # other arguments are ignored when state is `absent`
392 "priority": "10",
393 "maxpwdlife": "90",
394 "historylength": "8",
395 "minlength": "16",
396 "maxfailcount": "6",
397 }
398 return_value = {}
399 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}}]
400 changed = False
401
402 self._test_base(module_args, return_value, mock_calls, changed)
403
404 def test_global(self):
405 """Modify the global policy"""
406 module_args = {
407 "maxpwdlife": "60",
408 "minpwdlife": "24",
409 "historylength": "8",
410 "minclasses": "3",
411 "minlength": "12",
412 "maxfailcount": "8",
413 "failinterval": "60",
414 "lockouttime": "600",
415 "gracelimit": 3,
416 "maxrepeat": 3,
417 "maxsequence": 3,
418 "dictcheck": True,
419 "usercheck": True,
420 }
421 return_value = {
422 "cn": ["global_policy"],
423 "krbmaxpwdlife": ["90"],
424 "krbminpwdlife": ["1"],
425 "krbpwdmindiffchars": ["3"],
426 "krbpwdminlength": ["16"],
427 "krbpwdmaxfailure": ["6"],
428 "krbpwdfailurecountinterval": ["60"],
429 "krbpwdlockoutduration": ["600"],
430 "passwordgracelimit": ["3"],
431 "ipapwdmaxrepeat": ["3"],
432 "ipapwdmaxsequence": ["3"],
433 "ipapwddictcheck": [True],
434 "ipapwdusercheck": [True],
435 "dn": "cn=global_policy,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
436 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
437 }
438 mock_calls = (
439 {"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "global_policy"}},
440 {
441 "method": "pwpolicy_mod",
442 "name": None,
443 "item": {
444 "krbmaxpwdlife": "60",
445 "krbminpwdlife": "24",
446 "krbpwdhistorylength": "8",
447 "krbpwdmindiffchars": "3",
448 "krbpwdminlength": "12",
449 "krbpwdmaxfailure": "8",
450 "krbpwdfailurecountinterval": "60",
451 "krbpwdlockoutduration": "600",
452 "passwordgracelimit": "3",
453 "ipapwdmaxrepeat": "3",
454 "ipapwdmaxsequence": "3",
455 "ipapwddictcheck": True,
456 "ipapwdusercheck": True,
457 },
458 },
459 )
460 changed = True
461
462 self._test_base(module_args, return_value, mock_calls, changed)
463
464 def test_global_no_change(self):
465 """Global policy already matches the given arguments. No change needed"""
466 module_args = {
467 "maxpwdlife": "90",
468 "minpwdlife": "1",
469 "historylength": "8",
470 "minclasses": "3",
471 "minlength": "16",
472 "maxfailcount": "6",
473 "failinterval": "60",
474 "lockouttime": "600",
475 "gracelimit": 3,
476 "maxrepeat": 3,
477 "maxsequence": 3,
478 "dictcheck": True,
479 "usercheck": True,
480 }
481 return_value = {
482 "cn": ["global_policy"],
483 "krbmaxpwdlife": ["90"],
484 "krbminpwdlife": ["1"],
485 "krbpwdhistorylength": ["8"],
486 "krbpwdmindiffchars": ["3"],
487 "krbpwdminlength": ["16"],
488 "krbpwdmaxfailure": ["6"],
489 "krbpwdfailurecountinterval": ["60"],
490 "krbpwdlockoutduration": ["600"],
491 "passwordgracelimit": ["3"],
492 "ipapwdmaxrepeat": ["3"],
493 "ipapwdmaxsequence": ["3"],
494 "ipapwddictcheck": [True],
495 "ipapwdusercheck": [True],
496 "dn": "cn=global_policy,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
497 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
498 }
499 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "global_policy"}}]
500 changed = False
501
502 self._test_base(module_args, return_value, mock_calls, changed)
503
504 def test_check_add(self):
505 """Add a new policy in check mode. pwpolicy_add shouldn't be called"""
506 module_args = {
507 "_ansible_check_mode": True,
508 "group": "admins",
509 "state": "present",
510 "priority": "10",
511 "maxpwdlife": "90",
512 "minpwdlife": "1",
513 "historylength": "8",
514 "minclasses": "3",
515 "minlength": "16",
516 "maxfailcount": "6",
517 "failinterval": "60",
518 "lockouttime": "600",
519 "gracelimit": 3,
520 "maxrepeat": 3,
521 "maxsequence": 3,
522 "dictcheck": True,
523 "usercheck": True,
524 }
525 return_value = {}
526 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "admins"}}]
527 changed = True
528
529 self._test_base(module_args, return_value, mock_calls, changed)
530
531 def test_check_mod(self):
532 """Modify a policy in check mode. pwpolicy_mod shouldn't be called"""
533 module_args = {
534 "_ansible_check_mode": True,
535 "group": "sysops",
536 "state": "present",
537 "priority": "10",
538 "maxpwdlife": "60",
539 "minpwdlife": "24",
540 "historylength": "8",
541 "minclasses": "3",
542 "minlength": "12",
543 "maxfailcount": "8",
544 "failinterval": "60",
545 "lockouttime": "600",
546 "gracelimit": 3,
547 "maxrepeat": 3,
548 "maxsequence": 3,
549 "dictcheck": True,
550 "usercheck": True,
551 }
552 return_value = {
553 "cn": ["sysops"],
554 "cospriority": ["10"],
555 "krbmaxpwdlife": ["90"],
556 "krbminpwdlife": ["1"],
557 "krbpwdhistorylength": ["8"],
558 "krbpwdmindiffchars": ["3"],
559 "krbpwdminlength": ["16"],
560 "krbpwdmaxfailure": ["6"],
561 "krbpwdfailurecountinterval": ["60"],
562 "krbpwdlockoutduration": ["600"],
563 "passwordgracelimit": ["3"],
564 "ipapwdmaxrepeat": ["3"],
565 "ipapwdmaxsequence": ["3"],
566 "ipapwddictcheck": [True],
567 "ipapwdusercheck": [True],
568 "dn": "cn=sysops,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
569 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
570 }
571 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}}]
572 changed = True
573
574 self._test_base(module_args, return_value, mock_calls, changed)
575
576 def test_check_del(self):
577 """Delete a policy in check mode. pwpolicy_del shouldn't be called"""
578 module_args = {"_ansible_check_mode": True, "group": "sysops", "state": "absent"}
579 return_value = {
580 "cn": ["sysops"],
581 "cospriority": ["10"],
582 "krbmaxpwdlife": ["90"],
583 "krbpwdhistorylength": ["8"],
584 "krbpwdminlength": ["16"],
585 "krbpwdmaxfailure": ["6"],
586 "dn": "cn=sysops,cn=EXAMPLE.COM,cn=kerberos,dc=example,dc=com",
587 "objectclass": ["top", "nscontainer", "krbpwdpolicy"],
588 }
589 mock_calls = [{"method": "pwpolicy_find", "name": None, "item": {"all": True, "cn": "sysops"}}]
590 changed = True
591
592 self._test_base(module_args, return_value, mock_calls, changed)
593
594 def test_fail_post(self):
595 """Fail due to an exception raised from _post_json"""
596 with set_module_args({"group": "admins", "state": "absent"}):
597 with patch_ipa(side_effect=Exception("ERROR MESSAGE")) as (mock_login, mock_post):
598 with self.assertRaises(AnsibleFailJson) as exec_info:
599 self.module.main()
600
601 self.assertEqual(exec_info.exception.args[0]["msg"], "ERROR MESSAGE")