Passed
Push — master ( cddcf6...1d3855 )
by Alexander
01:59
created

tcms.xmlrpc.tests.test_testrun   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 270
Duplicated Lines 7.78 %

Importance

Changes 0
Metric Value
wmc 20
eloc 191
dl 21
loc 270
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A TestUpdateTestRun.test_wrong_date_format() 0 20 2
A TestAddCase._fixture_setup() 0 10 1
A TestUpdateTestRun.test_successful_update() 0 28 1
A TestUpdateTestRun._fixture_setup() 0 9 1
A TestAddTag.test_add_tag() 0 8 1
A TestAddTag.test_add_tag_without_permissions() 0 18 2
A TestProductVersionWhenCreating._fixture_setup() 0 9 1
A TestRemoveTag._fixture_setup() 0 21 2
A TestAddCase.test_add_case() 0 8 1
A TestAddTag._fixture_setup() 0 17 1
A TestProductVersionWhenCreating.test_create_without_product_version() 0 10 1
A TestProductVersionWhenCreating.test_create_with_product_version() 0 15 1
A TestRemoveTag.test_remove_tag_without_permissions() 21 21 2
A TestAddCase.test_add_case_without_permissions() 0 17 2
A TestRemoveTag.test_remove_tag() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
# -*- coding: utf-8 -*-
2
# pylint: disable=attribute-defined-outside-init, objects-update-used
3
from datetime import datetime
4
from xmlrpc.client import ProtocolError
5
6
from django.test import override_settings
7
from django.contrib.auth.models import Permission
8
9
from tcms_api import xmlrpc
10
11
from tcms.testruns.models import TestRun, TestExecution
12
13
from tcms.tests import remove_perm_from_user
14
from tcms.tests.factories import TestCaseFactory, BuildFactory
15
from tcms.tests.factories import TestRunFactory
16
from tcms.tests.factories import ProductFactory
17
from tcms.tests.factories import TestPlanFactory
18
from tcms.tests.factories import TagFactory
19
from tcms.tests.factories import UserFactory
20
from tcms.tests.factories import VersionFactory
21
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
22
23
24
class TestAddCase(XmlrpcAPIBaseTest):
25
    def _fixture_setup(self):
26
        super()._fixture_setup()
27
28
        self.plan = TestPlanFactory(author=self.api_user)
29
30
        self.test_case = TestCaseFactory()
31
        self.test_case.save()  # generate history object
32
        self.plan.add_case(self.test_case)
33
34
        self.test_run = TestRunFactory(plan=self.plan)
35
36
    def test_add_case(self):
37
        result = self.rpc_client.exec.TestRun.add_case(self.test_run.pk, self.test_case.pk)
38
        self.assertTrue(isinstance(result, dict))
39
40
        test_case_run = TestExecution.objects.get(run=self.test_run.pk, case=self.test_case.pk)
41
        self.assertEqual(test_case_run.pk, result['case_run_id'])
42
        self.assertEqual(test_case_run.case.pk, result['case_id'])
43
        self.assertEqual(test_case_run.run.pk, result['run_id'])
44
45
    def test_add_case_without_permissions(self):
46
        unauthorized_user = UserFactory()
47
        unauthorized_user.set_password('api-testing')
48
        unauthorized_user.save()
49
50
        unauthorized_user.user_permissions.add(*Permission.objects.all())
51
        remove_perm_from_user(unauthorized_user, 'testruns.add_testexecution')
52
53
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
54
                                       'api-testing',
55
                                       '%s/xml-rpc/' % self.live_server_url).server
56
57
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
58
            rpc_client.TestRun.add_case(self.test_run.pk, self.test_case.pk)
59
60
        exists = TestExecution.objects.filter(run=self.test_run.pk, case=self.test_case.pk).exists()
61
        self.assertFalse(exists)
62
63
64
class TestAddTag(XmlrpcAPIBaseTest):
65
    def _fixture_setup(self):
66
        super(TestAddTag, self)._fixture_setup()
67
68
        self.product = ProductFactory()
69
        self.version = VersionFactory()
70
        self.build = self.product.build.first()
71
        self.plan = TestPlanFactory(author=self.api_user, product=self.product)
72
73
        self.test_runs = [
74
            TestRunFactory(product_version=self.version, build=self.build,
75
                           default_tester=None, plan=self.plan),
76
            TestRunFactory(product_version=self.version, build=self.build,
77
                           default_tester=None, plan=self.plan),
78
        ]
79
80
        self.tag0 = TagFactory(name='xmlrpc_test_tag_0')
81
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
82
83
    def test_add_tag(self):
84
        result = self.rpc_client.exec.TestRun.add_tag(self.test_runs[0].pk, self.tag0.name)
85
        self.assertEqual(len(result), 1)
86
        self.assertEqual(result[0]['id'], self.tag0.pk)
87
        self.assertEqual(result[0]['name'], self.tag0.name)
88
89
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
90
        self.assertTrue(tag_exists)
91
92
    def test_add_tag_without_permissions(self):
93
        unauthorized_user = UserFactory()
94
        unauthorized_user.set_password('api-testing')
95
        unauthorized_user.save()
96
97
        unauthorized_user.user_permissions.add(*Permission.objects.all())
98
        remove_perm_from_user(unauthorized_user, 'testruns.add_testruntag')
99
100
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
101
                                       'api-testing',
102
                                       '%s/xml-rpc/' % self.live_server_url).server
103
104
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
105
            rpc_client.TestRun.add_tag(self.test_runs[0].pk, self.tag0.name)
106
107
        # tags were not modified
108
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
109
        self.assertFalse(tag_exists)
110
111
112
class TestRemoveTag(XmlrpcAPIBaseTest):
113
    def _fixture_setup(self):
114
        super(TestRemoveTag, self)._fixture_setup()
115
116
        self.product = ProductFactory()
117
        self.version = VersionFactory()
118
        self.build = self.product.build.first()
119
        self.plan = TestPlanFactory(author=self.api_user, product=self.product)
120
121
        self.test_runs = [
122
            TestRunFactory(product_version=self.version, build=self.build,
123
                           default_tester=None, plan=self.plan),
124
            TestRunFactory(product_version=self.version, build=self.build,
125
                           default_tester=None, plan=self.plan),
126
        ]
127
128
        self.tag0 = TagFactory(name='xmlrpc_test_tag_0')
129
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
130
131
        for tag in [self.tag0, self.tag1]:
132
            self.test_runs[0].add_tag(tag)
133
            self.test_runs[1].add_tag(tag)
134
135
    def test_remove_tag(self):
136
        result = self.rpc_client.exec.TestRun.remove_tag(self.test_runs[0].pk, self.tag0.name)
137
        self.assertEqual(len(result), 1)
138
        self.assertEqual(result[0]['id'], self.tag1.pk)
139
        self.assertEqual(result[0]['name'], self.tag1.name)
140
141
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
142
        self.assertFalse(tag_exists)
143
144
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag1.pk).exists()
145
        self.assertTrue(tag_exists)
146
147 View Code Duplication
    def test_remove_tag_without_permissions(self):
0 ignored issues
show
Duplication introduced by Mr. Senko
This code seems to be duplicated in your project.
Loading history...
148
        unauthorized_user = UserFactory()
149
        unauthorized_user.set_password('api-testing')
150
        unauthorized_user.save()
151
152
        unauthorized_user.user_permissions.add(*Permission.objects.all())
153
        remove_perm_from_user(unauthorized_user, 'testruns.delete_testruntag')
154
155
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
156
                                       'api-testing',
157
                                       '%s/xml-rpc/' % self.live_server_url).server
158
159
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
160
            rpc_client.TestRun.remove_tag(self.test_runs[0].pk, self.tag0.name)
161
162
        # tags were not modified
163
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
164
        self.assertTrue(tag_exists)
165
166
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag1.pk).exists()
167
        self.assertTrue(tag_exists)
168
169
170
class TestProductVersionWhenCreating(XmlrpcAPIBaseTest):
171
    def _fixture_setup(self):
172
        super()._fixture_setup()
173
174
        self.product = ProductFactory()
175
        self.version = VersionFactory()
176
        self.build = self.product.build.first()
177
        self.plan = TestPlanFactory(author=self.api_user,
178
                                    product=self.product,
179
                                    product_version=self.version)
180
181
    def test_create_without_product_version(self):
182
        test_run_fields = {
183
            'plan': self.plan.pk,
184
            'build': self.build.pk,
185
            'summary': 'TR without product_version',
186
            'manager': self.api_user.username,
187
        }
188
189
        result = self.rpc_client.exec.TestRun.create(test_run_fields)
190
        self.assertEqual(result['product_version'], self.plan.product_version.value)
191
192
    def test_create_with_product_version(self):
193
        version2 = VersionFactory()
194
195
        test_run_fields = {
196
            'plan': self.plan.pk,
197
            'build': self.build.pk,
198
            'summary': 'TR with product_version',
199
            'manager': self.api_user.pk,
200
            'product_version': version2.pk,
201
        }
202
203
        result = self.rpc_client.exec.TestRun.create(test_run_fields)
204
        # the result is still using product_version from TR.plan.product_version
205
        # not the one we specified above
206
        self.assertEqual(result['product_version'], self.plan.product_version.value)
207
208
209
@override_settings(LANGUAGE_CODE='en-us')
210
class TestUpdateTestRun(XmlrpcAPIBaseTest):
211
    def _fixture_setup(self):
212
        super()._fixture_setup()
213
214
        self.test_run = TestRunFactory()
215
216
        self.updated_test_plan = TestPlanFactory()
217
        self.updated_build = BuildFactory()
218
        self.updated_summary = 'Updated summary.'
219
        self.updated_stop_date = datetime.strptime('2020-05-05', '%Y-%m-%d')
220
221
    def test_successful_update(self):
222
        update_fields = {
223
            'plan': self.updated_test_plan.pk,
224
            'build': self.updated_build.pk,
225
            'summary': self.updated_summary,
226
            'stop_date': self.updated_stop_date
227
        }
228
229
        # assert test plan is not already updated
230
        self.assertNotEqual(self.updated_test_plan, self.test_run.plan.name)
231
        self.assertNotEqual(self.updated_build, self.test_run.build.name)
232
        self.assertNotEqual(self.updated_summary, self.test_run.summary)
233
        self.assertNotEqual(self.updated_stop_date, self.test_run.stop_date)
234
235
        updated_test_run = self.rpc_client.exec.TestRun.update(self.test_run.pk, update_fields)
236
        self.test_run.refresh_from_db()
237
238
        # compare result, returned from API call with test run from DB
239
        self.assertEqual(updated_test_run['plan'], self.test_run.plan.name)
240
        self.assertEqual(updated_test_run['build'], self.test_run.build.name)
241
        self.assertEqual(updated_test_run['summary'], self.test_run.summary)
242
        self.assertEqual(updated_test_run['stop_date'], str(self.test_run.stop_date))
243
244
        # compare result, returned from API call with params sent to the API
245
        self.assertEqual(updated_test_run['plan'], self.updated_test_plan.name)
246
        self.assertEqual(updated_test_run['build'], self.updated_build.name)
247
        self.assertEqual(updated_test_run['summary'], self.updated_summary)
248
        self.assertEqual(updated_test_run['stop_date'], str(self.updated_stop_date))
249
250
    def test_wrong_date_format(self):
251
        test_run = TestRunFactory()
252
        update_fields = {
253
            'plan': self.updated_test_plan.pk,
254
            'build': self.updated_build.pk,
255
            'summary': self.updated_summary,
256
            'stop_date': '10-10-2010'
257
        }
258
259
        with self.assertRaisesMessage(Exception,
260
                                      'The stop date is invalid. The valid format is YYYY-MM-DD.'):
261
            self.rpc_client.exec.TestRun.update(test_run.pk, update_fields)
262
263
        # assert test run fields have not been updated
264
        test_run.refresh_from_db()
265
        self.assertNotEqual(update_fields['plan'], test_run.plan.pk)
266
        self.assertNotEqual(update_fields['build'], test_run.build.pk)
267
        self.assertNotEqual(update_fields['summary'], test_run.summary)
268
        self.assertNotEqual(datetime.strptime(update_fields['stop_date'], '%d-%m-%Y'),
269
                            test_run.stop_date)
270