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