1from ansible.module_utils.source_control.bitbucket import BitbucketHelper
2from ansible.modules.source_control.bitbucket import bitbucket_pipeline_variable
3from units.compat import unittest
4from units.compat.mock import patch
5from units.modules.utils import AnsibleFailJson, AnsibleExitJson, ModuleTestCase, set_module_args
6
7
8class TestBucketPipelineVariableModule(ModuleTestCase):
9    def setUp(self):
10        super(TestBucketPipelineVariableModule, self).setUp()
11        self.module = bitbucket_pipeline_variable
12
13    def test_without_required_parameters(self):
14        with self.assertRaises(AnsibleFailJson) as exec_info:
15            set_module_args({
16                'username': 'name',
17                'repository': 'repo',
18                'name': 'PIPELINE_VAR_NAME',
19                'state': 'absent',
20            })
21            self.module.main()
22
23        self.assertEqual(exec_info.exception.args[0]['msg'], BitbucketHelper.error_messages['required_client_id'])
24
25    def test_missing_value_with_present_state(self):
26        with self.assertRaises(AnsibleFailJson) as exec_info:
27            set_module_args({
28                'client_id': 'ABC',
29                'client_secret': 'XXX',
30                'username': 'name',
31                'repository': 'repo',
32                'name': 'PIPELINE_VAR_NAME',
33                'state': 'present',
34            })
35            self.module.main()
36
37        self.assertEqual(exec_info.exception.args[0]['msg'], self.module.error_messages['required_value'])
38
39    @patch.dict('os.environ', {
40        'BITBUCKET_CLIENT_ID': 'ABC',
41        'BITBUCKET_CLIENT_SECRET': 'XXX',
42    })
43    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
44    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
45    def test_env_vars_params(self, *args):
46        with self.assertRaises(AnsibleExitJson):
47            set_module_args({
48                'username': 'name',
49                'repository': 'repo',
50                'name': 'PIPELINE_VAR_NAME',
51                'state': 'absent',
52            })
53            self.module.main()
54
55    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
56    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
57    def test_create_variable(self, *args):
58        with patch.object(self.module, 'create_pipeline_variable') as create_pipeline_variable_mock:
59            with self.assertRaises(AnsibleExitJson) as exec_info:
60                set_module_args({
61                    'client_id': 'ABC',
62                    'client_secret': 'XXX',
63                    'username': 'name',
64                    'repository': 'repo',
65                    'name': 'PIPELINE_VAR_NAME',
66                    'value': '42',
67                    'state': 'present',
68                })
69                self.module.main()
70
71            self.assertEqual(create_pipeline_variable_mock.call_count, 1)
72            self.assertEqual(exec_info.exception.args[0]['changed'], True)
73
74    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
75    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
76    def test_create_variable_check_mode(self, *args):
77        with patch.object(self.module, 'create_pipeline_variable') as create_pipeline_variable_mock:
78            with self.assertRaises(AnsibleExitJson) as exec_info:
79                set_module_args({
80                    'client_id': 'ABC',
81                    'client_secret': 'XXX',
82                    'username': 'name',
83                    'repository': 'repo',
84                    'name': 'PIPELINE_VAR_NAME',
85                    'value': '42',
86                    'state': 'present',
87                    '_ansible_check_mode': True,
88                })
89                self.module.main()
90
91            self.assertEqual(create_pipeline_variable_mock.call_count, 0)
92            self.assertEqual(exec_info.exception.args[0]['changed'], True)
93
94    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
95    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
96        'name': 'PIPELINE_VAR_NAME',
97        'value': 'Im alive',
98        'type': 'pipeline_variable',
99        'secured': False,
100        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
101    })
102    def test_update_variable(self, *args):
103        with patch.object(self.module, 'update_pipeline_variable') as update_pipeline_variable_mock:
104            with self.assertRaises(AnsibleExitJson) as exec_info:
105                set_module_args({
106                    'client_id': 'ABC',
107                    'client_secret': 'XXX',
108                    'username': 'name',
109                    'repository': 'repo',
110                    'name': 'PIPELINE_VAR_NAME',
111                    'value': '42',
112                    'state': 'present',
113                })
114                self.module.main()
115
116            self.assertEqual(update_pipeline_variable_mock.call_count, 1)
117            self.assertEqual(exec_info.exception.args[0]['changed'], True)
118
119    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
120    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
121        'name': 'PIPELINE_VAR_NAME',
122        'type': 'pipeline_variable',
123        'secured': True,
124        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
125    })
126    def test_update_secured_variable(self, *args):
127        with patch.object(self.module, 'update_pipeline_variable') as update_pipeline_variable_mock:
128            with self.assertRaises(AnsibleExitJson) as exec_info:
129                set_module_args({
130                    'client_id': 'ABC',
131                    'client_secret': 'XXX',
132                    'username': 'name',
133                    'repository': 'repo',
134                    'name': 'PIPELINE_VAR_NAME',
135                    'value': '42',
136                    'secured': True,
137                    'state': 'present',
138                })
139                self.module.main()
140
141            self.assertEqual(update_pipeline_variable_mock.call_count, 1)
142            self.assertEqual(exec_info.exception.args[0]['changed'], True)
143
144    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
145    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
146        'name': 'PIPELINE_VAR_NAME',
147        'value': '42',
148        'type': 'pipeline_variable',
149        'secured': False,
150        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
151    })
152    def test_update_secured_state(self, *args):
153        with patch.object(self.module, 'update_pipeline_variable') as update_pipeline_variable_mock:
154            with self.assertRaises(AnsibleExitJson) as exec_info:
155                set_module_args({
156                    'client_id': 'ABC',
157                    'client_secret': 'XXX',
158                    'username': 'name',
159                    'repository': 'repo',
160                    'name': 'PIPELINE_VAR_NAME',
161                    'value': '42',
162                    'secured': True,
163                    'state': 'present',
164                })
165                self.module.main()
166
167            self.assertEqual(update_pipeline_variable_mock.call_count, 1)
168            self.assertEqual(exec_info.exception.args[0]['changed'], True)
169
170    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
171    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
172        'name': 'PIPELINE_VAR_NAME',
173        'value': '42',
174        'type': 'pipeline_variable',
175        'secured': False,
176        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
177    })
178    def test_dont_update_same_value(self, *args):
179        with patch.object(self.module, 'update_pipeline_variable') as update_pipeline_variable_mock:
180            with self.assertRaises(AnsibleExitJson) as exec_info:
181                set_module_args({
182                    'client_id': 'ABC',
183                    'client_secret': 'XXX',
184                    'username': 'name',
185                    'repository': 'repo',
186                    'name': 'PIPELINE_VAR_NAME',
187                    'value': '42',
188                    'state': 'present',
189                })
190                self.module.main()
191
192            self.assertEqual(update_pipeline_variable_mock.call_count, 0)
193            self.assertEqual(exec_info.exception.args[0]['changed'], False)
194
195    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
196    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
197        'name': 'PIPELINE_VAR_NAME',
198        'value': 'Im alive',
199        'type': 'pipeline_variable',
200        'secured': False,
201        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
202    })
203    def test_update_variable_check_mode(self, *args):
204        with patch.object(self.module, 'update_pipeline_variable') as update_pipeline_variable_mock:
205            with self.assertRaises(AnsibleExitJson) as exec_info:
206                set_module_args({
207                    'client_id': 'ABC',
208                    'client_secret': 'XXX',
209                    'username': 'name',
210                    'repository': 'repo',
211                    'name': 'PIPELINE_VAR_NAME',
212                    'value': '42',
213                    'state': 'present',
214                    '_ansible_check_mode': True,
215                })
216                self.module.main()
217
218            self.assertEqual(update_pipeline_variable_mock.call_count, 0)
219            self.assertEqual(exec_info.exception.args[0]['changed'], True)
220
221    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
222    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
223        'name': 'PIPELINE_VAR_NAME',
224        'value': 'Im alive',
225        'type': 'pipeline_variable',
226        'secured': False,
227        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
228    })
229    def test_delete_variable(self, *args):
230        with patch.object(self.module, 'delete_pipeline_variable') as delete_pipeline_variable_mock:
231            with self.assertRaises(AnsibleExitJson) as exec_info:
232                set_module_args({
233                    'client_id': 'ABC',
234                    'client_secret': 'XXX',
235                    'username': 'name',
236                    'repository': 'repo',
237                    'name': 'PIPELINE_VAR_NAME',
238                    'state': 'absent',
239                })
240                self.module.main()
241
242            self.assertEqual(delete_pipeline_variable_mock.call_count, 1)
243            self.assertEqual(exec_info.exception.args[0]['changed'], True)
244
245    @patch.object(BitbucketHelper, 'fetch_access_token', return_value='token')
246    @patch.object(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value=None)
247    def test_delete_absent_variable(self, *args):
248        with patch.object(self.module, 'delete_pipeline_variable') as delete_pipeline_variable_mock:
249            with self.assertRaises(AnsibleExitJson) as exec_info:
250                set_module_args({
251                    'client_id': 'ABC',
252                    'client_secret': 'XXX',
253                    'username': 'name',
254                    'repository': 'repo',
255                    'name': 'PIPELINE_VAR_NAME',
256                    'state': 'absent',
257                })
258                self.module.main()
259
260            self.assertEqual(delete_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(bitbucket_pipeline_variable, 'get_existing_pipeline_variable', return_value={
265        'name': 'PIPELINE_VAR_NAME',
266        'value': 'Im alive',
267        'type': 'pipeline_variable',
268        'secured': False,
269        'uuid': '{9ddb0507-439a-495a- 99f3 - 564f15138127}'
270    })
271    def test_delete_variable_check_mode(self, *args):
272        with patch.object(self.module, 'delete_pipeline_variable') as delete_pipeline_variable_mock:
273            with self.assertRaises(AnsibleExitJson) as exec_info:
274                set_module_args({
275                    'client_id': 'ABC',
276                    'client_secret': 'XXX',
277                    'username': 'name',
278                    'repository': 'repo',
279                    'name': 'PIPELINE_VAR_NAME',
280                    'state': 'absent',
281                    '_ansible_check_mode': True,
282                })
283                self.module.main()
284
285            self.assertEqual(delete_pipeline_variable_mock.call_count, 0)
286            self.assertEqual(exec_info.exception.args[0]['changed'], True)
287
288
289if __name__ == '__main__':
290    unittest.main()
291