Code Duplication    Length = 14-17 lines in 16 locations

tests/test_tpm.py 16 locations

@@ 266-282 (lines=17) @@
263
            data = json.load(data_file)
264
            with requests_mock.Mocker() as m:
265
                fake_data(request_url, m)
266
                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.
@@ 815-830 (lines=16) @@
812
        self.assertEqual(response, None)
813
814
class GeneralClientTestCases(unittest.TestCase):
815
    """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))
@@ 496-511 (lines=16) @@
493
            data = json.load(data_file)
494
            with requests_mock.Mocker() as m:
495
                fake_data(request_url, m)
496
                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.
@@ 369-384 (lines=16) @@
366
            data = json.load(data_file)
367
            with requests_mock.Mocker() as m:
368
                fake_data(request_url, m)
369
                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.
@@ 352-367 (lines=16) @@
349
            data = json.load(data_file)
350
            with requests_mock.Mocker() as m:
351
                fake_data(request_url, m)
352
                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
                response = self.client.show_password(password_id)
370
            # number of passwords as from original json file.
@@ 249-264 (lines=16) @@
246
        path_to_mock = 'projects/4/unarchive.json'
247
        request_url = api_url + path_to_mock
248
        with requests_mock.Mocker() as m:
249
            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
                response = self.client.list_subprojects(project_id)
267
            # number of passwords as from original json file.
@@ 166-181 (lines=16) @@
163
            data = json.load(data_file)
164
            with requests_mock.Mocker() as m:
165
                fake_data(request_url, m)
166
                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.
@@ 149-164 (lines=16) @@
146
            data = json.load(data_file)
147
            with requests_mock.Mocker() as m:
148
                fake_data(request_url, m)
149
                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
                response = self.client.list_passwords_of_project(project_id)
167
            # number of passwords as from original json file.
@@ 132-147 (lines=16) @@
129
            data = json.load(data_file)
130
            with requests_mock.Mocker() as m:
131
                fake_data(request_url, m)
132
                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
                response = self.client.show_project(project_id)
150
            # number of passwords as from original json file.
@@ 742-756 (lines=15) @@
739
        data_file = open(resource_file)
740
        data = json.load(data_file)
741
        with requests_mock.Mocker() as m:
742
            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)
@@ 606-620 (lines=15) @@
603
        data_file = open(resource_file)
604
        data = json.load(data_file)
605
        with requests_mock.Mocker() as m:
606
            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
                fake_data(request_url, m)
623
                response = self.client.show_user(user_id)
@@ 480-494 (lines=15) @@
477
        data = json.load(data_file)
478
        with requests_mock.Mocker() as m:
479
            fake_data(request_url, m)
480
            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
                response = self.client.list_mypasswords_search(search)
497
            # number of passwords as from original json file.
@@ 336-350 (lines=15) @@
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
            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
                response = self.client.list_passwords_search(search)
353
            # number of passwords as from original json file.
@@ 116-130 (lines=15) @@
113
        data = json.load(data_file)
114
        with requests_mock.Mocker() as m:
115
            fake_data(request_url, m)
116
            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
                response = self.client.list_projects_search(search)
133
            # number of passwords as from original json file.
@@ 622-635 (lines=14) @@
619
            data_file = open(resource_file)
620
            data = json.load(data_file)
621
            with requests_mock.Mocker() as m:
622
                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)
@@ 987-1000 (lines=14) @@
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
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'