Passed
Push — master ( f58c94...986f62 )
by Alexander
03:34
created

TestRemovesCase.test_nothing_change_if_invalid_run_passed()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nop 1
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 TestExecutionFactory
19
from tcms.tests.factories import TagFactory
20
from tcms.tests.factories import UserFactory
21
from tcms.tests.factories import VersionFactory
22
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
23
24
25
class TestAddCase(XmlrpcAPIBaseTest):
26
    def _fixture_setup(self):
27
        super()._fixture_setup()
28
29
        self.plan = TestPlanFactory(author=self.api_user)
30
31
        self.test_case = TestCaseFactory()
32
        self.test_case.save()  # generate history object
33
        self.plan.add_case(self.test_case)
34
35
        self.test_run = TestRunFactory(plan=self.plan)
36
37
    def test_add_case(self):
38
        result = self.rpc_client.exec.TestRun.add_case(self.test_run.pk, self.test_case.pk)
39
        self.assertTrue(isinstance(result, dict))
40
41
        execution = TestExecution.objects.get(run=self.test_run.pk, case=self.test_case.pk)
42
        self.assertEqual(execution.pk, result['case_run_id'])
43
        self.assertEqual(execution.case.pk, result['case_id'])
44
        self.assertEqual(execution.run.pk, result['run_id'])
45
46
    def test_add_case_without_permissions(self):
47
        unauthorized_user = UserFactory()
48
        unauthorized_user.set_password('api-testing')
49
        unauthorized_user.save()
50
51
        unauthorized_user.user_permissions.add(*Permission.objects.all())
52
        remove_perm_from_user(unauthorized_user, 'testruns.add_testexecution')
53
54
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
55
                                       'api-testing',
56
                                       '%s/xml-rpc/' % self.live_server_url).server
57
58
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
59
            rpc_client.TestRun.add_case(self.test_run.pk, self.test_case.pk)
60
61
        exists = TestExecution.objects.filter(run=self.test_run.pk, case=self.test_case.pk).exists()
62
        self.assertFalse(exists)
63
64
65
class TestRemovesCase(XmlrpcAPIBaseTest):
66
    def _fixture_setup(self):
67
        super()._fixture_setup()
68
69
        self.plan = TestPlanFactory(author=self.api_user)
70
71
        self.test_case = TestCaseFactory()
72
        self.test_case.save()  # generate history object
73
        self.plan.add_case(self.test_case)
74
75
        self.test_run = TestRunFactory(plan=self.plan)
76
        self.test_execution = TestExecutionFactory(run=self.test_run, case=self.test_case)
77
        self.test_execution.save()
78
79
    def test_nothing_change_if_invalid_case_passed(self):
80
        self.rpc_client.exec.TestRun.remove_case(self.test_run.pk, 999999)
81
        self.test_execution.refresh_from_db()
82
        self.assertTrue(TestExecution.objects.filter(pk=self.test_execution.pk).exists())
83
        self.assertEqual(1, TestExecution.objects.count())
84
85
    def test_nothing_change_if_invalid_run_passed(self):
86
        self.rpc_client.exec.TestRun.remove_case(99999, self.test_case.pk)
87
        self.test_execution.refresh_from_db()
88
        self.assertTrue(TestExecution.objects.filter(pk=self.test_execution.pk).exists())
89
        self.assertEqual(1, TestExecution.objects.count())
90
91
    def test_remove_case_without_permissions(self):
92
        unauthorized_user = UserFactory()
93
        unauthorized_user.set_password('api-testing')
94
        unauthorized_user.save()
95
96
        unauthorized_user.user_permissions.add(*Permission.objects.all())
97
        remove_perm_from_user(unauthorized_user, 'testruns.delete_testexecution')
98
99
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
100
                                       'api-testing',
101
                                       '%s/xml-rpc/' % self.live_server_url).server
102
103
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
104
            rpc_client.TestRun.remove_case(self.test_run.pk, self.test_case.pk)
105
106
        exists = TestExecution.objects.filter(run=self.test_run.pk, case=self.test_case.pk).exists()
107
        self.assertTrue(exists)
108
109
    def test_should_remove_a_case_run(self):
110
        self.rpc_client.exec.TestRun.remove_case(self.test_run.pk, self.test_case.pk)
111
        self.assertFalse(TestExecution.objects.filter(pk=self.test_execution.pk).exists())
112
113
114
class TestAddTag(XmlrpcAPIBaseTest):
115
    def _fixture_setup(self):
116
        super(TestAddTag, self)._fixture_setup()
117
118
        self.product = ProductFactory()
119
        self.version = VersionFactory()
120
        self.build = self.product.build.first()
121
        self.plan = TestPlanFactory(author=self.api_user, product=self.product)
122
123
        self.test_runs = [
124
            TestRunFactory(product_version=self.version, build=self.build,
125
                           default_tester=None, plan=self.plan),
126
            TestRunFactory(product_version=self.version, build=self.build,
127
                           default_tester=None, plan=self.plan),
128
        ]
129
130
        self.tag0 = TagFactory(name='xmlrpc_test_tag_0')
131
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
132
133
    def test_add_tag(self):
134
        result = self.rpc_client.exec.TestRun.add_tag(self.test_runs[0].pk, self.tag0.name)
135
        self.assertEqual(len(result), 1)
136
        self.assertEqual(result[0]['id'], self.tag0.pk)
137
        self.assertEqual(result[0]['name'], self.tag0.name)
138
139
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
140
        self.assertTrue(tag_exists)
141
142
    def test_add_tag_without_permissions(self):
143
        unauthorized_user = UserFactory()
144
        unauthorized_user.set_password('api-testing')
145
        unauthorized_user.save()
146
147
        unauthorized_user.user_permissions.add(*Permission.objects.all())
148
        remove_perm_from_user(unauthorized_user, 'testruns.add_testruntag')
149
150
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
151
                                       'api-testing',
152
                                       '%s/xml-rpc/' % self.live_server_url).server
153
154
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
155
            rpc_client.TestRun.add_tag(self.test_runs[0].pk, self.tag0.name)
156
157
        # tags were not modified
158
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
159
        self.assertFalse(tag_exists)
160
161
162
class TestRemoveTag(XmlrpcAPIBaseTest):
163
    def _fixture_setup(self):
164
        super(TestRemoveTag, self)._fixture_setup()
165
166
        self.product = ProductFactory()
167
        self.version = VersionFactory()
168
        self.build = self.product.build.first()
169
        self.plan = TestPlanFactory(author=self.api_user, product=self.product)
170
171
        self.test_runs = [
172
            TestRunFactory(product_version=self.version, build=self.build,
173
                           default_tester=None, plan=self.plan),
174
            TestRunFactory(product_version=self.version, build=self.build,
175
                           default_tester=None, plan=self.plan),
176
        ]
177
178
        self.tag0 = TagFactory(name='xmlrpc_test_tag_0')
179
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
180
181
        for tag in [self.tag0, self.tag1]:
182
            self.test_runs[0].add_tag(tag)
183
            self.test_runs[1].add_tag(tag)
184
185
    def test_remove_tag(self):
186
        result = self.rpc_client.exec.TestRun.remove_tag(self.test_runs[0].pk, self.tag0.name)
187
        self.assertEqual(len(result), 1)
188
        self.assertEqual(result[0]['id'], self.tag1.pk)
189
        self.assertEqual(result[0]['name'], self.tag1.name)
190
191
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
192
        self.assertFalse(tag_exists)
193
194
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag1.pk).exists()
195
        self.assertTrue(tag_exists)
196
197 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...
198
        unauthorized_user = UserFactory()
199
        unauthorized_user.set_password('api-testing')
200
        unauthorized_user.save()
201
202
        unauthorized_user.user_permissions.add(*Permission.objects.all())
203
        remove_perm_from_user(unauthorized_user, 'testruns.delete_testruntag')
204
205
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
206
                                       'api-testing',
207
                                       '%s/xml-rpc/' % self.live_server_url).server
208
209
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
210
            rpc_client.TestRun.remove_tag(self.test_runs[0].pk, self.tag0.name)
211
212
        # tags were not modified
213
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag0.pk).exists()
214
        self.assertTrue(tag_exists)
215
216
        tag_exists = TestRun.objects.filter(pk=self.test_runs[0].pk, tag__pk=self.tag1.pk).exists()
217
        self.assertTrue(tag_exists)
218
219
220
class TestProductVersionWhenCreating(XmlrpcAPIBaseTest):
221
    def _fixture_setup(self):
222
        super()._fixture_setup()
223
224
        self.product = ProductFactory()
225
        self.version = VersionFactory()
226
        self.build = self.product.build.first()
227
        self.plan = TestPlanFactory(author=self.api_user,
228
                                    product=self.product,
229
                                    product_version=self.version)
230
231
    def test_create_without_product_version(self):
232
        test_run_fields = {
233
            'plan': self.plan.pk,
234
            'build': self.build.pk,
235
            'summary': 'TR without product_version',
236
            'manager': self.api_user.username,
237
        }
238
239
        result = self.rpc_client.exec.TestRun.create(test_run_fields)
240
        self.assertEqual(result['product_version'], self.plan.product_version.value)
241
242
    def test_create_with_product_version(self):
243
        version2 = VersionFactory()
244
245
        test_run_fields = {
246
            'plan': self.plan.pk,
247
            'build': self.build.pk,
248
            'summary': 'TR with product_version',
249
            'manager': self.api_user.pk,
250
            'product_version': version2.pk,
251
        }
252
253
        result = self.rpc_client.exec.TestRun.create(test_run_fields)
254
        # the result is still using product_version from TR.plan.product_version
255
        # not the one we specified above
256
        self.assertEqual(result['product_version'], self.plan.product_version.value)
257
258
259
@override_settings(LANGUAGE_CODE='en-us')
260
class TestUpdateTestRun(XmlrpcAPIBaseTest):
261
    def _fixture_setup(self):
262
        super()._fixture_setup()
263
264
        self.test_run = TestRunFactory()
265
266
        self.updated_test_plan = TestPlanFactory()
267
        self.updated_build = BuildFactory()
268
        self.updated_summary = 'Updated summary.'
269
        self.updated_stop_date = datetime.strptime('2020-05-05', '%Y-%m-%d')
270
271
    def test_successful_update(self):
272
        update_fields = {
273
            'plan': self.updated_test_plan.pk,
274
            'build': self.updated_build.pk,
275
            'summary': self.updated_summary,
276
            'stop_date': self.updated_stop_date
277
        }
278
279
        # assert test plan is not already updated
280
        self.assertNotEqual(self.updated_test_plan, self.test_run.plan.name)
281
        self.assertNotEqual(self.updated_build, self.test_run.build.name)
282
        self.assertNotEqual(self.updated_summary, self.test_run.summary)
283
        self.assertNotEqual(self.updated_stop_date, self.test_run.stop_date)
284
285
        updated_test_run = self.rpc_client.exec.TestRun.update(self.test_run.pk, update_fields)
286
        self.test_run.refresh_from_db()
287
288
        # compare result, returned from API call with test run from DB
289
        self.assertEqual(updated_test_run['plan'], self.test_run.plan.name)
290
        self.assertEqual(updated_test_run['build'], self.test_run.build.name)
291
        self.assertEqual(updated_test_run['summary'], self.test_run.summary)
292
        self.assertEqual(updated_test_run['stop_date'], str(self.test_run.stop_date))
293
294
        # compare result, returned from API call with params sent to the API
295
        self.assertEqual(updated_test_run['plan'], self.updated_test_plan.name)
296
        self.assertEqual(updated_test_run['build'], self.updated_build.name)
297
        self.assertEqual(updated_test_run['summary'], self.updated_summary)
298
        self.assertEqual(updated_test_run['stop_date'], str(self.updated_stop_date))
299
300
    def test_wrong_date_format(self):
301
        test_run = TestRunFactory()
302
        update_fields = {
303
            'plan': self.updated_test_plan.pk,
304
            'build': self.updated_build.pk,
305
            'summary': self.updated_summary,
306
            'stop_date': '10-10-2010'
307
        }
308
309
        with self.assertRaisesMessage(Exception,
310
                                      'The stop date is invalid. The valid format is YYYY-MM-DD.'):
311
            self.rpc_client.exec.TestRun.update(test_run.pk, update_fields)
312
313
        # assert test run fields have not been updated
314
        test_run.refresh_from_db()
315
        self.assertNotEqual(update_fields['plan'], test_run.plan.pk)
316
        self.assertNotEqual(update_fields['build'], test_run.build.pk)
317
        self.assertNotEqual(update_fields['summary'], test_run.summary)
318
        self.assertNotEqual(datetime.strptime(update_fields['stop_date'], '%d-%m-%Y'),
319
                            test_run.stop_date)
320