Code Duplication    Length = 14-17 lines in 15 locations

tests/test_tpm.py 15 locations

@@ 266-282 (lines=17) @@
263
            data_file = open(resource_file)
264
            data = json.load(data_file)
265
            with requests_mock.Mocker() as m:
266
                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)
@@ 496-511 (lines=16) @@
493
            data_file = open(resource_file)
494
            data = json.load(data_file)
495
            with requests_mock.Mocker() as m:
496
                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)
@@ 369-384 (lines=16) @@
366
            data_file = open(resource_file)
367
            data = json.load(data_file)
368
            with requests_mock.Mocker() as m:
369
                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)
@@ 352-367 (lines=16) @@
349
            data_file = open(resource_file)
350
            data = json.load(data_file)
351
            with requests_mock.Mocker() as m:
352
                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
                fake_data(request_url, m)
370
                response = self.client.show_password(password_id)
@@ 249-264 (lines=16) @@
246
        """Test function unarchive_project."""
247
        path_to_mock = 'projects/4/unarchive.json'
248
        request_url = api_url + path_to_mock
249
        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
                fake_data(request_url, m)
267
                response = self.client.list_subprojects(project_id)
@@ 166-181 (lines=16) @@
163
            data_file = open(resource_file)
164
            data = json.load(data_file)
165
            with requests_mock.Mocker() as m:
166
                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)
@@ 149-164 (lines=16) @@
146
            data_file = open(resource_file)
147
            data = json.load(data_file)
148
            with requests_mock.Mocker() as m:
149
                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
                fake_data(request_url, m)
167
                response = self.client.list_passwords_of_project(project_id)
@@ 132-147 (lines=16) @@
129
            data_file = open(resource_file)
130
            data = json.load(data_file)
131
            with requests_mock.Mocker() as m:
132
                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
                fake_data(request_url, m)
150
                response = self.client.show_project(project_id)
@@ 742-756 (lines=15) @@
739
        resource_file = os.path.normpath(request_path)
740
        data_file = open(resource_file)
741
        data = json.load(data_file)
742
        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)
@@ 606-620 (lines=15) @@
603
        resource_file = os.path.normpath(request_path)
604
        data_file = open(resource_file)
605
        data = json.load(data_file)
606
        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
            with requests_mock.Mocker() as m:
623
                fake_data(request_url, m)
@@ 480-494 (lines=15) @@
477
        data_file = open(resource_file)
478
        data = json.load(data_file)
479
        with requests_mock.Mocker() as m:
480
            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
                fake_data(request_url, m)
497
                response = self.client.list_mypasswords_search(search)
@@ 336-350 (lines=15) @@
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
        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
                fake_data(request_url, m)
353
                response = self.client.list_passwords_search(search)
@@ 116-130 (lines=15) @@
113
        data_file = open(resource_file)
114
        data = json.load(data_file)
115
        with requests_mock.Mocker() as m:
116
            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
                fake_data(request_url, m)
133
                response = self.client.list_projects_search(search)
@@ 622-635 (lines=14) @@
619
            resource_file = os.path.normpath(request_path)
620
            data_file = open(resource_file)
621
            data = json.load(data_file)
622
            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()
@@ 987-1000 (lines=14) @@
984
985
    def test_wrong_url_exception(self):
986
        """Exception if URL does not match REGEXurl."""
987
        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))