Debian Codegraph @

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

raw
1 # Copyright (c) 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 unittest.mock import patch
8
9 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
10 AnsibleExitJson,
11 AnsibleFailJson,
12 ModuleTestCase,
13 set_module_args,
14 )
15
16 from ansible_collections.community.general.plugins.module_utils._bitbucket import BitbucketHelper
17 from ansible_collections.community.general.plugins.modules import bitbucket_pipeline_variable
18
19
20 class TestBucketPipelineVariableModule(ModuleTestCase):
21 def setUp(self):
22 super().setUp()
23 self.module = bitbucket_pipeline_variable
24
25 def test_without_required_parameters(self):
26 with self.assertRaises(AnsibleFailJson) as exec_info:
27 with set_module_args(
28 {
29 "workspace": "name",
30 "repository": "repo",
31 "name": "PIPELINE_VAR_NAME",
32 "state": "absent",
33 }
34 ):
35 self.module.main()
36
37 self.assertEqual(exec_info.exception.args[0]["failed"], True)
38
39 def test_missing_value_with_present_state(self):
40 with self.assertRaises(AnsibleFailJson) as exec_info:
41 with set_module_args(
42 {
43 "client_id": "ABC",
44 "client_secret": "XXX",
45 "workspace": "name",
46 "repository": "repo",
47 "name": "PIPELINE_VAR_NAME",
48 "state": "present",
49 }
50 ):
51 self.module.main()
52
53 self.assertEqual(exec_info.exception.args[0]["msg"], self.module.error_messages["required_value"])
54
55 @patch.dict(
56 "os.environ",
57 {
58 "BITBUCKET_CLIENT_ID": "ABC",
59 "BITBUCKET_CLIENT_SECRET": "XXX",
60 },
61 )
62 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
63 @patch.object(bitbucket_pipeline_variable, "get_existing_pipeline_variable", return_value=None)
64 def test_oauth_env_vars_params(self, *args):
65 with self.assertRaises(AnsibleExitJson):
66 with set_module_args(
67 {
68 "workspace": "name",
69 "repository": "repo",
70 "name": "PIPELINE_VAR_NAME",
71 "state": "absent",
72 }
73 ):
74 self.module.main()
75
76 @patch.dict(
77 "os.environ",
78 {
79 "BITBUCKET_USERNAME": "ABC",
80 "BITBUCKET_PASSWORD": "XXX",
81 },
82 )
83 @patch.object(bitbucket_pipeline_variable, "get_existing_pipeline_variable", return_value=None)
84 def test_basic_auth_env_vars_params(self, *args):
85 with self.assertRaises(AnsibleExitJson):
86 with set_module_args(
87 {
88 "workspace": "name",
89 "repository": "repo",
90 "name": "PIPELINE_VAR_NAME",
91 "state": "absent",
92 }
93 ):
94 self.module.main()
95
96 @patch.object(bitbucket_pipeline_variable, "get_existing_pipeline_variable", return_value=None)
97 def test_create_variable(self, *args):
98 with patch.object(self.module, "create_pipeline_variable") as create_pipeline_variable_mock:
99 with self.assertRaises(AnsibleExitJson) as exec_info:
100 with set_module_args(
101 {
102 "user": "ABC",
103 "password": "XXX",
104 "workspace": "name",
105 "repository": "repo",
106 "name": "PIPELINE_VAR_NAME",
107 "value": "42",
108 "state": "present",
109 }
110 ):
111 self.module.main()
112
113 self.assertEqual(create_pipeline_variable_mock.call_count, 1)
114 self.assertEqual(exec_info.exception.args[0]["changed"], True)
115
116 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
117 @patch.object(bitbucket_pipeline_variable, "get_existing_pipeline_variable", return_value=None)
118 def test_create_variable_check_mode(self, *args):
119 with patch.object(self.module, "create_pipeline_variable") as create_pipeline_variable_mock:
120 with self.assertRaises(AnsibleExitJson) as exec_info:
121 with set_module_args(
122 {
123 "client_id": "ABC",
124 "client_secret": "XXX",
125 "workspace": "name",
126 "repository": "repo",
127 "name": "PIPELINE_VAR_NAME",
128 "value": "42",
129 "state": "present",
130 "_ansible_check_mode": True,
131 }
132 ):
133 self.module.main()
134
135 self.assertEqual(create_pipeline_variable_mock.call_count, 0)
136 self.assertEqual(exec_info.exception.args[0]["changed"], True)
137
138 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
139 @patch.object(
140 bitbucket_pipeline_variable,
141 "get_existing_pipeline_variable",
142 return_value={
143 "name": "PIPELINE_VAR_NAME",
144 "value": "Im alive",
145 "type": "pipeline_variable",
146 "secured": False,
147 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
148 },
149 )
150 def test_update_variable(self, *args):
151 with patch.object(self.module, "update_pipeline_variable") as update_pipeline_variable_mock:
152 with self.assertRaises(AnsibleExitJson) as exec_info:
153 with set_module_args(
154 {
155 "client_id": "ABC",
156 "client_secret": "XXX",
157 "workspace": "name",
158 "repository": "repo",
159 "name": "PIPELINE_VAR_NAME",
160 "value": "42",
161 "state": "present",
162 }
163 ):
164 self.module.main()
165
166 self.assertEqual(update_pipeline_variable_mock.call_count, 1)
167 self.assertEqual(exec_info.exception.args[0]["changed"], True)
168
169 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
170 @patch.object(
171 bitbucket_pipeline_variable,
172 "get_existing_pipeline_variable",
173 return_value={
174 "name": "PIPELINE_VAR_NAME",
175 "type": "pipeline_variable",
176 "secured": True,
177 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
178 },
179 )
180 def test_update_secured_variable(self, *args):
181 with patch.object(self.module, "update_pipeline_variable") as update_pipeline_variable_mock:
182 with self.assertRaises(AnsibleExitJson) as exec_info:
183 with set_module_args(
184 {
185 "client_id": "ABC",
186 "client_secret": "XXX",
187 "workspace": "name",
188 "repository": "repo",
189 "name": "PIPELINE_VAR_NAME",
190 "value": "42",
191 "secured": True,
192 "state": "present",
193 }
194 ):
195 self.module.main()
196
197 self.assertEqual(update_pipeline_variable_mock.call_count, 1)
198 self.assertEqual(exec_info.exception.args[0]["changed"], True)
199
200 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
201 @patch.object(
202 bitbucket_pipeline_variable,
203 "get_existing_pipeline_variable",
204 return_value={
205 "name": "PIPELINE_VAR_NAME",
206 "value": "42",
207 "type": "pipeline_variable",
208 "secured": False,
209 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
210 },
211 )
212 def test_update_secured_state(self, *args):
213 with patch.object(self.module, "update_pipeline_variable") as update_pipeline_variable_mock:
214 with self.assertRaises(AnsibleExitJson) as exec_info:
215 with set_module_args(
216 {
217 "client_id": "ABC",
218 "client_secret": "XXX",
219 "workspace": "name",
220 "repository": "repo",
221 "name": "PIPELINE_VAR_NAME",
222 "value": "42",
223 "secured": True,
224 "state": "present",
225 }
226 ):
227 self.module.main()
228
229 self.assertEqual(update_pipeline_variable_mock.call_count, 1)
230 self.assertEqual(exec_info.exception.args[0]["changed"], True)
231
232 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
233 @patch.object(
234 bitbucket_pipeline_variable,
235 "get_existing_pipeline_variable",
236 return_value={
237 "name": "PIPELINE_VAR_NAME",
238 "value": "42",
239 "type": "pipeline_variable",
240 "secured": False,
241 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
242 },
243 )
244 def test_dont_update_same_value(self, *args):
245 with patch.object(self.module, "update_pipeline_variable") as update_pipeline_variable_mock:
246 with self.assertRaises(AnsibleExitJson) as exec_info:
247 with set_module_args(
248 {
249 "client_id": "ABC",
250 "client_secret": "XXX",
251 "workspace": "name",
252 "repository": "repo",
253 "name": "PIPELINE_VAR_NAME",
254 "value": "42",
255 "state": "present",
256 }
257 ):
258 self.module.main()
259
260 self.assertEqual(update_pipeline_variable_mock.call_count, 0)
261 self.assertEqual(exec_info.exception.args[0]["changed"], False)
262
263 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
264 @patch.object(
265 bitbucket_pipeline_variable,
266 "get_existing_pipeline_variable",
267 return_value={
268 "name": "PIPELINE_VAR_NAME",
269 "value": "Im alive",
270 "type": "pipeline_variable",
271 "secured": False,
272 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
273 },
274 )
275 def test_update_variable_check_mode(self, *args):
276 with patch.object(self.module, "update_pipeline_variable") as update_pipeline_variable_mock:
277 with self.assertRaises(AnsibleExitJson) as exec_info:
278 with set_module_args(
279 {
280 "client_id": "ABC",
281 "client_secret": "XXX",
282 "workspace": "name",
283 "repository": "repo",
284 "name": "PIPELINE_VAR_NAME",
285 "value": "42",
286 "state": "present",
287 "_ansible_check_mode": True,
288 }
289 ):
290 self.module.main()
291
292 self.assertEqual(update_pipeline_variable_mock.call_count, 0)
293 self.assertEqual(exec_info.exception.args[0]["changed"], True)
294
295 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
296 @patch.object(
297 bitbucket_pipeline_variable,
298 "get_existing_pipeline_variable",
299 return_value={
300 "name": "PIPELINE_VAR_NAME",
301 "value": "Im alive",
302 "type": "pipeline_variable",
303 "secured": False,
304 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
305 },
306 )
307 def test_delete_variable(self, *args):
308 with patch.object(self.module, "delete_pipeline_variable") as delete_pipeline_variable_mock:
309 with self.assertRaises(AnsibleExitJson) as exec_info:
310 with set_module_args(
311 {
312 "client_id": "ABC",
313 "client_secret": "XXX",
314 "workspace": "name",
315 "repository": "repo",
316 "name": "PIPELINE_VAR_NAME",
317 "state": "absent",
318 }
319 ):
320 self.module.main()
321
322 self.assertEqual(delete_pipeline_variable_mock.call_count, 1)
323 self.assertEqual(exec_info.exception.args[0]["changed"], True)
324
325 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
326 @patch.object(bitbucket_pipeline_variable, "get_existing_pipeline_variable", return_value=None)
327 def test_delete_absent_variable(self, *args):
328 with patch.object(self.module, "delete_pipeline_variable") as delete_pipeline_variable_mock:
329 with self.assertRaises(AnsibleExitJson) as exec_info:
330 with set_module_args(
331 {
332 "client_id": "ABC",
333 "client_secret": "XXX",
334 "workspace": "name",
335 "repository": "repo",
336 "name": "PIPELINE_VAR_NAME",
337 "state": "absent",
338 }
339 ):
340 self.module.main()
341
342 self.assertEqual(delete_pipeline_variable_mock.call_count, 0)
343 self.assertEqual(exec_info.exception.args[0]["changed"], False)
344
345 @patch.object(BitbucketHelper, "fetch_access_token", return_value="token")
346 @patch.object(
347 bitbucket_pipeline_variable,
348 "get_existing_pipeline_variable",
349 return_value={
350 "name": "PIPELINE_VAR_NAME",
351 "value": "Im alive",
352 "type": "pipeline_variable",
353 "secured": False,
354 "uuid": "{9ddb0507-439a-495a- 99f3 - 564f15138127}",
355 },
356 )
357 def test_delete_variable_check_mode(self, *args):
358 with patch.object(self.module, "delete_pipeline_variable") as delete_pipeline_variable_mock:
359 with self.assertRaises(AnsibleExitJson) as exec_info:
360 with set_module_args(
361 {
362 "client_id": "ABC",
363 "client_secret": "XXX",
364 "workspace": "name",
365 "repository": "repo",
366 "name": "PIPELINE_VAR_NAME",
367 "state": "absent",
368 "_ansible_check_mode": True,
369 }
370 ):
371 self.module.main()
372
373 self.assertEqual(delete_pipeline_variable_mock.call_count, 0)
374 self.assertEqual(exec_info.exception.args[0]["changed"], True)