Completed
Branch devel (d9c4b1)
by Andreas
29s
created

TestPluginQueries.test_no_match_found_exception()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
1
# Make coding more python3-ish
2
from __future__ import (absolute_import, division, print_function)
3
__metaclass__ = type
4
5
from argparse import ArgumentParser
6
7
from ansible.compat.tests import unittest
8
from ansible.compat.tests.mock import patch
9
10
from ansible.errors import AnsibleError
11
from ansible.module_utils import six
12
from tpmstore.tpmstore import LookupModule
13
from tpm import TpmApiv4
14
from tpm import TPMException
15
from logging import getLogger
16
17
18
log = getLogger(__name__)
19
20
21
class TestPluginQueries(unittest.TestCase):
22
    
23
    def setUp(self):
24
        self.lookup_plugin = LookupModule()
25
        self.patcher = patch('tpm.TpmApiv4.__init__', return_value=None)
26
        self.tpm_init_mock = self.patcher.start()
27
    
28
    def tearDown(self):
29
        self.patcher.stop()
30
    
31
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}, {'id': 73}])
32
    def test_too_many_results_exception(self, tpm_list_mock):
33
        search_sring = "2result"
34
        exception_error = 'Found more then one match for the entry, please be more specific: {}'.format(search_sring)
35
        with self.assertRaises(AnsibleError) as context:
36
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring)])
37
        log.debug("context exception: {}".format(context.exception))
38
        self.assertTrue(exception_error in str(context.exception))
39
40
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[])
41
    def test_create_needs_projectid_exception(self, tpm_list_mock):
42
        search_sring = "noresult"
43
        exception_error = 'To create a complete new entry, project_id is mandatory.'
44
        with self.assertRaises(AnsibleError) as context:
45
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring), 'create=True'])
46
        log.debug("context exception: {}".format(context.exception))
47
        self.assertTrue(exception_error in str(context.exception))
48
49
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[])
50
    def test_no_match_found_exception(self, tpm_list_mock):
51
        search_sring = "noresult"
52
        exception_error = "Found no match for: {}".format(search_sring)
53
        with self.assertRaises(AnsibleError) as context:
54
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search_sring), 'create=False'])
55
        log.debug("context exception: {}".format(context.exception))
56
        self.assertTrue(exception_error in str(context.exception))
57
58 View Code Duplication
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
59
    @patch('tpm.TpmApiv4.create_password', side_effect=TPMException("Name already exists."))
60
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[])
61
    def test_exception_on_create(self, mock_search, mock_create, mock_show):
62
        exception_error = "Name already exists."
63
        tpmurl = 'https://foo.bar'
64
        tpmuser = 'thatsme'
65
        tpmpass = 'mysecret'
66
        search = 'A new Entry'
67
        project_id = '4'
68
        with self.assertRaises(AnsibleError) as context:
69
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search), 'create=True', 'project_id={}'.format(project_id)])
70
        log.debug("context exception: {}".format(context.exception))
71
        self.assertTrue(exception_error in str(context.exception))
72
73 View Code Duplication
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'})
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
74
    @patch('tpm.TpmApiv4.update_password', side_effect=TPMException("Can not update because of reasons."))
75
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}])
76
    def test_exception_on_update(self, mock_search, mock_create, mock_show):
77
        exception_error = "Can not update because of reasons."
78
        tpmurl = 'https://foo.bar'
79
        tpmuser = 'thatsme'
80
        tpmpass = 'mysecret'
81
        search = 'A new Entry'
82
        project_id = '4'
83
        with self.assertRaises(AnsibleError) as context:
84
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name={}'.format(search), 'create=True', 'project_id={}'.format(project_id)])
85
        log.debug("context exception: {}".format(context.exception))
86
        self.assertTrue(exception_error in str(context.exception))
87
88
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}])
89
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar'})
90
    def test_successful_default_result(self, tpm_show_mock, tpm_list_mock):
91
        unlock_reason = 'to unlock'
92
        result = self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'name=1result', 'reason={}'.format(unlock_reason)])
93
        self.assertEqual(self.tpm_init_mock.call_args[1].get('unlock_reason'), unlock_reason)
94
        self.assertEqual(result, ['foobar'])
95
96
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}])
97
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar'})   
98
    def test_successful_search_result(self, mock_show, mock_search):
99
        search = 'username:[foobar]'
100
        plugin_args = ['https://foo.bar', 'tpmuser', 'tpmass', 'search={}'.format(search)]
101
        result = self.lookup_plugin.run(plugin_args)
102
        self.assertEqual(mock_search.call_args[0][0], search)
103
104
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 42}])
105
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 42, 'password': 'foobar', 'username': 'bar'})
106
    def test_successful_return_value_result(self, mock_show, mock_search):
107
        plugin_args = ['https://foo.bar', 'tpmuser', 'tpmass', 'name=1result', 'return_value=username']
108
        result = self.lookup_plugin.run(plugin_args)
109
        self.assertEqual(result[0], 'bar')
110
111
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'A new Entry'})
112
    @patch('tpm.TpmApiv4.create_password', return_value={'id': 73})
113
    @patch('tpm.TpmApiv4.generate_password', return_value={'password': 'random secret'})
114
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[])
115
    def test_verify_all_values_passed_at_create(self, mock_search, mock_generate_pass, mock_create, mock_show):
116
        tpmurl = 'https://foo.bar'
117
        tpmuser = 'thatsme'
118
        tpmpass = 'mysecret'
119
        search = 'A new Entry'
120
        project_id = '4'
121
        random_password = "random secret"
122
        username = 'root'
123
        access_info = 'ssh://root@host'
124
        tags = 'root,ssh,aws,cloud'
125
        notes = 'Created by Ansible'
126
        reason = 'because I can'
127
        email = '[email protected]'
128
        expiry_date = "1983-04-25"
129
        result = self.lookup_plugin.run([tpmurl, tpmuser, tpmpass, 'name={}'.format(search),
130
                                         'create=True', 'project_id={}'.format(project_id), 'password=random',
131
                                         'username={}'.format(username), 'access_info={}'.format(access_info),
132
                                         'tags={}'.format(tags), 'notes={}'.format(notes), 'email={}'.format(email),
133
                                         'expiry_date={}'.format(expiry_date), 'reason={}'.format(reason)])
134
        self.assertEqual(self.tpm_init_mock.call_args[0][0], tpmurl, 'tpmurl not passed')
135
        self.assertEqual(self.tpm_init_mock.call_args[1].get('username'), tpmuser, 'tpmuser not passed')
136
        self.assertEqual(self.tpm_init_mock.call_args[1].get('password'), tpmpass, 'tpmpass not passed')
137
        self.assertEqual(self.tpm_init_mock.call_args[1].get('unlock_reason'), reason, 'unlock_reason not passed')
138
        self.assertEqual(mock_search.call_args[0][0], 'name:[{}]'.format(search), 'search not passed')
139
        self.assertEqual(mock_create.call_args[0][0].get('project_id'), project_id, 'project_id not passed')
140
        self.assertEqual(mock_create.call_args[0][0].get('expiry_date'), expiry_date, 'expiry_date not passed')
141
        self.assertEqual(mock_create.call_args[0][0].get('password'), random_password, 'random_password not passed')
142
        self.assertEqual(mock_create.call_args[0][0].get('username'), username, 'username passed')
143
        self.assertEqual(mock_create.call_args[0][0].get('access_info'), access_info, 'access_info not passed')
144
        self.assertEqual(mock_create.call_args[0][0].get('tags'), tags, 'tags not passed')
145
        self.assertEqual(mock_create.call_args[0][0].get('notes'), notes, 'notes not passed')
146
        self.assertEqual(mock_create.call_args[0][0].get('email'), email, 'email not passed')
147
148
149
    @patch('tpm.TpmApiv4.show_password', return_value={'id': 73, 'name': 'An old entry'})
150
    @patch('tpm.TpmApiv4.update_password', return_value={'id': 73})
151
    @patch('tpm.TpmApiv4.generate_password', return_value={'password': 'random secret'})
152
    @patch('tpm.TpmApiv4.list_passwords_search', return_value=[{'id': 73}])
153
    def test_verify_all_values_passed_at_update(self, mock_search, mock_generate_pass, mock_update_pass, mock_show):
154
        tpmurl = 'https://foo.bar'
155
        tpmuser = 'thatsme'
156
        tpmpass = 'mysecret'
157
        search = 'An old entry'
158
        project_id = '4'
159
        random_password = "random secret"
160
        username = 'root'
161
        access_info = 'ssh://root@host'
162
        tags = 'root,ssh,aws,cloud'
163
        notes = 'Created by Ansible'
164
        reason = 'because I can'
165
        email = '[email protected]'
166
        expiry_date = "1983-04-25"
167
        result = self.lookup_plugin.run([tpmurl, tpmuser, tpmpass, 'name={}'.format(search),
168
                                         'create=True', 'project_id={}'.format(project_id), 'password=random',
169
                                         'username={}'.format(username), 'access_info={}'.format(access_info),
170
                                         'tags={}'.format(tags), 'notes={}'.format(notes), 'email={}'.format(email),
171
                                         'expiry_date={}'.format(expiry_date), 'reason={}'.format(reason)])
172
        log.debug(mock_update_pass.call_args[0][0])
173
        self.assertEqual(self.tpm_init_mock.call_args[0][0], tpmurl)
174
        self.assertEqual(self.tpm_init_mock.call_args[1].get('username'), tpmuser)
175
        self.assertEqual(self.tpm_init_mock.call_args[1].get('password'), tpmpass)
176
        self.assertEqual(self.tpm_init_mock.call_args[1].get('unlock_reason'), reason)
177
        self.assertEqual(mock_search.call_args[0][0], 'name:[{}]'.format(search))
178
        self.assertEqual(mock_update_pass.call_args[0][0], 73)
179
        self.assertEqual(mock_update_pass.call_args[0][1].get('project_id'), project_id)
180
        self.assertEqual(mock_update_pass.call_args[0][1].get('expiry_date'), expiry_date)
181
        self.assertEqual(mock_update_pass.call_args[0][1].get('password'), random_password)
182
        self.assertEqual(mock_update_pass.call_args[0][1].get('username'), username)
183
        self.assertEqual(mock_update_pass.call_args[0][1].get('access_info'), access_info)
184
        self.assertEqual(mock_update_pass.call_args[0][1].get('tags'), tags)
185
        self.assertEqual(mock_update_pass.call_args[0][1].get('notes'), notes)
186
        self.assertEqual(mock_update_pass.call_args[0][1].get('email'), email)
187
188
189
class TestExceptions(unittest.TestCase):
190
191
    def setUp(self):
192
        self.lookup_plugin = LookupModule()
193
194
    def test_least_arguments_exception(self):
195
        exception_error = 'At least 4 arguments required.'
196
        with self.assertRaises(AnsibleError) as context:
197
            self.lookup_plugin.run(['tpmurl', 'tpmuser'])
198
        log.debug("context exception: {}".format(context.exception))
199
        self.assertTrue(exception_error in str(context.exception))
200
201
    def test_create_argument_exception(self):
202
        wrong_term = 'Foo'
203
        exception_error = "create can only be True or False and not: {}".format(wrong_term)
204
        with self.assertRaises(AnsibleError) as context:
205
            self.lookup_plugin.run(['tpmurl', 'tpmuser', 'tpmass', 'create={}'.format(wrong_term)])
206
        log.debug("context exception: {}".format(context.exception))
207
        self.assertTrue(exception_error in str(context.exception))
208
209
    def test_invalid_url_exception(self):
210
        wrong_url = 'ftp://foo.bar'
211
        exception_error = "First argument has to be a valid URL to TeamPasswordManager API: {}".format(wrong_url)
212
        with self.assertRaises(AnsibleError) as context:
213
            self.lookup_plugin.run(['{}'.format(wrong_url), 'tpmuser', 'tpmass', 'name=dostuff'])
214
        log.debug("context exception: {}".format(context.exception))
215
        self.assertTrue(exception_error in str(context.exception))
216
217
    def test_other_tpm_exception(self):
218
        wrong_url = 'https://does.not.exists.com'
219
        exception_error = "Connection error for "
220
        with self.assertRaises(AnsibleError) as context:
221
            self.lookup_plugin.run(['{}'.format(wrong_url), 'tpmuser', 'tpmass', 'name=dostuff'])
222
        log.debug("context exception: {}".format(context.exception))
223
        self.assertTrue(exception_error in str(context.exception))
224
225
    def test_no_name_or_search_set_exception(self):
226
        exception_error = 'Either "name" or "search" have to be set.'
227
        with self.assertRaises(AnsibleError) as context:
228
            self.lookup_plugin.run(['https://foo.bar', 'tpmuser', 'tpmass', 'return_value=foobar'])
229
        log.debug("context exception: {}".format(context.exception))
230
        self.assertTrue(exception_error in str(context.exception))
231
        
232