Code Duplication    Length = 14-17 lines in 16 locations

tests/test_tpm.py 16 locations

@@ 266-282 (lines=17) @@
263
            # number of passwords as from original json file.
264
            self.assertEqual(data, response)
265
266
    def test_function_list_subprojects_action_new_pwd(self):
267
        """Test function list_subprojects_action_new_pwd."""
268
        action = 'new_pwd'
269
        for project in Projects:
270
            project_id = project.get('id')
271
            log.debug("Testing with Project ID: {}".format(project_id))
272
            path_to_mock = 'projects/{}/subprojects/{}.json'.format(project_id, action)
273
            request_url = api_url + path_to_mock
274
            request_path = local_path + path_to_mock
275
            resource_file = os.path.normpath(request_path)
276
            data_file = open(resource_file)
277
            data = json.load(data_file)
278
            with requests_mock.Mocker() as m:
279
                fake_data(request_url, m)
280
                response = self.client.list_subprojects_action(project_id, action)
281
            # number of passwords as from original json file.
282
            self.assertEqual(data, response)
283
284
class ClientPasswordTestCase(unittest.TestCase):
285
    """Test cases for all password related queries."""
@@ 815-830 (lines=16) @@
812
    def setUp(self):
813
        self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS')
814
815
    def test_paging(self):
816
        """Test paging, if number of items is same as from original data source."""
817
        path_to_mock = 'passwords.json'
818
        request_url = api_url + path_to_mock
819
        request_path = local_path + path_to_mock
820
        resource_file = os.path.normpath(request_path)
821
        data_file = open(resource_file)
822
        data = json.load(data_file)
823
        with requests_mock.Mocker() as m:
824
            fake_data(request_url, m)
825
            response = self.client.list_passwords()
826
        # number of passwords as from original json file.
827
        source_items = len(data)
828
        response_items = len(response)
829
        log.debug("Source Items: {}; Response Items: {}".format(source_items, response_items))
830
        self.assertEqual(source_items, response_items)
831
832
    def test_provide_unlock_reason(self):
833
        """Test providing an unlock reason."""
@@ 496-511 (lines=16) @@
493
            # number of passwords as from original json file.
494
            self.assertEqual(data, response)
495
496
    def test_function_show_mypassword(self):
497
        """Test function show_mypassword."""
498
        for password in MyPasswords:
499
            password_id = password.get('id')
500
            log.debug("Testing with Password ID: {}".format(password_id))
501
            path_to_mock = 'my_passwords/{}.json'.format(password_id)
502
            request_url = api_url + path_to_mock
503
            request_path = local_path + path_to_mock
504
            resource_file = os.path.normpath(request_path)
505
            data_file = open(resource_file)
506
            data = json.load(data_file)
507
            with requests_mock.Mocker() as m:
508
                fake_data(request_url, m)
509
                response = self.client.show_mypassword(password_id)
510
            # number of passwords as from original json file.
511
            self.assertEqual(data, response)
512
513
    def test_function_create_mypassword(self):
514
        """Test function create_mypassword."""
@@ 369-384 (lines=16) @@
366
            # number of passwords as from original json file.
367
            self.assertEqual(data, response)
368
369
    def test_function_list_user_access_on_password(self):
370
        """Test function list_user_access_on_password."""
371
        for password in Passwords:
372
            password_id = password.get('id')
373
            log.debug("Testing with Password ID: {}".format(password_id))
374
            path_to_mock = 'passwords/{}/security.json'.format(password_id)
375
            request_url = api_url + path_to_mock
376
            request_path = local_path + path_to_mock
377
            resource_file = os.path.normpath(request_path)
378
            data_file = open(resource_file)
379
            data = json.load(data_file)
380
            with requests_mock.Mocker() as m:
381
                fake_data(request_url, m)
382
                response = self.client.list_user_access_on_password(password_id)
383
            # number of passwords as from original json file.
384
            self.assertEqual(data, response)
385
386
    def test_function_create_password(self):
387
        """Test function create_password."""
@@ 352-367 (lines=16) @@
349
            # number of passwords as from original json file.
350
            self.assertEqual(data, response)
351
352
    def test_function_show_password(self):
353
        """Test function show_password."""
354
        for password in Passwords:
355
            password_id = password.get('id')
356
            log.debug("Testing with Password ID: {}".format(password_id))
357
            path_to_mock = 'passwords/{}.json'.format(password_id)
358
            request_url = api_url + path_to_mock
359
            request_path = local_path + path_to_mock
360
            resource_file = os.path.normpath(request_path)
361
            data_file = open(resource_file)
362
            data = json.load(data_file)
363
            with requests_mock.Mocker() as m:
364
                fake_data(request_url, m)
365
                response = self.client.show_password(password_id)
366
            # number of passwords as from original json file.
367
            self.assertEqual(data, response)
368
369
    def test_function_list_user_access_on_password(self):
370
        """Test function list_user_access_on_password."""
@@ 249-264 (lines=16) @@
246
            response = self.client.unarchive_project('4')
247
        self.assertEqual(response, None)
248
249
    def test_function_list_subprojects(self):
250
        """Test function list_subprojects."""
251
        for project in Projects:
252
            project_id = project.get('id')
253
            log.debug("Testing with Project ID: {}".format(project_id))
254
            path_to_mock = 'projects/{}/subprojects.json'.format(project_id)
255
            request_url = api_url + path_to_mock
256
            request_path = local_path + path_to_mock
257
            resource_file = os.path.normpath(request_path)
258
            data_file = open(resource_file)
259
            data = json.load(data_file)
260
            with requests_mock.Mocker() as m:
261
                fake_data(request_url, m)
262
                response = self.client.list_subprojects(project_id)
263
            # number of passwords as from original json file.
264
            self.assertEqual(data, response)
265
266
    def test_function_list_subprojects_action_new_pwd(self):
267
        """Test function list_subprojects_action_new_pwd."""
@@ 166-181 (lines=16) @@
163
            # number of passwords as from original json file.
164
            self.assertEqual(data, response)
165
166
    def test_function_list_user_access_on_project(self):
167
        """Test function list_user_access_on_project."""
168
        for project in Projects:
169
            project_id = project.get('id')
170
            log.debug("Testing with Project ID: {}".format(project_id))
171
            path_to_mock = 'projects/{}/security.json'.format(project_id)
172
            request_url = api_url + path_to_mock
173
            request_path = local_path + path_to_mock
174
            resource_file = os.path.normpath(request_path)
175
            data_file = open(resource_file)
176
            data = json.load(data_file)
177
            with requests_mock.Mocker() as m:
178
                fake_data(request_url, m)
179
                response = self.client.list_user_access_on_project(project_id)
180
            # number of passwords as from original json file.
181
            self.assertEqual(data, response)
182
183
    def test_function_create_project(self):
184
        """Test function create_project."""
@@ 149-164 (lines=16) @@
146
            # number of passwords as from original json file.
147
            self.assertEqual(data, response)
148
149
    def test_function_list_passwords_of_project(self):
150
        """Test function list_passwords_of_project."""
151
        for project in Projects:
152
            project_id = project.get('id')
153
            log.debug("Testing with Project ID: {}".format(project_id))
154
            path_to_mock = 'projects/{}/passwords.json'.format(project_id)
155
            request_url = api_url + path_to_mock
156
            request_path = local_path + path_to_mock
157
            resource_file = os.path.normpath(request_path)
158
            data_file = open(resource_file)
159
            data = json.load(data_file)
160
            with requests_mock.Mocker() as m:
161
                fake_data(request_url, m)
162
                response = self.client.list_passwords_of_project(project_id)
163
            # number of passwords as from original json file.
164
            self.assertEqual(data, response)
165
166
    def test_function_list_user_access_on_project(self):
167
        """Test function list_user_access_on_project."""
@@ 132-147 (lines=16) @@
129
            # number of passwords as from original json file.
130
            self.assertEqual(data, response)
131
132
    def test_function_show_project(self):
133
        """Test function show_project."""
134
        for project in Projects:
135
            project_id = project.get('id')
136
            log.debug("Testing with Project ID: {}".format(project_id))
137
            path_to_mock = 'projects/{}.json'.format(project_id)
138
            request_url = api_url + path_to_mock
139
            request_path = local_path + path_to_mock
140
            resource_file = os.path.normpath(request_path)
141
            data_file = open(resource_file)
142
            data = json.load(data_file)
143
            with requests_mock.Mocker() as m:
144
                fake_data(request_url, m)
145
                response = self.client.show_project(project_id)
146
            # number of passwords as from original json file.
147
            self.assertEqual(data, response)
148
149
    def test_function_list_passwords_of_project(self):
150
        """Test function list_passwords_of_project."""
@@ 742-756 (lines=15) @@
739
            response = self.client.list_groups()
740
        self.assertEqual(data, response)
741
742
    def test_function_show_group(self):
743
        """Test function show_group."""
744
        for group in Groups:
745
            group_id = group.get('id')
746
            log.debug("Testing with Project ID: {}".format(group_id))
747
            path_to_mock = 'groups/{}.json'.format(group_id)
748
            request_url = api_url + path_to_mock
749
            request_path = local_path + path_to_mock
750
            resource_file = os.path.normpath(request_path)
751
            data_file = open(resource_file)
752
            data = json.load(data_file)
753
            with requests_mock.Mocker() as m:
754
                fake_data(request_url, m)
755
                response = self.client.show_group(group_id)
756
            self.assertEqual(data, response)
757
758
    def test_function_create_group(self):
759
        """Test function create_group."""
@@ 606-620 (lines=15) @@
603
            response = self.client.list_users()
604
        self.assertEqual(data, response)
605
606
    def test_function_show_user(self):
607
        """Test function show_user."""
608
        for user in Users:
609
            user_id = user.get('id')
610
            log.debug("Testing with Project ID: {}".format(user_id))
611
            path_to_mock = 'users/{}.json'.format(user_id)
612
            request_url = api_url + path_to_mock
613
            request_path = local_path + path_to_mock
614
            resource_file = os.path.normpath(request_path)
615
            data_file = open(resource_file)
616
            data = json.load(data_file)
617
            with requests_mock.Mocker() as m:
618
                fake_data(request_url, m)
619
                response = self.client.show_user(user_id)
620
            self.assertEqual(data, response)
621
622
    def test_function_show_me(self):
623
        """Test function show_me."""
@@ 480-494 (lines=15) @@
477
        # number of passwords as from original json file.
478
        self.assertEqual(data, response)
479
480
    def test_function_list_mypasswords_search(self):
481
        """Test function list_mypasswords_search."""
482
        searches = ['amazon', 'backup', 'facebook', 'john', 'jonny']
483
        for search in searches:
484
            path_to_mock = 'my_passwords/search/{}.json'.format(search)
485
            request_url = api_url + path_to_mock
486
            request_path = local_path + path_to_mock
487
            resource_file = os.path.normpath(request_path)
488
            data_file = open(resource_file)
489
            data = json.load(data_file)
490
            with requests_mock.Mocker() as m:
491
                fake_data(request_url, m)
492
                response = self.client.list_mypasswords_search(search)
493
            # number of passwords as from original json file.
494
            self.assertEqual(data, response)
495
496
    def test_function_show_mypassword(self):
497
        """Test function show_mypassword."""
@@ 336-350 (lines=15) @@
333
            response = sorted(self.client.list_passwords_favorite(), key=lambda k: k['id'])
334
        self.assertEqual(data, response)
335
336
    def test_function_list_passwords_search(self):
337
        """Test function list_passwords_search."""
338
        searches = ['backup', 'dns', 'facebook', 'firewall', 'reddit', 'test']
339
        for search in searches:
340
            path_to_mock = 'passwords/search/{}.json'.format(search)
341
            request_url = api_url + path_to_mock
342
            request_path = local_path + path_to_mock
343
            resource_file = os.path.normpath(request_path)
344
            data_file = open(resource_file)
345
            data = json.load(data_file)
346
            with requests_mock.Mocker() as m:
347
                fake_data(request_url, m)
348
                response = self.client.list_passwords_search(search)
349
            # number of passwords as from original json file.
350
            self.assertEqual(data, response)
351
352
    def test_function_show_password(self):
353
        """Test function show_password."""
@@ 116-130 (lines=15) @@
113
        # number of passwords as from original json file.
114
        self.assertEqual(data, response)
115
116
    def test_function_list_projects_search(self):
117
        """Test function list_projects_search."""
118
        searches = ['company', 'internal', 'website']
119
        for search in searches:
120
            path_to_mock = 'projects/search/{}.json'.format(search)
121
            request_url = api_url + path_to_mock
122
            request_path = local_path + path_to_mock
123
            resource_file = os.path.normpath(request_path)
124
            data_file = open(resource_file)
125
            data = json.load(data_file)
126
            with requests_mock.Mocker() as m:
127
                fake_data(request_url, m)
128
                response = self.client.list_projects_search(search)
129
            # number of passwords as from original json file.
130
            self.assertEqual(data, response)
131
132
    def test_function_show_project(self):
133
        """Test function show_project."""
@@ 622-635 (lines=14) @@
619
                response = self.client.show_user(user_id)
620
            self.assertEqual(data, response)
621
622
    def test_function_show_me(self):
623
        """Test function show_me."""
624
        path_to_mock = 'users/me.json'
625
        request_url = api_url + path_to_mock
626
        request_path = local_path + path_to_mock
627
        resource_file = os.path.normpath(request_path)
628
        data_file = open(resource_file)
629
        data = json.load(data_file)
630
        with requests_mock.Mocker() as m:
631
            fake_data(request_url, m)
632
            response = self.client.show_me()
633
            response2 = self.client.who_am_i()
634
        self.assertEqual(data, response)
635
        self.assertEqual(response2, response)
636
637
    def test_function_create_user(self):
638
        """Test function create_user."""
@@ 987-1000 (lines=14) @@
984
class ExceptionOnRequestsTestCases(unittest.TestCase):
985
    """Test case for Request based Exceptions."""
986
    def setUp(self):
987
        self.client = tpm.TpmApiv4('https://tpm.example.com', username='USER', password='PASS')
988
989
    def test_connection_exception(self):
990
        """Exception if connection fails."""
991
        exception_error = "Connection error for HTTPSConnectionPool(host='tpm.example.com', port=443)"
992
        with self.assertRaises(tpm.TPMException) as context:
993
            self.client.list_passwords()
994
        log.debug("context exception: {}".format(context.exception))
995
        self.assertTrue(exception_error in str(context.exception))
996
997
    def test_value_error_exception(self):
998
        """Exception if value is not json format."""
999
        path_to_mock = 'passwords/value_error.json'
1000
        request_url = api_url + path_to_mock
1001
        exception_error = "No JSON object could be decoded: "
1002
        exception_error3 = "Expecting value: line 1 column 1 (char 0): "
1003
        resource_file = os.path.normpath('tests/resources/{}'.format(path_to_mock))