Passed
Push — master ( 3727b7...72c322 )
by Alexander
02:28
created

TestExecutionCreate._fixture_setup()   A

Complexity

Conditions 1

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 15
rs 9.8
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
# pylint: disable=invalid-name, attribute-defined-outside-init, objects-update-used
3
4
from datetime import datetime
5
6
from xmlrpc.client import ProtocolError
7
from xmlrpc.client import Fault as XmlRPCFault
8
9
from django.test import override_settings
10
11
from tcms.core.contrib.linkreference.models import LinkReference
12
from tcms.testruns.models import TestExecutionStatus
13
14
from tcms.tests.factories import ProductFactory
15
from tcms.tests.factories import TestCaseFactory
16
from tcms.tests.factories import TestExecutionFactory
17
from tcms.tests.factories import TestPlanFactory
18
from tcms.tests.factories import TestRunFactory
19
from tcms.tests.factories import UserFactory
20
from tcms.tests.factories import VersionFactory
21
from tcms.tests.factories import BuildFactory
22
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
23
24
25
@override_settings(LANGUAGE_CODE='en-us')
26
class TestExecutionCreate(XmlrpcAPIBaseTest):  # pylint: disable=too-many-instance-attributes
27
    """Test TestExecution.create"""
28
29
    def _fixture_setup(self):
30
        super()._fixture_setup()
31
32
        self.staff = UserFactory(username='staff', email='[email protected]')
33
34
        self.product = ProductFactory(name='Nitrate')
35
        self.version = VersionFactory(value='0.1', product=self.product)
36
        self.build = self.product.build.first()
37
        self.plan = TestPlanFactory(author=self.api_user, product=self.product)
38
        self.test_run = TestRunFactory(product_version=self.version, build=self.build,
39
                                       default_tester=None, plan=self.plan)
40
        self.status = TestExecutionStatus.objects.get(name='IDLE')
41
        self.case = TestCaseFactory(author=self.api_user, default_tester=None, plan=[self.plan])
42
43
        self.execution_pks = []
44
45
    def test_create_with_no_required_fields(self):
46
        values = [
47
            {
48
                "assignee": self.staff.pk,
49
                "status": self.status.pk,
50
            },
51
            {
52
                "build": self.build.pk,
53
                "assignee": self.staff.pk,
54
                "status": 1,
55
            },
56
            {
57
                "run": self.test_run.pk,
58
                "build": self.build.pk,
59
                "assignee": self.staff.pk,
60
                "status": self.status.pk,
61
            },
62
        ]
63
        for value in values:
64
            with self.assertRaisesRegex(XmlRPCFault, 'This field is required'):
65
                self.rpc_client.exec.TestCaseRun.create(value)
66
67
    def test_create_with_required_fields(self):
68
        tcr = self.rpc_client.exec.TestExecution.create({
69
            "run": self.test_run.pk,
70
            "build": self.build.pk,
71
            "case": self.case.pk,
72
            "case_text_version": 15,
73
        })
74
        self.assertIsNotNone(tcr)
75
        self.execution_pks.append(tcr['case_run_id'])
76
        self.assertEqual(tcr['build_id'], self.build.pk)
77
        self.assertEqual(tcr['case_id'], self.case.pk)
78
        self.assertEqual(tcr['run_id'], self.test_run.pk)
79
80
    def test_create_with_all_fields(self):
81
        tcr = self.rpc_client.exec.TestCaseRun.create({
82
            "run": self.test_run.pk,
83
            "build": self.build.pk,
84
            "case": self.case.pk,
85
            "assignee": self.api_user.pk,
86
            "sortkey": 90,
87
            "status": self.status.pk,
88
            "case_text_version": 3,
89
        })
90
        self.assertIsNotNone(tcr)
91
        self.execution_pks.append(tcr['case_run_id'])
92
        self.assertEqual(tcr['build_id'], self.build.pk)
93
        self.assertEqual(tcr['case_id'], self.case.pk)
94
        self.assertEqual(tcr['assignee_id'], self.api_user.pk)
95
        self.assertEqual(tcr['sortkey'], 90)
96
        self.assertEqual(tcr['status'], 'IDLE')
97
        self.assertEqual(tcr['case_text_version'], 3)
98
99
    def test_create_with_non_exist_fields(self):
100
        values = [
101
            {
102
                "run": self.test_run.pk,
103
                "build": self.build.pk,
104
                "case": 111111,
105
            },
106
            {
107
                "run": 11111,
108
                "build": self.build.pk,
109
                "case": self.case.pk,
110
            },
111
            {
112
                "run": self.test_run.pk,
113
                "build": 11222222,
114
                "case": self.case.pk,
115
            },
116
        ]
117
        for value in values:
118
            with self.assertRaisesRegex(XmlRPCFault, 'Select a valid choice'):
119
                self.rpc_client.exec.TestCaseRun.create(value)
120
121
    def test_create_with_no_perm(self):
122
        values = {
123
            "run": self.test_run.pk,
124
            "build": self.build.pk,
125
            "case": self.case.pk,
126
            "assignee": self.api_user.pk,
127
            "sortkey": 2,
128
            "status": self.status.pk,
129
        }
130
        self.rpc_client.exec.Auth.logout()
131
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
132
            self.rpc_client.exec.TestCaseRun.create(values)
133
134
135
class TestCaseRunAddComment(XmlrpcAPIBaseTest):
136
    """Test TestExecution.add_comment"""
137
138
    def _fixture_setup(self):
139
        super(TestCaseRunAddComment, self)._fixture_setup()
140
141
        self.case_run_1 = TestExecutionFactory()
142
        self.case_run_2 = TestExecutionFactory()
143
144
    def test_add_comment_with_int(self):
145
        comment = self.rpc_client.exec.TestExecution.add_comment(self.case_run_2.pk, "Hello World!")
146
        self.assertIsNone(comment)
147
148
149
@override_settings(LANGUAGE_CODE='en-us')
150
class TestCaseRunAddLink(XmlrpcAPIBaseTest):
151
    """Test TestExecution.add_link"""
152
153
    def _fixture_setup(self):
154
        super()._fixture_setup()
155
156
        self.case_run = TestExecutionFactory()
157
158
    def test_attach_log_with_non_existing_id(self):
159
        with self.assertRaisesRegex(XmlRPCFault, 'constraint fail|violates foreign key'):
160
            self.rpc_client.exec.TestCaseRun.add_link(-5, 'A test log', 'http://example.com')
161
162
    def test_attach_log_with_invalid_url(self):
163
        with self.assertRaisesRegex(XmlRPCFault, 'Enter a valid URL'):
164
            self.rpc_client.exec.TestExecution.add_link(self.case_run.pk, "UT test logs", 'aaaaaa')
165
166
    def test_attach_log(self):
167
        url = "http://127.0.0.1/test/test-log.log"
168
        log_id = self.rpc_client.exec.TestExecution.add_link(self.case_run.pk, "UT test logs", url)
169
        self.assertGreater(log_id, 0)
170
171
172
class TestCaseRunRemoveLink(XmlrpcAPIBaseTest):
173
174
    def _fixture_setup(self):
175
        super()._fixture_setup()
176
177
        self.status_idle = TestExecutionStatus.objects.get(name='IDLE')
178
        self.tester = UserFactory()
179
        self.case_run = TestExecutionFactory(assignee=self.tester, tested_by=None,
180
                                             sortkey=10,
181
                                             status=self.status_idle)
182
183
    def setUp(self):
184
        super().setUp()
185
186
        self.rpc_client.exec.TestExecution.add_link(
187
            self.case_run.pk, 'Related issue', 'https://localhost/issue/1')
188
        self.link = self.case_run.links()[0]
189
190
    def test_doesnt_raise_with_non_existing_id(self):
191
        self.rpc_client.exec.TestCaseRun.remove_link(-9, self.link.pk)
192
        links = self.case_run.links()
193
        self.assertEqual(1, links.count())
194
        self.assertEqual(self.link.pk, links[0].pk)
195
196
    def test_detach_log_with_non_exist_log(self):
197
        self.rpc_client.exec.TestCaseRun.remove_link(self.case_run.pk, 999999999)
198
        links = self.case_run.links()
199
        self.assertEqual(1, links.count())
200
        self.assertEqual(self.link.pk, links[0].pk)
201
202
    def test_detach_log(self):
203
        self.rpc_client.exec.TestExecution.remove_link(self.case_run.pk, self.link.pk)
204
        self.assertEqual([], list(self.case_run.links()))
205
206
207
class TestCaseRunFilter(XmlrpcAPIBaseTest):
208
209
    def _fixture_setup(self):
210
        super()._fixture_setup()
211
212
        self.status_idle = TestExecutionStatus.objects.get(name='IDLE')
213
        self.tester = UserFactory()
214
        self.case_run = TestExecutionFactory(assignee=self.tester, tested_by=None,
215
                                             sortkey=10,
216
                                             status=self.status_idle)
217
218
    def test_with_non_exist_id(self):
219
        found = self.rpc_client.exec.TestCaseRun.filter({'pk': -1})
220
        self.assertEqual(0, len(found))
221
222
    def test_filter_by_id(self):
223
        tcr = self.rpc_client.exec.TestExecution.filter({'pk': self.case_run.pk})[0]
224
        self.assertIsNotNone(tcr)
225
        self.assertEqual(tcr['build_id'], self.case_run.build.pk)
226
        self.assertEqual(tcr['case_id'], self.case_run.case.pk)
227
        self.assertEqual(tcr['assignee_id'], self.tester.pk)
228
        self.assertEqual(tcr['tested_by_id'], None)
229
        self.assertEqual(tcr['sortkey'], 10)
230
        self.assertEqual(tcr['status'], 'IDLE')
231
        self.assertEqual(tcr['status_id'], self.status_idle.pk)
232
233
234
class TestCaseRunGetLinks(XmlrpcAPIBaseTest):
235
236
    def _fixture_setup(self):
237
        super()._fixture_setup()
238
239
        self.case_run_1 = TestExecutionFactory()
240
        self.case_run_2 = TestExecutionFactory()
241
242
        self.rpc_client.exec.TestCaseRun.add_link(
243
            self.case_run_1.pk,
244
            "Test logs",
245
            "http://www.google.com")
246
247
    def test_get_links_with_non_exist_id(self):
248
        result = self.rpc_client.exec.TestCaseRun.get_links(-9)
249
        self.assertEqual([], result)
250
251
    def test_get_empty_logs(self):
252
        logs = self.rpc_client.exec.TestExecution.get_links(self.case_run_2.pk)
253
        self.assertIsInstance(logs, list)
254
        self.assertEqual(len(logs), 0)
255
256
    def test_get_links(self):
257
        tcr_log = LinkReference.objects.get(test_case_run=self.case_run_1.pk)
258
        logs = self.rpc_client.exec.TestExecution.get_links(self.case_run_1.pk)
259
        self.assertIsInstance(logs, list)
260
        self.assertEqual(len(logs), 1)
261
        self.assertEqual(logs[0]['id'], tcr_log.pk)
262
        self.assertEqual(logs[0]['name'], "Test logs")
263
        self.assertEqual(logs[0]['url'], "http://www.google.com")
264
265
266
@override_settings(LANGUAGE_CODE='en-us')
267
class TestCaseRunUpdate(XmlrpcAPIBaseTest):
268
269
    def _fixture_setup(self):
270
        super()._fixture_setup()
271
272
        self.user = UserFactory()
273
        self.build = BuildFactory()
274
        self.case_run_1 = TestExecutionFactory()
275
        self.case_run_2 = TestExecutionFactory()
276
        self.status_running = TestExecutionStatus.objects.get(name='RUNNING')
277
278
    def test_update_with_single_caserun(self):
279
        tcr = self.rpc_client.exec.TestCaseRun.update(self.case_run_1.pk, {
280
            "build": self.build.pk,
281
            "assignee": self.user.pk,
282
            "status": self.status_running.pk,
283
            "sortkey": 90
284
        })
285
        self.assertEqual(tcr['build'], self.build.name)
286
        self.assertEqual(tcr['assignee'], self.user.username)
287
        self.assertEqual(tcr['status'], 'RUNNING')
288
        self.assertEqual(tcr['sortkey'], 90)
289
290
    def test_update_with_non_existing_build(self):
291
        with self.assertRaisesRegex(XmlRPCFault, 'Select a valid choice'):
292
            self.rpc_client.exec.TestCaseRun.update(self.case_run_1.pk, {"build": 1111111})
293
294
    def test_update_with_non_existing_assignee(self):
295
        with self.assertRaisesRegex(XmlRPCFault, 'Select a valid choice'):
296
            self.rpc_client.exec.TestCaseRun.update(self.case_run_1.pk, {"assignee": 1111111})
297
298
    def test_update_with_non_existing_status(self):
299
        with self.assertRaisesRegex(XmlRPCFault, 'Select a valid choice'):
300
            self.rpc_client.exec.TestExecution.update(self.case_run_1.pk,
301
                                                      {"status": 1111111})
302
303
    def test_update_with_no_perm(self):
304
        self.rpc_client.exec.Auth.logout()
305
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
306
            self.rpc_client.exec.TestExecution.update(self.case_run_1.pk,
307
                                                      {"close_date": datetime.now()})
308