Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_ipa_otpconfig.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_otpconfig
18
19
20 @contextmanager
21 def patch_ipa(**kwargs):
22 """Mock context manager for patching the methods in OTPConfigIPAClient 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_otpconfig.OTPConfigIPAClient
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 TestIPAOTPConfig(ModuleTestCase):
42 def setUp(self):
43 super().setUp()
44 self.module = ipa_otpconfig
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 OTPConfigIPAClient.otpconfig_show, 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 otpconfig_show).
56
57 mock_calls (list/tuple of dicts):
58 List of calls made to OTPConfigIPAClient._post_json, in order.
59 _post_json is called by all of the otpconfig_* methods of the class.
60 Pass an empty list if no calls are expected.
61
62 changed (bool):
63 Whether or not the module is supposed to be marked as changed
64 """
65
66 # Run the module
67 with set_module_args(module_args):
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_set_all_no_adjustment(self):
91 """Set values requiring no adjustment"""
92 module_args = {
93 "ipatokentotpauthwindow": 11,
94 "ipatokentotpsyncwindow": 12,
95 "ipatokenhotpauthwindow": 13,
96 "ipatokenhotpsyncwindow": 14,
97 }
98 return_value = {
99 "ipatokentotpauthwindow": ["11"],
100 "ipatokentotpsyncwindow": ["12"],
101 "ipatokenhotpauthwindow": ["13"],
102 "ipatokenhotpsyncwindow": ["14"],
103 }
104 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
105 changed = False
106
107 self._test_base(module_args, return_value, mock_calls, changed)
108
109 def test_set_all_aliases_no_adjustment(self):
110 """Set values requiring no adjustment on all using aliases values"""
111 module_args = {"totpauthwindow": 11, "totpsyncwindow": 12, "hotpauthwindow": 13, "hotpsyncwindow": 14}
112 return_value = {
113 "ipatokentotpauthwindow": ["11"],
114 "ipatokentotpsyncwindow": ["12"],
115 "ipatokenhotpauthwindow": ["13"],
116 "ipatokenhotpsyncwindow": ["14"],
117 }
118 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
119 changed = False
120
121 self._test_base(module_args, return_value, mock_calls, changed)
122
123 def test_set_totp_auth_window_no_adjustment(self):
124 """Set values requiring no adjustment on totpauthwindow"""
125 module_args = {"totpauthwindow": 11}
126 return_value = {
127 "ipatokentotpauthwindow": ["11"],
128 "ipatokentotpsyncwindow": ["12"],
129 "ipatokenhotpauthwindow": ["13"],
130 "ipatokenhotpsyncwindow": ["14"],
131 }
132 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
133 changed = False
134
135 self._test_base(module_args, return_value, mock_calls, changed)
136
137 def test_set_totp_sync_window_no_adjustment(self):
138 """Set values requiring no adjustment on totpsyncwindow"""
139 module_args = {"totpsyncwindow": 12}
140 return_value = {
141 "ipatokentotpauthwindow": ["11"],
142 "ipatokentotpsyncwindow": ["12"],
143 "ipatokenhotpauthwindow": ["13"],
144 "ipatokenhotpsyncwindow": ["14"],
145 }
146 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
147 changed = False
148
149 self._test_base(module_args, return_value, mock_calls, changed)
150
151 def test_set_hotp_auth_window_no_adjustment(self):
152 """Set values requiring no adjustment on hotpauthwindow"""
153 module_args = {"hotpauthwindow": 13}
154 return_value = {
155 "ipatokentotpauthwindow": ["11"],
156 "ipatokentotpsyncwindow": ["12"],
157 "ipatokenhotpauthwindow": ["13"],
158 "ipatokenhotpsyncwindow": ["14"],
159 }
160 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
161 changed = False
162
163 self._test_base(module_args, return_value, mock_calls, changed)
164
165 def test_set_hotp_sync_window_no_adjustment(self):
166 """Set values requiring no adjustment on hotpsyncwindow"""
167 module_args = {"hotpsyncwindow": 14}
168 return_value = {
169 "ipatokentotpauthwindow": ["11"],
170 "ipatokentotpsyncwindow": ["12"],
171 "ipatokenhotpauthwindow": ["13"],
172 "ipatokenhotpsyncwindow": ["14"],
173 }
174 mock_calls = ({"method": "otpconfig_show", "name": None}, {"method": "otpconfig_show", "name": None})
175 changed = False
176
177 self._test_base(module_args, return_value, mock_calls, changed)
178
179 def test_set_totp_auth_window(self):
180 """Set values requiring adjustment on totpauthwindow"""
181 module_args = {"totpauthwindow": 10}
182 return_value = {
183 "ipatokentotpauthwindow": ["11"],
184 "ipatokentotpsyncwindow": ["12"],
185 "ipatokenhotpauthwindow": ["13"],
186 "ipatokenhotpsyncwindow": ["14"],
187 }
188 mock_calls = (
189 {"method": "otpconfig_show", "name": None},
190 {"method": "otpconfig_mod", "name": None, "item": {"ipatokentotpauthwindow": "10"}},
191 {"method": "otpconfig_show", "name": None},
192 )
193 changed = True
194
195 self._test_base(module_args, return_value, mock_calls, changed)
196
197 def test_set_totp_sync_window(self):
198 """Set values requiring adjustment on totpsyncwindow"""
199 module_args = {"totpsyncwindow": 10}
200 return_value = {
201 "ipatokentotpauthwindow": ["11"],
202 "ipatokentotpsyncwindow": ["12"],
203 "ipatokenhotpauthwindow": ["13"],
204 "ipatokenhotpsyncwindow": ["14"],
205 }
206 mock_calls = (
207 {"method": "otpconfig_show", "name": None},
208 {"method": "otpconfig_mod", "name": None, "item": {"ipatokentotpsyncwindow": "10"}},
209 {"method": "otpconfig_show", "name": None},
210 )
211 changed = True
212
213 self._test_base(module_args, return_value, mock_calls, changed)
214
215 def test_set_hotp_auth_window(self):
216 """Set values requiring adjustment on hotpauthwindow"""
217 module_args = {"hotpauthwindow": 10}
218 return_value = {
219 "ipatokentotpauthwindow": ["11"],
220 "ipatokentotpsyncwindow": ["12"],
221 "ipatokenhotpauthwindow": ["13"],
222 "ipatokenhotpsyncwindow": ["14"],
223 }
224 mock_calls = (
225 {"method": "otpconfig_show", "name": None},
226 {"method": "otpconfig_mod", "name": None, "item": {"ipatokenhotpauthwindow": "10"}},
227 {"method": "otpconfig_show", "name": None},
228 )
229 changed = True
230
231 self._test_base(module_args, return_value, mock_calls, changed)
232
233 def test_set_hotp_sync_window(self):
234 """Set values requiring adjustment on hotpsyncwindow"""
235 module_args = {"hotpsyncwindow": 10}
236 return_value = {
237 "ipatokentotpauthwindow": ["11"],
238 "ipatokentotpsyncwindow": ["12"],
239 "ipatokenhotpauthwindow": ["13"],
240 "ipatokenhotpsyncwindow": ["14"],
241 }
242 mock_calls = (
243 {"method": "otpconfig_show", "name": None},
244 {"method": "otpconfig_mod", "name": None, "item": {"ipatokenhotpsyncwindow": "10"}},
245 {"method": "otpconfig_show", "name": None},
246 )
247 changed = True
248
249 self._test_base(module_args, return_value, mock_calls, changed)
250
251 def test_set_all(self):
252 """Set values requiring adjustment on all"""
253 module_args = {
254 "ipatokentotpauthwindow": 11,
255 "ipatokentotpsyncwindow": 12,
256 "ipatokenhotpauthwindow": 13,
257 "ipatokenhotpsyncwindow": 14,
258 }
259 return_value = {
260 "ipatokentotpauthwindow": ["1"],
261 "ipatokentotpsyncwindow": ["2"],
262 "ipatokenhotpauthwindow": ["3"],
263 "ipatokenhotpsyncwindow": ["4"],
264 }
265 mock_calls = (
266 {"method": "otpconfig_show", "name": None},
267 {
268 "method": "otpconfig_mod",
269 "name": None,
270 "item": {
271 "ipatokentotpauthwindow": "11",
272 "ipatokentotpsyncwindow": "12",
273 "ipatokenhotpauthwindow": "13",
274 "ipatokenhotpsyncwindow": "14",
275 },
276 },
277 {"method": "otpconfig_show", "name": None},
278 )
279 changed = True
280
281 self._test_base(module_args, return_value, mock_calls, changed)
282
283 def test_fail_post(self):
284 """Fail due to an exception raised from _post_json"""
285 with set_module_args(
286 {
287 "ipatokentotpauthwindow": 11,
288 "ipatokentotpsyncwindow": 12,
289 "ipatokenhotpauthwindow": 13,
290 "ipatokenhotpsyncwindow": 14,
291 }
292 ):
293 with patch_ipa(side_effect=Exception("ERROR MESSAGE")) as (mock_login, mock_post):
294 with self.assertRaises(AnsibleFailJson) as exec_info:
295 self.module.main()
296
297 self.assertEqual(exec_info.exception.args[0]["msg"], "ERROR MESSAGE")