1
|
|
|
import requests |
2
|
|
|
import requests_mock |
3
|
|
|
import unittest |
4
|
|
|
import os.path |
5
|
|
|
import tpm |
6
|
|
|
import json |
7
|
|
|
import logging |
8
|
|
|
import hmac |
9
|
|
|
import hashlib |
10
|
|
|
import time |
11
|
|
|
import random |
12
|
|
|
|
13
|
|
|
log = logging.getLogger(__name__) |
14
|
|
|
|
15
|
|
|
api_url = 'https://tpm.example.com/index.php/api/v4/' |
16
|
|
|
local_path = 'tests/resources/' |
17
|
|
|
|
18
|
|
|
item_limit = 20 |
19
|
|
|
|
20
|
|
|
def fake_data(url, m, altpath=False): |
21
|
|
|
""" |
22
|
|
|
A stub urlopen() implementation that load json responses from |
23
|
|
|
the filesystem. |
24
|
|
|
""" |
25
|
|
|
# Map path from url to a file |
26
|
|
|
path_parts = url.split('/')[6:] |
27
|
|
|
if altpath == False: |
28
|
|
|
path = '/'.join(path_parts) |
29
|
|
|
else: |
30
|
|
|
path = altpath |
31
|
|
|
resource_file = os.path.normpath('tests/resources/{}'.format(path)) |
32
|
|
|
with open(resource_file, 'r') as data_file: |
33
|
|
|
data_txt = data_file.read() |
34
|
|
|
|
35
|
|
|
data = json.loads(data_txt) |
36
|
|
|
data_len = len(data) |
37
|
|
|
log.debug('Data length: {}'.format(data_len)) |
38
|
|
|
|
39
|
|
|
# Must return a json-like object |
40
|
|
|
header = {} |
41
|
|
|
count = 0 |
42
|
|
|
while True: |
43
|
|
|
count += 1 |
44
|
|
|
if data_len > item_limit and isinstance(data,list): |
45
|
|
|
returndata = data[:item_limit] |
46
|
|
|
returndata_txt = json.dumps(returndata) |
47
|
|
|
data = data[item_limit:] |
48
|
|
|
data_txt = json.dumps(data) |
49
|
|
|
pageingurl = url.replace('.json', '/page/{}.json'.format(count)) |
50
|
|
|
log.debug("Registering URL: {}".format(pageingurl)) |
51
|
|
|
log.debug("Registering data: {}".format(returndata_txt)) |
52
|
|
|
log.debug("Data length: {}".format(len(returndata))) |
53
|
|
|
log.debug("Registering header: {}".format(header)) |
54
|
|
|
m.get(pageingurl, text=returndata_txt, headers=header.copy()) |
55
|
|
|
header = { 'link': '{}; rel="next"'.format(pageingurl)} |
56
|
|
|
data_len = len(data) |
57
|
|
|
else: |
58
|
|
|
log.debug("Registering URL: {}".format(url)) |
59
|
|
|
log.debug("Registering data: {}".format(data_txt)) |
60
|
|
|
log.debug("Registering header: {}".format(header)) |
61
|
|
|
m.get(url, text=data_txt, headers=header.copy()) |
62
|
|
|
header.clear() |
63
|
|
|
break |
64
|
|
|
|
65
|
|
|
class ClientProjectTestCase(unittest.TestCase): |
66
|
|
|
"""Test cases for all project related queries.""" |
67
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
68
|
|
|
# get all retrievable sample data |
69
|
|
|
path_to_mock = 'projects.json' |
70
|
|
|
request_url = api_url + path_to_mock |
71
|
|
|
with requests_mock.Mocker() as m: |
72
|
|
|
fake_data(request_url, m) |
73
|
|
|
global Projects |
74
|
|
|
Projects = client.list_projects() |
75
|
|
|
|
76
|
|
|
def setUp(self): |
77
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
78
|
|
|
|
79
|
|
|
def test_function_list_projects(self): |
80
|
|
|
"""Test function list_projects.""" |
81
|
|
|
path_to_mock = 'projects.json' |
82
|
|
|
request_url = api_url + path_to_mock |
83
|
|
|
request_path = local_path + path_to_mock |
84
|
|
|
resource_file = os.path.normpath(request_path) |
85
|
|
|
data_file = open(resource_file) |
86
|
|
|
data = json.load(data_file) |
87
|
|
|
with requests_mock.Mocker() as m: |
88
|
|
|
fake_data(request_url, m) |
89
|
|
|
response = self.client.list_projects() |
90
|
|
|
# number of passwords as from original json file. |
91
|
|
|
self.assertEqual(data, response) |
92
|
|
|
|
93
|
|
|
def test_function_list_projects_archived(self): |
94
|
|
|
"""Test function list_projects_archived.""" |
95
|
|
|
path_to_mock = 'projects/archived.json' |
96
|
|
|
request_url = api_url + path_to_mock |
97
|
|
|
request_path = local_path + path_to_mock |
98
|
|
|
resource_file = os.path.normpath(request_path) |
99
|
|
|
data_file = open(resource_file) |
100
|
|
|
data = json.load(data_file) |
101
|
|
|
with requests_mock.Mocker() as m: |
102
|
|
|
fake_data(request_url, m) |
103
|
|
|
response = self.client.list_projects_archived() |
104
|
|
|
# number of passwords as from original json file. |
105
|
|
|
self.assertEqual(data, response) |
106
|
|
|
|
107
|
|
|
def test_function_list_projects_favorite(self): |
108
|
|
|
"""Test function list_projects_favorite.""" |
109
|
|
|
path_to_mock = 'projects/favorite.json' |
110
|
|
|
request_url = api_url + path_to_mock |
111
|
|
|
request_path = local_path + path_to_mock |
112
|
|
|
resource_file = os.path.normpath(request_path) |
113
|
|
|
data_file = open(resource_file) |
114
|
|
|
data = json.load(data_file) |
115
|
|
|
with requests_mock.Mocker() as m: |
116
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
117
|
|
|
response = self.client.list_projects_favorite() |
118
|
|
|
# number of passwords as from original json file. |
119
|
|
|
self.assertEqual(data, response) |
120
|
|
|
|
121
|
|
|
def test_function_list_projects_search(self): |
122
|
|
|
"""Test function list_projects_search.""" |
123
|
|
|
searches = ['company', 'internal', 'website'] |
124
|
|
|
for search in searches: |
125
|
|
|
path_to_mock = 'projects/search/{}.json'.format(search) |
126
|
|
|
request_url = api_url + path_to_mock |
127
|
|
|
request_path = local_path + path_to_mock |
128
|
|
|
resource_file = os.path.normpath(request_path) |
129
|
|
|
data_file = open(resource_file) |
130
|
|
|
data = json.load(data_file) |
131
|
|
|
with requests_mock.Mocker() as m: |
132
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
133
|
|
|
response = self.client.list_projects_search(search) |
134
|
|
|
# number of passwords as from original json file. |
135
|
|
|
self.assertEqual(data, response) |
136
|
|
|
|
137
|
|
|
def test_function_show_project(self): |
138
|
|
|
"""Test function show_project.""" |
139
|
|
|
for project in Projects: |
140
|
|
|
project_id = project.get('id') |
141
|
|
|
log.debug("Testing with Project ID: {}".format(project_id)) |
142
|
|
|
path_to_mock = 'projects/{}.json'.format(project_id) |
143
|
|
|
request_url = api_url + path_to_mock |
144
|
|
|
request_path = local_path + path_to_mock |
145
|
|
|
resource_file = os.path.normpath(request_path) |
146
|
|
|
data_file = open(resource_file) |
147
|
|
|
data = json.load(data_file) |
148
|
|
|
with requests_mock.Mocker() as m: |
149
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
150
|
|
|
response = self.client.show_project(project_id) |
151
|
|
|
# number of passwords as from original json file. |
152
|
|
|
self.assertEqual(data, response) |
153
|
|
|
|
154
|
|
|
def test_function_list_passwords_of_project(self): |
155
|
|
|
"""Test function list_passwords_of_project.""" |
156
|
|
|
for project in Projects: |
157
|
|
|
project_id = project.get('id') |
158
|
|
|
log.debug("Testing with Project ID: {}".format(project_id)) |
159
|
|
|
path_to_mock = 'projects/{}/passwords.json'.format(project_id) |
160
|
|
|
request_url = api_url + path_to_mock |
161
|
|
|
request_path = local_path + path_to_mock |
162
|
|
|
resource_file = os.path.normpath(request_path) |
163
|
|
|
data_file = open(resource_file) |
164
|
|
|
data = json.load(data_file) |
165
|
|
|
with requests_mock.Mocker() as m: |
166
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
167
|
|
|
response = self.client.list_passwords_of_project(project_id) |
168
|
|
|
# number of passwords as from original json file. |
169
|
|
|
self.assertEqual(data, response) |
170
|
|
|
|
171
|
|
|
def test_function_list_user_access_on_project(self): |
172
|
|
|
"""Test function list_user_access_on_project.""" |
173
|
|
|
for project in Projects: |
174
|
|
|
project_id = project.get('id') |
175
|
|
|
log.debug("Testing with Project ID: {}".format(project_id)) |
176
|
|
|
path_to_mock = 'projects/{}/security.json'.format(project_id) |
177
|
|
|
request_url = api_url + path_to_mock |
178
|
|
|
request_path = local_path + path_to_mock |
179
|
|
|
resource_file = os.path.normpath(request_path) |
180
|
|
|
data_file = open(resource_file) |
181
|
|
|
data = json.load(data_file) |
182
|
|
|
with requests_mock.Mocker() as m: |
183
|
|
|
fake_data(request_url, m) |
184
|
|
|
response = self.client.list_user_access_on_project(project_id) |
185
|
|
|
# number of passwords as from original json file. |
186
|
|
|
self.assertEqual(data, response) |
187
|
|
|
|
188
|
|
|
def test_function_create_project(self): |
189
|
|
|
"""Test function create_project.""" |
190
|
|
|
path_to_mock = 'projects.json' |
191
|
|
|
request_url = api_url + path_to_mock |
192
|
|
|
return_data = { "id": 4 } |
193
|
|
|
create_data = { "name": "someproject"} |
194
|
|
|
with requests_mock.Mocker() as m: |
195
|
|
|
m.post(request_url, json=return_data, status_code=200) |
196
|
|
|
response = self.client.create_project(create_data) |
197
|
|
|
self.assertEqual(response, return_data.get('id')) |
198
|
|
|
|
199
|
|
|
def test_function_update_project(self): |
200
|
|
|
"""Test function update_project.""" |
201
|
|
|
path_to_mock = 'projects/4.json' |
202
|
|
|
request_url = api_url + path_to_mock |
203
|
|
|
update_data = { "name": "someproject"} |
204
|
|
|
with requests_mock.Mocker() as m: |
205
|
|
|
m.put(request_url, status_code=204) |
206
|
|
|
response = self.client.update_project('4', update_data) |
207
|
|
|
self.assertEqual(response, None) |
208
|
|
|
|
209
|
|
|
def test_function_change_parent_of_project(self): |
210
|
|
|
"""Test function change_parent_of_project.""" |
211
|
|
|
path_to_mock = 'projects/4/change_parent.json' |
212
|
|
|
request_url = api_url + path_to_mock |
213
|
|
|
with requests_mock.Mocker() as m: |
214
|
|
|
m.put(request_url, status_code=204) |
215
|
|
|
response = self.client.change_parent_of_project('4', '5') |
216
|
|
|
self.assertEqual(response, None) |
217
|
|
|
|
218
|
|
|
def test_function_update_security_of_project(self): |
219
|
|
|
"""Test function update_security_of_project.""" |
220
|
|
|
path_to_mock = 'projects/4/security.json' |
221
|
|
|
request_url = api_url + path_to_mock |
222
|
|
|
with requests_mock.Mocker() as m: |
223
|
|
|
m.put(request_url, status_code=204) |
224
|
|
|
response = self.client.update_security_of_project('4', {'name': 'setdata'}) |
225
|
|
|
self.assertEqual(response, None) |
226
|
|
|
|
227
|
|
|
def test_function_archive_project(self): |
228
|
|
|
"""Test function archive_project.""" |
229
|
|
|
path_to_mock = 'projects/4/archive.json' |
230
|
|
|
request_url = api_url + path_to_mock |
231
|
|
|
with requests_mock.Mocker() as m: |
232
|
|
|
m.put(request_url, status_code=204) |
233
|
|
|
response = self.client.archive_project('4') |
234
|
|
|
self.assertEqual(response, None) |
235
|
|
|
|
236
|
|
|
def test_function_delete_project(self): |
237
|
|
|
"""Test function delete_project.""" |
238
|
|
|
path_to_mock = 'projects/4.json' |
239
|
|
|
request_url = api_url + path_to_mock |
240
|
|
|
with requests_mock.Mocker() as m: |
241
|
|
|
m.delete(request_url, status_code=204) |
242
|
|
|
response = self.client.delete_project('4') |
243
|
|
|
self.assertEqual(response, None) |
244
|
|
|
|
245
|
|
|
def test_function_unarchive_project(self): |
246
|
|
|
"""Test function unarchive_project.""" |
247
|
|
|
path_to_mock = 'projects/4/unarchive.json' |
248
|
|
|
request_url = api_url + path_to_mock |
249
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
250
|
|
|
m.put(request_url, status_code=204) |
251
|
|
|
response = self.client.unarchive_project('4') |
252
|
|
|
self.assertEqual(response, None) |
253
|
|
|
|
254
|
|
|
def test_function_list_subprojects(self): |
255
|
|
|
"""Test function list_subprojects.""" |
256
|
|
|
for project in Projects: |
257
|
|
|
project_id = project.get('id') |
258
|
|
|
log.debug("Testing with Project ID: {}".format(project_id)) |
259
|
|
|
path_to_mock = 'projects/{}/subprojects.json'.format(project_id) |
260
|
|
|
request_url = api_url + path_to_mock |
261
|
|
|
request_path = local_path + path_to_mock |
262
|
|
|
resource_file = os.path.normpath(request_path) |
263
|
|
|
data_file = open(resource_file) |
264
|
|
|
data = json.load(data_file) |
265
|
|
|
with requests_mock.Mocker() as m: |
266
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
267
|
|
|
response = self.client.list_subprojects(project_id) |
268
|
|
|
# number of passwords as from original json file. |
269
|
|
|
self.assertEqual(data, response) |
270
|
|
|
|
271
|
|
|
def test_function_list_subprojects_action_new_pwd(self): |
272
|
|
|
"""Test function list_subprojects_action_new_pwd.""" |
273
|
|
|
action = 'new_pwd' |
274
|
|
|
for project in Projects: |
275
|
|
|
project_id = project.get('id') |
276
|
|
|
log.debug("Testing with Project ID: {}".format(project_id)) |
277
|
|
|
path_to_mock = 'projects/{}/subprojects/{}.json'.format(project_id, action) |
278
|
|
|
request_url = api_url + path_to_mock |
279
|
|
|
request_path = local_path + path_to_mock |
280
|
|
|
resource_file = os.path.normpath(request_path) |
281
|
|
|
data_file = open(resource_file) |
282
|
|
|
data = json.load(data_file) |
283
|
|
|
with requests_mock.Mocker() as m: |
284
|
|
|
fake_data(request_url, m) |
285
|
|
|
response = self.client.list_subprojects_action(project_id, action) |
286
|
|
|
# number of passwords as from original json file. |
287
|
|
|
self.assertEqual(data, response) |
288
|
|
|
|
289
|
|
|
class ClientPasswordTestCase(unittest.TestCase): |
290
|
|
|
"""Test cases for all password related queries.""" |
291
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
292
|
|
|
path_to_mock = 'passwords.json' |
293
|
|
|
request_url = api_url + path_to_mock |
294
|
|
|
with requests_mock.Mocker() as m: |
295
|
|
|
fake_data(request_url, m) |
296
|
|
|
global Passwords |
297
|
|
View Code Duplication |
Passwords = client.list_passwords() |
|
|
|
|
298
|
|
|
|
299
|
|
|
def setUp(self): |
300
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
301
|
|
|
|
302
|
|
|
def test_function_list_passwords(self): |
303
|
|
|
"""Test function list_passwords.""" |
304
|
|
|
path_to_mock = 'passwords.json' |
305
|
|
|
request_url = api_url + path_to_mock |
306
|
|
|
request_path = local_path + path_to_mock |
307
|
|
|
resource_file = os.path.normpath(request_path) |
308
|
|
|
data_file = open(resource_file) |
309
|
|
|
data = sorted(json.load(data_file), key=lambda k: k['id']) |
310
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
311
|
|
|
fake_data(request_url, m) |
312
|
|
|
response = sorted(self.client.list_passwords(), key=lambda k: k['id']) |
313
|
|
|
self.assertEqual(data, response) |
314
|
|
|
|
315
|
|
|
def test_function_list_passwords_archived(self): |
316
|
|
|
"""Test function list_passwords_archived.""" |
317
|
|
|
path_to_mock = 'passwords/archived.json' |
318
|
|
|
request_url = api_url + path_to_mock |
319
|
|
|
request_path = local_path + path_to_mock |
320
|
|
|
resource_file = os.path.normpath(request_path) |
321
|
|
|
data_file = open(resource_file) |
322
|
|
|
data = sorted(json.load(data_file), key=lambda k: k['id']) |
323
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
324
|
|
|
fake_data(request_url, m) |
325
|
|
|
response = sorted(self.client.list_passwords_archived(), key=lambda k: k['id']) |
326
|
|
|
self.assertEqual(data, response) |
327
|
|
|
|
328
|
|
|
def test_function_list_passwords_favorite(self): |
329
|
|
|
"""Test function list_passwords_favorite.""" |
330
|
|
|
path_to_mock = 'passwords/favorite.json' |
331
|
|
|
request_url = api_url + path_to_mock |
332
|
|
|
request_path = local_path + path_to_mock |
333
|
|
|
resource_file = os.path.normpath(request_path) |
334
|
|
|
data_file = open(resource_file) |
335
|
|
|
data = sorted(json.load(data_file), key=lambda k: k['id']) |
336
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
337
|
|
|
fake_data(request_url, m) |
338
|
|
|
response = sorted(self.client.list_passwords_favorite(), key=lambda k: k['id']) |
339
|
|
|
self.assertEqual(data, response) |
340
|
|
|
|
341
|
|
|
def test_function_list_passwords_search(self): |
342
|
|
|
"""Test function list_passwords_search.""" |
343
|
|
|
searches = ['backup', 'dns', 'facebook', 'firewall', 'reddit', 'test'] |
344
|
|
|
for search in searches: |
345
|
|
|
path_to_mock = 'passwords/search/{}.json'.format(search) |
346
|
|
|
request_url = api_url + path_to_mock |
347
|
|
|
request_path = local_path + path_to_mock |
348
|
|
|
resource_file = os.path.normpath(request_path) |
349
|
|
|
data_file = open(resource_file) |
350
|
|
|
data = json.load(data_file) |
351
|
|
|
with requests_mock.Mocker() as m: |
352
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
353
|
|
|
response = self.client.list_passwords_search(search) |
354
|
|
|
# number of passwords as from original json file. |
355
|
|
|
self.assertEqual(data, response) |
356
|
|
|
|
357
|
|
|
def test_function_show_password(self): |
358
|
|
|
"""Test function show_password.""" |
359
|
|
|
for password in Passwords: |
360
|
|
|
password_id = password.get('id') |
361
|
|
|
log.debug("Testing with Password ID: {}".format(password_id)) |
362
|
|
|
path_to_mock = 'passwords/{}.json'.format(password_id) |
363
|
|
|
request_url = api_url + path_to_mock |
364
|
|
|
request_path = local_path + path_to_mock |
365
|
|
|
resource_file = os.path.normpath(request_path) |
366
|
|
|
data_file = open(resource_file) |
367
|
|
|
data = json.load(data_file) |
368
|
|
|
with requests_mock.Mocker() as m: |
369
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
370
|
|
|
response = self.client.show_password(password_id) |
371
|
|
|
# number of passwords as from original json file. |
372
|
|
|
self.assertEqual(data, response) |
373
|
|
|
|
374
|
|
|
def test_function_list_user_access_on_password(self): |
375
|
|
|
"""Test function list_user_access_on_password.""" |
376
|
|
|
for password in Passwords: |
377
|
|
|
password_id = password.get('id') |
378
|
|
|
log.debug("Testing with Password ID: {}".format(password_id)) |
379
|
|
|
path_to_mock = 'passwords/{}/security.json'.format(password_id) |
380
|
|
|
request_url = api_url + path_to_mock |
381
|
|
|
request_path = local_path + path_to_mock |
382
|
|
|
resource_file = os.path.normpath(request_path) |
383
|
|
|
data_file = open(resource_file) |
384
|
|
|
data = json.load(data_file) |
385
|
|
|
with requests_mock.Mocker() as m: |
386
|
|
|
fake_data(request_url, m) |
387
|
|
|
response = self.client.list_user_access_on_password(password_id) |
388
|
|
|
# number of passwords as from original json file. |
389
|
|
|
self.assertEqual(data, response) |
390
|
|
|
|
391
|
|
|
def test_function_create_password(self): |
392
|
|
|
"""Test function create_password.""" |
393
|
|
|
path_to_mock = 'passwords.json' |
394
|
|
|
request_url = api_url + path_to_mock |
395
|
|
|
return_data = { "id": 4 } |
396
|
|
|
create_data = { "name": "someproject"} |
397
|
|
|
with requests_mock.Mocker() as m: |
398
|
|
|
m.post(request_url, json=return_data, status_code=200) |
399
|
|
|
response = self.client.create_password(create_data) |
400
|
|
|
self.assertEqual(response, return_data.get('id')) |
401
|
|
|
|
402
|
|
|
def test_function_update_password(self): |
403
|
|
|
"""Test function update_password.""" |
404
|
|
|
path_to_mock = 'passwords/4.json' |
405
|
|
|
request_url = api_url + path_to_mock |
406
|
|
|
update_data = { "name": "someproject"} |
407
|
|
|
with requests_mock.Mocker() as m: |
408
|
|
|
m.put(request_url, status_code=204) |
409
|
|
|
response = self.client.update_password('4', update_data) |
410
|
|
|
self.assertEqual(response, None) |
411
|
|
|
|
412
|
|
|
def test_function_update_security_of_password(self): |
413
|
|
|
"""Test function update_security_of_password.""" |
414
|
|
|
path_to_mock = 'passwords/4/security.json' |
415
|
|
|
request_url = api_url + path_to_mock |
416
|
|
|
with requests_mock.Mocker() as m: |
417
|
|
|
m.put(request_url, status_code=204) |
418
|
|
|
response = self.client.update_security_of_password('4', {'name': 'setdata'}) |
419
|
|
|
self.assertEqual(response, None) |
420
|
|
|
|
421
|
|
|
def test_function_update_custom_fields_of_password(self): |
422
|
|
|
"""Test function update_custom_fields_of_password.""" |
423
|
|
|
path_to_mock = 'passwords/4/custom_fields.json' |
424
|
|
|
request_url = api_url + path_to_mock |
425
|
|
|
with requests_mock.Mocker() as m: |
426
|
|
|
m.put(request_url, status_code=204) |
427
|
|
|
response = self.client.update_custom_fields_of_password('4', {'name': 'setdata'}) |
428
|
|
|
self.assertEqual(response, None) |
429
|
|
|
|
430
|
|
|
def test_function_delete_password(self): |
431
|
|
|
"""Test function delete_password.""" |
432
|
|
|
path_to_mock = 'passwords/4.json' |
433
|
|
|
request_url = api_url + path_to_mock |
434
|
|
|
with requests_mock.Mocker() as m: |
435
|
|
|
m.delete(request_url, status_code=204) |
436
|
|
|
response = self.client.delete_password('4') |
437
|
|
|
self.assertEqual(response, None) |
438
|
|
|
|
439
|
|
|
def test_function_lock_password(self): |
440
|
|
|
"""Test function lock_password.""" |
441
|
|
|
path_to_mock = 'passwords/4/lock.json' |
442
|
|
|
request_url = api_url + path_to_mock |
443
|
|
|
with requests_mock.Mocker() as m: |
444
|
|
|
m.put(request_url, status_code=204) |
445
|
|
|
response = self.client.lock_password('4') |
446
|
|
|
self.assertEqual(response, None) |
447
|
|
|
|
448
|
|
|
def test_function_unlock_password(self): |
449
|
|
|
"""Test function unlock_password.""" |
450
|
|
|
path_to_mock = 'passwords/4/unlock.json' |
451
|
|
|
request_url = api_url + path_to_mock |
452
|
|
|
unlock_reason = 'because I can' |
453
|
|
|
with requests_mock.Mocker() as m: |
454
|
|
|
m.put(request_url, status_code=204, request_headers={'X-Unlock-Reason': unlock_reason}) |
455
|
|
|
response = self.client.unlock_password('4', unlock_reason) |
456
|
|
|
self.assertEqual(response, None) |
457
|
|
|
|
458
|
|
|
class ClientMyPasswordTestCase(unittest.TestCase): |
459
|
|
|
"""Test cases for all mypassword related queries.""" |
460
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
461
|
|
|
path_to_mock = 'my_passwords.json' |
462
|
|
|
request_url = api_url + path_to_mock |
463
|
|
|
with requests_mock.Mocker() as m: |
464
|
|
|
fake_data(request_url, m) |
465
|
|
|
global MyPasswords |
466
|
|
|
MyPasswords = client.list_mypasswords() |
467
|
|
|
|
468
|
|
|
def setUp(self): |
469
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
470
|
|
|
|
471
|
|
|
def test_function_list_mypasswords(self): |
472
|
|
|
"""Test function list_mypasswords.""" |
473
|
|
|
path_to_mock = 'my_passwords.json' |
474
|
|
|
request_url = api_url + path_to_mock |
475
|
|
|
request_path = local_path + path_to_mock |
476
|
|
|
resource_file = os.path.normpath(request_path) |
477
|
|
|
data_file = open(resource_file) |
478
|
|
|
data = json.load(data_file) |
479
|
|
|
with requests_mock.Mocker() as m: |
480
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
481
|
|
|
response = self.client.list_mypasswords() |
482
|
|
|
# number of passwords as from original json file. |
483
|
|
|
self.assertEqual(data, response) |
484
|
|
|
|
485
|
|
|
def test_function_list_mypasswords_search(self): |
486
|
|
|
"""Test function list_mypasswords_search.""" |
487
|
|
|
searches = ['amazon', 'backup', 'facebook', 'john', 'jonny'] |
488
|
|
|
for search in searches: |
489
|
|
|
path_to_mock = 'my_passwords/search/{}.json'.format(search) |
490
|
|
|
request_url = api_url + path_to_mock |
491
|
|
|
request_path = local_path + path_to_mock |
492
|
|
|
resource_file = os.path.normpath(request_path) |
493
|
|
|
data_file = open(resource_file) |
494
|
|
|
data = json.load(data_file) |
495
|
|
|
with requests_mock.Mocker() as m: |
496
|
|
View Code Duplication |
fake_data(request_url, m) |
|
|
|
|
497
|
|
|
response = self.client.list_mypasswords_search(search) |
498
|
|
|
# number of passwords as from original json file. |
499
|
|
|
self.assertEqual(data, response) |
500
|
|
|
|
501
|
|
|
def test_function_show_mypassword(self): |
502
|
|
|
"""Test function show_mypassword.""" |
503
|
|
|
for password in MyPasswords: |
504
|
|
|
password_id = password.get('id') |
505
|
|
|
log.debug("Testing with Password ID: {}".format(password_id)) |
506
|
|
|
path_to_mock = 'my_passwords/{}.json'.format(password_id) |
507
|
|
|
request_url = api_url + path_to_mock |
508
|
|
|
request_path = local_path + path_to_mock |
509
|
|
|
resource_file = os.path.normpath(request_path) |
510
|
|
|
data_file = open(resource_file) |
511
|
|
|
data = json.load(data_file) |
512
|
|
|
with requests_mock.Mocker() as m: |
513
|
|
|
fake_data(request_url, m) |
514
|
|
|
response = self.client.show_mypassword(password_id) |
515
|
|
|
# number of passwords as from original json file. |
516
|
|
|
self.assertEqual(data, response) |
517
|
|
|
|
518
|
|
|
def test_function_create_mypassword(self): |
519
|
|
|
"""Test function create_mypassword.""" |
520
|
|
|
path_to_mock = 'my_passwords.json' |
521
|
|
|
request_url = api_url + path_to_mock |
522
|
|
|
return_data = { "id": 4 } |
523
|
|
|
create_data = { "name": "someproject"} |
524
|
|
|
with requests_mock.Mocker() as m: |
525
|
|
|
m.post(request_url, json=return_data, status_code=200) |
526
|
|
|
response = self.client.create_mypassword(create_data) |
527
|
|
|
self.assertEqual(response, return_data.get('id')) |
528
|
|
|
|
529
|
|
|
|
530
|
|
|
def test_function_update_mypassword(self): |
531
|
|
|
"""Test function update_mypassword.""" |
532
|
|
|
path_to_mock = 'my_passwords/4.json' |
533
|
|
|
request_url = api_url + path_to_mock |
534
|
|
|
update_data = { "name": "someproject"} |
535
|
|
|
with requests_mock.Mocker() as m: |
536
|
|
|
m.put(request_url, status_code=204) |
537
|
|
|
response = self.client.update_mypassword('4', update_data) |
538
|
|
|
self.assertEqual(response, None) |
539
|
|
|
|
540
|
|
|
def test_function_delete_mypassword(self): |
541
|
|
|
"""Test function delete_mypassword.""" |
542
|
|
|
path_to_mock = 'my_passwords/4.json' |
543
|
|
|
request_url = api_url + path_to_mock |
544
|
|
|
with requests_mock.Mocker() as m: |
545
|
|
|
m.delete(request_url, status_code=204) |
546
|
|
|
response = self.client.delete_mypassword('4') |
547
|
|
|
self.assertEqual(response, None) |
548
|
|
|
|
549
|
|
|
def test_function_set_favorite_password(self): |
550
|
|
|
"""Test function set_favorite_password.""" |
551
|
|
|
path_to_mock = 'favorite_passwords/4.json' |
552
|
|
|
request_url = api_url + path_to_mock |
553
|
|
|
with requests_mock.Mocker() as m: |
554
|
|
|
m.post(request_url, status_code=204) |
555
|
|
|
response = self.client.set_favorite_password('4') |
556
|
|
|
self.assertEqual(response, None) |
557
|
|
|
|
558
|
|
|
def test_function_unset_favorite_password(self): |
559
|
|
|
"""Test function unset_favorite_password.""" |
560
|
|
|
path_to_mock = 'favorite_passwords/4.json' |
561
|
|
|
request_url = api_url + path_to_mock |
562
|
|
|
with requests_mock.Mocker() as m: |
563
|
|
|
m.delete(request_url, status_code=204) |
564
|
|
|
response = self.client.unset_favorite_password('4') |
565
|
|
|
self.assertEqual(response, None) |
566
|
|
|
|
567
|
|
|
def test_function_set_favorite_project(self): |
568
|
|
|
"""Test function set_favorite_project.""" |
569
|
|
|
path_to_mock = 'favorite_project/4.json' |
570
|
|
|
request_url = api_url + path_to_mock |
571
|
|
|
with requests_mock.Mocker() as m: |
572
|
|
|
m.post(request_url, status_code=204) |
573
|
|
|
response = self.client.set_favorite_project('4') |
574
|
|
|
self.assertEqual(response, None) |
575
|
|
|
|
576
|
|
|
def test_function_unset_favorite_project(self): |
577
|
|
|
"""Test function unset_favorite_project.""" |
578
|
|
|
path_to_mock = 'favorite_project/4.json' |
579
|
|
|
request_url = api_url + path_to_mock |
580
|
|
|
with requests_mock.Mocker() as m: |
581
|
|
|
m.delete(request_url, status_code=204) |
582
|
|
|
response = self.client.unset_favorite_project('4') |
583
|
|
|
self.assertEqual(response, None) |
584
|
|
|
|
585
|
|
|
class ClientUsersTestCase(unittest.TestCase): |
586
|
|
|
"""Test cases for all user related queries.""" |
587
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
588
|
|
|
path_to_mock = 'users.json' |
589
|
|
|
request_url = api_url + path_to_mock |
590
|
|
|
with requests_mock.Mocker() as m: |
591
|
|
|
fake_data(request_url, m) |
592
|
|
|
global Users |
593
|
|
|
Users = client.list_users() |
594
|
|
|
|
595
|
|
|
def setUp(self): |
596
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
597
|
|
|
|
598
|
|
|
def test_function_list_users(self): |
599
|
|
|
"""Test function list_users.""" |
600
|
|
|
path_to_mock = 'users.json' |
601
|
|
|
request_url = api_url + path_to_mock |
602
|
|
|
request_path = local_path + path_to_mock |
603
|
|
|
resource_file = os.path.normpath(request_path) |
604
|
|
|
data_file = open(resource_file) |
605
|
|
|
data = json.load(data_file) |
606
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
607
|
|
|
fake_data(request_url, m) |
608
|
|
|
response = self.client.list_users() |
609
|
|
|
self.assertEqual(data, response) |
610
|
|
|
|
611
|
|
|
def test_function_show_user(self): |
612
|
|
|
"""Test function show_user.""" |
613
|
|
|
for user in Users: |
614
|
|
|
user_id = user.get('id') |
615
|
|
|
log.debug("Testing with Project ID: {}".format(user_id)) |
616
|
|
|
path_to_mock = 'users/{}.json'.format(user_id) |
617
|
|
|
request_url = api_url + path_to_mock |
618
|
|
|
request_path = local_path + path_to_mock |
619
|
|
|
resource_file = os.path.normpath(request_path) |
620
|
|
|
data_file = open(resource_file) |
621
|
|
|
data = json.load(data_file) |
622
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
623
|
|
|
fake_data(request_url, m) |
624
|
|
|
response = self.client.show_user(user_id) |
625
|
|
|
self.assertEqual(data, response) |
626
|
|
|
|
627
|
|
|
def test_function_show_me(self): |
628
|
|
|
"""Test function show_me.""" |
629
|
|
|
path_to_mock = 'users/me.json' |
630
|
|
|
request_url = api_url + path_to_mock |
631
|
|
|
request_path = local_path + path_to_mock |
632
|
|
|
resource_file = os.path.normpath(request_path) |
633
|
|
|
data_file = open(resource_file) |
634
|
|
|
data = json.load(data_file) |
635
|
|
|
with requests_mock.Mocker() as m: |
636
|
|
|
fake_data(request_url, m) |
637
|
|
|
response = self.client.show_me() |
638
|
|
|
response2 = self.client.who_am_i() |
639
|
|
|
self.assertEqual(data, response) |
640
|
|
|
self.assertEqual(response2, response) |
641
|
|
|
|
642
|
|
|
def test_function_create_user(self): |
643
|
|
|
"""Test function create_user.""" |
644
|
|
|
path_to_mock = 'users.json' |
645
|
|
|
request_url = api_url + path_to_mock |
646
|
|
|
return_data = { "id": 4 } |
647
|
|
|
create_data = { "name": "someuser"} |
648
|
|
|
with requests_mock.Mocker() as m: |
649
|
|
|
m.post(request_url, json=return_data, status_code=200) |
650
|
|
|
response = self.client.create_user(create_data) |
651
|
|
|
self.assertEqual(response, return_data.get('id')) |
652
|
|
|
|
653
|
|
|
def test_function_update_user(self): |
654
|
|
|
"""Test function update_user.""" |
655
|
|
|
path_to_mock = 'users/4.json' |
656
|
|
|
request_url = api_url + path_to_mock |
657
|
|
|
update_data = { "name": "someuser"} |
658
|
|
|
with requests_mock.Mocker() as m: |
659
|
|
|
m.put(request_url, status_code=204) |
660
|
|
|
response = self.client.update_user('4', update_data) |
661
|
|
|
self.assertEqual(response, None) |
662
|
|
|
|
663
|
|
|
def test_function_change_user_password(self): |
664
|
|
|
"""Test function change_user_password.""" |
665
|
|
|
path_to_mock = 'users/4/change_password.json' |
666
|
|
|
request_url = api_url + path_to_mock |
667
|
|
|
update_data = { "password": "NewSecret"} |
668
|
|
|
with requests_mock.Mocker() as m: |
669
|
|
|
m.put(request_url, status_code=204) |
670
|
|
|
response = self.client.change_user_password('4', update_data) |
671
|
|
|
self.assertEqual(response, None) |
672
|
|
|
|
673
|
|
|
def test_function_activate_user(self): |
674
|
|
|
"""Test function activate_user.""" |
675
|
|
|
path_to_mock = 'users/4/activate.json' |
676
|
|
|
request_url = api_url + path_to_mock |
677
|
|
|
with requests_mock.Mocker() as m: |
678
|
|
|
m.put(request_url, status_code=204) |
679
|
|
|
response = self.client.activate_user('4') |
680
|
|
|
self.assertEqual(response, None) |
681
|
|
|
|
682
|
|
|
def test_function_deactivate_user(self): |
683
|
|
|
"""Test function deactivate_user.""" |
684
|
|
|
path_to_mock = 'users/4/deactivate.json' |
685
|
|
|
request_url = api_url + path_to_mock |
686
|
|
|
with requests_mock.Mocker() as m: |
687
|
|
|
m.put(request_url, status_code=204) |
688
|
|
|
response = self.client.deactivate_user('4') |
689
|
|
|
self.assertEqual(response, None) |
690
|
|
|
|
691
|
|
|
def test_function_convert_user_to_ldap(self): |
692
|
|
|
"""Test function convert_user_to_ldap.""" |
693
|
|
|
path_to_mock = 'users/4/convert_to_ldap.json' |
694
|
|
|
request_url = api_url + path_to_mock |
695
|
|
|
login_dn = 'CN=Jane,CN=Users,DC=tpm,DC=local' |
696
|
|
|
def match_request_text(request): |
697
|
|
|
return {"login_dn": login_dn} |
698
|
|
|
with requests_mock.Mocker() as m: |
699
|
|
|
m.put(request_url, status_code=204, additional_matcher=match_request_text) |
700
|
|
|
response = self.client.convert_user_to_ldap('4', login_dn) |
701
|
|
|
self.assertEqual(response, None) |
702
|
|
|
|
703
|
|
|
def test_function_convert_ldap_user_to_normal(self): |
704
|
|
|
"""Test function convert_ldap_user_to_normal.""" |
705
|
|
|
path_to_mock = 'users/4/convert_to_normal.json' |
706
|
|
|
request_url = api_url + path_to_mock |
707
|
|
|
with requests_mock.Mocker() as m: |
708
|
|
|
m.put(request_url, status_code=204) |
709
|
|
|
response = self.client.convert_ldap_user_to_normal('4') |
710
|
|
|
self.assertEqual(response, None) |
711
|
|
|
|
712
|
|
|
def test_function_delete_user(self): |
713
|
|
|
"""Test function delete_user.""" |
714
|
|
|
path_to_mock = 'users/4.json' |
715
|
|
|
request_url = api_url + path_to_mock |
716
|
|
|
with requests_mock.Mocker() as m: |
717
|
|
|
m.delete(request_url, status_code=204) |
718
|
|
|
response = self.client.delete_user('4') |
719
|
|
|
self.assertEqual(response, None) |
720
|
|
|
|
721
|
|
|
class ClientGroupsTestCase(unittest.TestCase): |
722
|
|
|
"""Test cases for all group related queries.""" |
723
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
724
|
|
|
path_to_mock = 'groups.json' |
725
|
|
|
request_url = api_url + path_to_mock |
726
|
|
|
with requests_mock.Mocker() as m: |
727
|
|
|
fake_data(request_url, m) |
728
|
|
|
global Groups |
729
|
|
|
Groups = client.list_groups() |
730
|
|
|
|
731
|
|
|
def setUp(self): |
732
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
733
|
|
|
|
734
|
|
|
def test_function_list_groups(self): |
735
|
|
|
"""Test function list_groups.""" |
736
|
|
|
path_to_mock = 'groups.json' |
737
|
|
|
request_url = api_url + path_to_mock |
738
|
|
|
request_path = local_path + path_to_mock |
739
|
|
|
resource_file = os.path.normpath(request_path) |
740
|
|
|
data_file = open(resource_file) |
741
|
|
|
data = json.load(data_file) |
742
|
|
View Code Duplication |
with requests_mock.Mocker() as m: |
|
|
|
|
743
|
|
|
fake_data(request_url, m) |
744
|
|
|
response = self.client.list_groups() |
745
|
|
|
self.assertEqual(data, response) |
746
|
|
|
|
747
|
|
|
def test_function_show_group(self): |
748
|
|
|
"""Test function show_group.""" |
749
|
|
|
for group in Groups: |
750
|
|
|
group_id = group.get('id') |
751
|
|
|
log.debug("Testing with Project ID: {}".format(group_id)) |
752
|
|
|
path_to_mock = 'groups/{}.json'.format(group_id) |
753
|
|
|
request_url = api_url + path_to_mock |
754
|
|
|
request_path = local_path + path_to_mock |
755
|
|
|
resource_file = os.path.normpath(request_path) |
756
|
|
|
data_file = open(resource_file) |
757
|
|
|
data = json.load(data_file) |
758
|
|
|
with requests_mock.Mocker() as m: |
759
|
|
|
fake_data(request_url, m) |
760
|
|
|
response = self.client.show_group(group_id) |
761
|
|
|
self.assertEqual(data, response) |
762
|
|
|
|
763
|
|
|
def test_function_create_group(self): |
764
|
|
|
"""Test function create_group.""" |
765
|
|
|
path_to_mock = 'groups.json' |
766
|
|
|
request_url = api_url + path_to_mock |
767
|
|
|
return_data = { "id": 4 } |
768
|
|
|
create_data = { "name": "somegroup"} |
769
|
|
|
with requests_mock.Mocker() as m: |
770
|
|
|
m.post(request_url, json=return_data, status_code=200) |
771
|
|
|
response = self.client.create_group(create_data) |
772
|
|
|
self.assertEqual(response, return_data.get('id')) |
773
|
|
|
|
774
|
|
|
def test_function_update_group(self): |
775
|
|
|
"""Test function update_group.""" |
776
|
|
|
path_to_mock = 'groups/4.json' |
777
|
|
|
request_url = api_url + path_to_mock |
778
|
|
|
update_data = { "name": "somegroup"} |
779
|
|
|
with requests_mock.Mocker() as m: |
780
|
|
|
m.put(request_url, status_code=204) |
781
|
|
|
response = self.client.update_group('4', update_data) |
782
|
|
|
self.assertEqual(response, None) |
783
|
|
|
|
784
|
|
|
def test_function_add_user_to_group(self): |
785
|
|
|
"""Test function add_user_to_group.""" |
786
|
|
|
group_id = '3' |
787
|
|
|
user_id = '4' |
788
|
|
|
path_to_mock = 'groups/{}/add_user/{}.json'.format(group_id, user_id) |
789
|
|
|
request_url = api_url + path_to_mock |
790
|
|
|
with requests_mock.Mocker() as m: |
791
|
|
|
m.put(request_url, status_code=204) |
792
|
|
|
response = self.client.add_user_to_group(group_id, user_id) |
793
|
|
|
self.assertEqual(response, None) |
794
|
|
|
|
795
|
|
|
def test_function_delete_user_from_group(self): |
796
|
|
|
"""Test function delete_user_from_group.""" |
797
|
|
|
group_id = '3' |
798
|
|
|
user_id = '4' |
799
|
|
|
path_to_mock = 'groups/{}/delete_user/{}.json'.format(group_id, user_id) |
800
|
|
|
request_url = api_url + path_to_mock |
801
|
|
|
with requests_mock.Mocker() as m: |
802
|
|
|
m.put(request_url, status_code=204) |
803
|
|
|
response = self.client.delete_user_from_group(group_id, user_id) |
804
|
|
|
self.assertEqual(response, None) |
805
|
|
|
|
806
|
|
|
def test_function_delete_group(self): |
807
|
|
|
"""Test function delete_group.""" |
808
|
|
|
path_to_mock = 'groups/4.json' |
809
|
|
|
request_url = api_url + path_to_mock |
810
|
|
|
with requests_mock.Mocker() as m: |
811
|
|
|
m.delete(request_url, status_code=204) |
812
|
|
|
response = self.client.delete_group('4') |
813
|
|
|
self.assertEqual(response, None) |
814
|
|
|
|
815
|
|
|
class GeneralClientTestCases(unittest.TestCase): |
816
|
|
|
"""general test cases for client queries.""" |
817
|
|
|
def setUp(self): |
818
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
819
|
|
|
|
820
|
|
|
def test_paging(self): |
821
|
|
|
"""Test paging, if number of items is same as from original data source.""" |
822
|
|
|
path_to_mock = 'passwords.json' |
823
|
|
|
request_url = api_url + path_to_mock |
824
|
|
|
request_path = local_path + path_to_mock |
825
|
|
|
resource_file = os.path.normpath(request_path) |
826
|
|
|
data_file = open(resource_file) |
827
|
|
|
data = json.load(data_file) |
828
|
|
|
with requests_mock.Mocker() as m: |
829
|
|
|
fake_data(request_url, m) |
830
|
|
|
response = self.client.list_passwords() |
831
|
|
|
# number of passwords as from original json file. |
832
|
|
|
source_items = len(data) |
833
|
|
|
paging_count = round((source_items / 20) + 0.5) |
834
|
|
|
response_items = len(response) |
835
|
|
|
log.debug("Paging should be {}".format(paging_count)) |
836
|
|
|
log.debug("Paged {} times".format(m.call_count)) |
837
|
|
|
log.debug("Source Items: {}; Response Items: {}".format(source_items, response_items)) |
838
|
|
|
self.assertEqual(paging_count, m.call_count) |
839
|
|
|
self.assertEqual(source_items, response_items) |
840
|
|
|
|
841
|
|
|
def test_provide_unlock_reason(self): |
842
|
|
|
"""Test providing an unlock reason.""" |
843
|
|
|
path_to_mock = 'passwords/14.json' |
844
|
|
|
request_url = api_url + path_to_mock |
845
|
|
|
request_path = local_path + path_to_mock |
846
|
|
|
resource_file = os.path.normpath(request_path) |
847
|
|
|
data_file = open(resource_file) |
848
|
|
|
data = json.load(data_file) |
849
|
|
|
unlock_reason = 'because I can' |
850
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS', unlock_reason=unlock_reason) |
851
|
|
|
with requests_mock.Mocker() as m: |
852
|
|
|
m.get(request_url, request_headers={'X-Unlock-Reason': unlock_reason}) |
853
|
|
|
response = client.show_password('14') |
854
|
|
|
history = m.request_history |
855
|
|
|
request_unlock_reason = history[0].headers.get('X-Unlock-Reason') |
856
|
|
|
self.assertEqual(request_unlock_reason, unlock_reason) |
857
|
|
|
|
858
|
|
|
def test_key_authentciation(self): |
859
|
|
|
"""Test Key authentication header.""" |
860
|
|
|
private_key='private_secret' |
861
|
|
|
public_key='public_secret' |
862
|
|
|
client = tpm.TpmApiv4('https://tpm.example.com', private_key=private_key, public_key=public_key) |
863
|
|
|
path_to_mock = 'version.json' |
864
|
|
|
request_url = api_url + path_to_mock |
865
|
|
|
with requests_mock.Mocker() as m: |
866
|
|
|
fake_data(request_url, m) |
867
|
|
|
response = client.get_version() |
868
|
|
|
history = m.request_history |
869
|
|
|
request_hash = history[0].headers.get('X-Request-Hash') |
870
|
|
|
request_pubkey = history[0].headers.get('X-Public-Key') |
871
|
|
|
request_timestamp = history[0].headers.get('X-Request-Timestamp') |
872
|
|
|
timestamp = str(int(time.time())) |
873
|
|
|
unhashed = 'api/v4/' + path_to_mock + request_timestamp |
874
|
|
|
hashed = hmac.new(str.encode(private_key), |
875
|
|
|
msg=unhashed.encode('utf-8'), |
876
|
|
|
digestmod=hashlib.sha256).hexdigest() |
877
|
|
|
self.assertEqual(request_hash, hashed) |
878
|
|
|
|
879
|
|
|
def test_function_generate_password(self): |
880
|
|
|
"""Test function generate_password.""" |
881
|
|
|
path_to_mock = 'generate_password.json' |
882
|
|
|
request_url = api_url + path_to_mock |
883
|
|
|
request_path = local_path + path_to_mock |
884
|
|
|
resource_file = os.path.normpath(request_path) |
885
|
|
|
data_file = open(resource_file) |
886
|
|
|
data = json.load(data_file) |
887
|
|
|
with requests_mock.Mocker() as m: |
888
|
|
|
fake_data(request_url, m) |
889
|
|
|
response = self.client.generate_password() |
890
|
|
|
self.assertEqual(data, response) |
891
|
|
|
|
892
|
|
|
def test_get_version(self): |
893
|
|
|
"""Test function get_version.""" |
894
|
|
|
path_to_mock = 'version.json' |
895
|
|
|
request_url = api_url + path_to_mock |
896
|
|
|
request_path = local_path + path_to_mock |
897
|
|
|
resource_file = os.path.normpath(request_path) |
898
|
|
|
data_file = open(resource_file) |
899
|
|
|
data = json.load(data_file) |
900
|
|
|
with requests_mock.Mocker() as m: |
901
|
|
|
fake_data(request_url, m) |
902
|
|
|
response = self.client.get_version() |
903
|
|
|
self.assertEqual(data, response) |
904
|
|
|
|
905
|
|
|
def test_a_call_with_v3(self): |
906
|
|
|
"""Test function get_version with v3 API.""" |
907
|
|
|
path_to_mock = 'version.json' |
908
|
|
|
request_url = 'https://tpm.example.com/index.php/api/v3/' + path_to_mock |
909
|
|
|
request_path = local_path + path_to_mock |
910
|
|
|
resource_file = os.path.normpath(request_path) |
911
|
|
|
data_file = open(resource_file) |
912
|
|
|
data = json.load(data_file) |
913
|
|
|
client = tpm.TpmApiv3('https://tpm.example.com', username='USER', password='PASS') |
914
|
|
|
with requests_mock.Mocker() as m: |
915
|
|
|
fake_data(request_url, m) |
916
|
|
|
response = client.get_version() |
917
|
|
|
self.assertEqual(data, response) |
918
|
|
|
|
919
|
|
|
def test_function_check_latest(self): |
920
|
|
|
"""Test function generate_password.""" |
921
|
|
|
path_to_mock = 'version/check_latest.json' |
922
|
|
|
request_url = api_url + path_to_mock |
923
|
|
|
request_path = local_path + path_to_mock |
924
|
|
|
resource_file = os.path.normpath(request_path) |
925
|
|
|
data_file = open(resource_file) |
926
|
|
|
data = json.load(data_file) |
927
|
|
|
with requests_mock.Mocker() as m: |
928
|
|
|
fake_data(request_url, m) |
929
|
|
|
response = self.client.get_latest_version() |
930
|
|
|
self.assertEqual(data, response) |
931
|
|
|
|
932
|
|
|
def test_function_up_to_date_true(self): |
933
|
|
|
"""Test function up_to_date is true.""" |
934
|
|
|
path_to_mock = 'version/check_latest.json' |
935
|
|
|
request_url = api_url + path_to_mock |
936
|
|
|
request_path = local_path + path_to_mock |
937
|
|
|
resource_file = os.path.normpath(request_path) |
938
|
|
|
data_file = open(resource_file) |
939
|
|
|
data = json.load(data_file) |
940
|
|
|
with requests_mock.Mocker() as m: |
941
|
|
|
fake_data(request_url, m) |
942
|
|
|
response_up_to_date_true = self.client.up_to_date() |
943
|
|
|
self.assertTrue(response_up_to_date_true) |
944
|
|
|
|
945
|
|
|
def test_function_up_to_date_false(self): |
946
|
|
|
"""Test function up_to_date is false.""" |
947
|
|
|
path_to_mock = 'version/check_latest.json' |
948
|
|
|
request_url = api_url + path_to_mock |
949
|
|
|
with requests_mock.Mocker() as m: |
950
|
|
|
fake_data(request_url, m, 'version/check_outdated.json') |
951
|
|
|
response_up_to_date_false = self.client.up_to_date() |
952
|
|
|
self.assertFalse(response_up_to_date_false) |
953
|
|
|
|
954
|
|
|
|
955
|
|
|
class ExceptionTestCases(unittest.TestCase): |
956
|
|
|
"""Test case for Config Exceptions.""" |
957
|
|
|
def test_wrong_auth_exception1(self): |
958
|
|
|
"""Exception if wrong authentication mehtod with username but private_key.""" |
959
|
|
|
with self.assertRaises(tpm.TpmApi.ConfigError) as context: |
960
|
|
|
tpm.TpmApiv4('https://tpm.example.com', username='USER', private_key='PASS') |
961
|
|
|
log.debug("context exception: {}".format(context.exception)) |
962
|
|
|
self.assertEqual("'No authentication specified (user/password or private/public key)'", str(context.exception)) |
963
|
|
|
|
964
|
|
|
def test_wrong_auth_exception2(self): |
965
|
|
|
"""Exception if wrong authentication mehtod with public key but password.""" |
966
|
|
|
with self.assertRaises(tpm.TpmApi.ConfigError) as context: |
967
|
|
|
tpm.TpmApiv4('https://tpm.example.com', public_key='USER', password='PASS') |
968
|
|
|
log.debug("context exception: {}".format(context.exception)) |
969
|
|
|
self.assertEqual("'No authentication specified (user/password or private/public key)'", str(context.exception)) |
970
|
|
|
|
971
|
|
|
def test_wrong_auth_exception3(self): |
972
|
|
|
"""Exception if wrong authentication mehtod with username but public_key.""" |
973
|
|
|
with self.assertRaises(tpm.TpmApi.ConfigError) as context: |
974
|
|
|
tpm.TpmApiv4('https://tpm.example.com', username='USER', public_key='PASS') |
975
|
|
|
log.debug("context exception: {}".format(context.exception)) |
976
|
|
|
self.assertEqual("'No authentication specified (user/password or private/public key)'", str(context.exception)) |
977
|
|
|
|
978
|
|
|
def test_wrong_auth_exception4(self): |
979
|
|
|
"""Exception if wrong authentication mehtod with private key but password.""" |
980
|
|
|
with self.assertRaises(tpm.TpmApi.ConfigError) as context: |
981
|
|
|
tpm.TpmApiv4('https://tpm.example.com', private_key='USER', password='PASS') |
982
|
|
|
log.debug("context exception: {}".format(context.exception)) |
983
|
|
|
self.assertEqual("'No authentication specified (user/password or private/public key)'", str(context.exception)) |
984
|
|
|
|
985
|
|
|
def test_wrong_url_exception(self): |
986
|
|
|
"""Exception if URL does not match REGEXurl.""" |
987
|
|
View Code Duplication |
wrong_url = 'ftp://tpm.example.com' |
|
|
|
|
988
|
|
|
with self.assertRaises(tpm.TpmApiv4.ConfigError) as context: |
989
|
|
|
tpm.TpmApiv4(wrong_url, username='USER', password='PASS') |
990
|
|
|
log.debug("context exception: {}".format(context.exception)) |
991
|
|
|
self.assertEqual("'Invalid URL: {}'".format(wrong_url), str(context.exception)) |
992
|
|
|
|
993
|
|
|
class ExceptionOnRequestsTestCases(unittest.TestCase): |
994
|
|
|
"""Test case for Request based Exceptions.""" |
995
|
|
|
def setUp(self): |
996
|
|
|
self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS') |
997
|
|
|
|
998
|
|
|
def test_connection_exception(self): |
999
|
|
|
"""Exception if connection fails.""" |
1000
|
|
|
exception_error = "Connection error for " |
1001
|
|
|
with self.assertRaises(tpm.TPMException) as context: |
1002
|
|
|
self.client.list_passwords() |
1003
|
|
|
log.debug("context exception: {}".format(context.exception)) |
1004
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
1005
|
|
|
|
1006
|
|
|
def test_value_error_exception(self): |
1007
|
|
|
"""Exception if value is not json format.""" |
1008
|
|
|
path_to_mock = 'passwords/value_error.json' |
1009
|
|
|
request_url = api_url + path_to_mock |
1010
|
|
|
exception_error = "No JSON object could be decoded: " |
1011
|
|
|
exception_error3 = "Expecting value: line 1 column 1 (char 0): " |
1012
|
|
|
resource_file = os.path.normpath('tests/resources/{}'.format(path_to_mock)) |
1013
|
|
|
data = open(resource_file) |
1014
|
|
|
with self.assertRaises(ValueError) as context: |
1015
|
|
|
with requests_mock.Mocker() as m: |
1016
|
|
|
m.get(request_url, text=str(data)) |
1017
|
|
|
response = self.client.show_password('value_error') |
1018
|
|
View Code Duplication |
log.debug("context exception: {}".format(context.exception)) |
|
|
|
|
1019
|
|
|
self.assertTrue(str(context.exception).startswith(exception_error) or str(context.exception).startswith(exception_error3)) |
1020
|
|
|
|
1021
|
|
|
def test_exception_on_error_in_result(self): |
1022
|
|
|
"""Exception if "error" found in result.""" |
1023
|
|
|
exception_error = 'something bad happened' |
1024
|
|
|
error_json={'error': 'not good', 'message': exception_error} |
1025
|
|
|
path_to_mock = 'passwords/json_error.json' |
1026
|
|
|
request_url = api_url + path_to_mock |
1027
|
|
|
with self.assertRaises(tpm.TPMException) as context: |
1028
|
|
|
with requests_mock.Mocker() as m: |
1029
|
|
|
m.get(request_url, json=error_json) |
1030
|
|
|
response = self.client.show_password('json_error') |
1031
|
|
|
log.debug("context exception: {}".format(context.exception)) |
1032
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
1033
|
|
|
|
1034
|
|
|
def test_exception_on_403(self): |
1035
|
|
|
"""Exception if 403 forbidden.""" |
1036
|
|
|
path_to_mock = 'passwords.json' |
1037
|
|
|
request_url = api_url + path_to_mock |
1038
|
|
|
exception_error = "{} forbidden".format(request_url) |
1039
|
|
|
with self.assertRaises(tpm.TPMException) as context: |
1040
|
|
|
with requests_mock.Mocker() as m: |
1041
|
|
|
m.get(request_url, text='forbidden', status_code=403) |
1042
|
|
|
response = self.client.list_passwords() |
1043
|
|
View Code Duplication |
log.debug("context exception: {}".format(context.exception)) |
|
|
|
|
1044
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
1045
|
|
|
|
1046
|
|
|
def test_exception_on_404(self): |
1047
|
|
|
"""Exception if 404 not found.""" |
1048
|
|
|
path_to_mock = 'passwords.json' |
1049
|
|
|
request_url = api_url + path_to_mock |
1050
|
|
|
exception_error = "{} not found".format(request_url) |
1051
|
|
|
with self.assertRaises(tpm.TPMException) as context: |
1052
|
|
|
with requests_mock.Mocker() as m: |
1053
|
|
|
m.get(request_url, text='not found', status_code=404) |
1054
|
|
|
response = self.client.list_passwords() |
1055
|
|
View Code Duplication |
log.debug("context exception: {}".format(context.exception)) |
|
|
|
|
1056
|
|
|
self.assertTrue(exception_error in str(context.exception)) |
1057
|
|
|
|
1058
|
|
|
def test_exception_on_405(self): |
1059
|
|
|
"""Exception if 405 Method Not Allowed.""" |
1060
|
|
|
path_to_mock = 'passwords.json' |
1061
|
|
|
request_url = api_url + path_to_mock |
1062
|
|
|
exception_error = "{} Method Not Allowed".format(request_url) |
1063
|
|
|
with self.assertRaises(ValueError) as context: |
1064
|
|
|
with requests_mock.Mocker() as m: |
1065
|
|
|
m.get(request_url, text='Method Not Allowed', status_code=405) |
1066
|
|
|
response = self.client.list_passwords() |
1067
|
|
|
log.debug("context exception: {}".format(context.exception)) |
1068
|
|
|
self.assertTrue(str(context.exception).endswith(exception_error)) |
1069
|
|
|
|