tcms.testcases.tests.test_views   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 510
Duplicated Lines 17.45 %

Importance

Changes 0
Metric Value
wmc 33
eloc 378
dl 89
loc 510
rs 9.76
c 0
b 0
f 0

29 Methods

Rating   Name   Duplication   Size   Complexity  
A TestMultipleEmailField.test_clean() 0 21 3
A TestGetTestCase.setUpTestData() 0 4 1
A TestMultipleEmailField.setUpClass() 0 4 1
A TestMultipleEmailField.test_to_python() 0 12 2
A TestGetTestCase.test_test_case_is_shown() 0 6 1
A TestEditCaseView.test_show_edit_page() 0 4 1
A TestSearchCases.setUpTestData() 0 6 1
A TestEditCaseView.test_edit_a_case() 0 14 1
A TestNewCase.test_create_test_case_successfully_from_plan() 0 14 1
B TestEditTestCaseViewPermission.setUpTestData() 45 45 1
A TestEditTestCaseViewPermission.verify_get_with_permission() 0 5 1
A TestNewCase.test_create_test_case_successfully() 0 8 1
A TestEditCaseView.test_initial_default_tester() 0 12 1
A TestSearchCases.test_page_renders() 0 3 1
A TestEditCaseView.test_invalid_notify_formset() 0 15 1
A TestNewCase._assertTestCase() 0 15 1
A TestEditCaseView.test_404_if_case_id_not_exist() 0 4 1
A TestNewCasePermission.verify_post_with_permission() 0 9 1
A TestCloneCase.test_refuse_if_missing_argument() 0 5 1
B TestNewCasePermission.setUpTestData() 44 44 1
A TestEditTestCaseViewPermission.verify_post_with_permission() 0 7 1
A TestCloneCase.test_user_without_permission_should_not_be_able_to_clone_a_case() 0 14 1
B TestEditCaseView.setUpTestData() 0 55 1
A TestNewCasePermission.verify_get_with_permission() 0 5 1
A TestNewCase.test_notify_formset_invalid() 0 16 1
A TestCloneCase.test_show_clone_page_with_selected_cases() 0 9 2
A TestCloneCase.setUpTestData() 0 6 1
B TestNewCase.setUpTestData() 0 48 1
A TestSearchCases.test_get_parameter_should_be_accepted_for_a_product() 0 6 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=invalid-name, too-many-ancestors
3
# pylint: disable=objects-update-used
4
5
import unittest
6
from datetime import timedelta
7
from http import HTTPStatus
8
9
from django.forms import ValidationError
10
from django.urls import reverse
11
from django.utils.translation import gettext_lazy as _
12
13
from tcms.management.models import Priority
14
from tcms.testcases.fields import MultipleEmailField
15
from tcms.testcases.models import TestCase, TestCasePlan, TestCaseStatus
16
from tcms.tests import (
17
    BaseCaseRun,
18
    BasePlanCase,
19
    PermissionsTestCase,
20
    remove_perm_from_user,
21
    user_should_have_perm,
22
)
23
from tcms.tests.factories import TestCaseFactory
24
25
26
class TestGetTestCase(BaseCaseRun):
27
    @classmethod
28
    def setUpTestData(cls):
29
        super().setUpTestData()
30
        user_should_have_perm(cls.tester, "testcases.view_testcase")
31
32
    def test_test_case_is_shown(self):
33
        url = reverse("testcases-get", args=[self.case_1.pk])
34
        response = self.client.get(url)
35
36
        # will not fail when running under different locale
37
        self.assertEqual(HTTPStatus.OK, response.status_code)
38
39
40
class TestMultipleEmailField(unittest.TestCase):
41
    @classmethod
42
    def setUpClass(cls):
43
        super().setUpClass()
44
        cls.field = MultipleEmailField()
45
46
    def test_to_python(self):
47
        value = "zhangsan@localhost"
48
        pyobj = self.field.to_python(value)
49
        self.assertEqual(pyobj, [value])
50
51
        value = "zhangsan@localhost,,[email protected],"
52
        pyobj = self.field.to_python(value)
53
        self.assertEqual(pyobj, ["zhangsan@localhost", "[email protected]"])
54
55
        for value in ("", None, []):
56
            pyobj = self.field.to_python(value)
57
            self.assertEqual(pyobj, [])
58
59
    def test_clean(self):
60
        value = "zhangsan@localhost"
61
        data = self.field.clean(value)
62
        self.assertEqual(data, value)
63
64
        data = self.field.clean("zhangsan@localhost,[email protected]")
65
        self.assertEqual(data, "zhangsan@localhost,[email protected]")
66
67
        data = self.field.clean(",zhangsan@localhost, ,[email protected], \n")
68
        self.assertEqual(data, "zhangsan@localhost,[email protected]")
69
70
        with self.assertRaises(ValidationError):
71
            self.field.clean(",zhangsan,zhangsan@localhost, \n,[email protected], ")
72
73
        with self.assertRaises(ValidationError):
74
            self.field.required = True
75
            self.field.clean("")
76
77
        self.field.required = False
78
        data = self.field.clean("")
79
        self.assertEqual(data, "")
80
81
82
class TestNewCase(BasePlanCase):
83
    @classmethod
84
    def setUpTestData(cls):
85
        super().setUpTestData()
86
87
        cls.new_case_url = reverse("testcases-new")
88
89
        cls.summary = "summary"
90
        cls.text = "some text description"
91
        cls.script = "some script"
92
        cls.arguments = "args1, args2, args3"
93
        cls.requirement = "requirement"
94
        cls.link = "http://somelink.net"
95
        cls.notes = "notes"
96
        cls.data = {
97
            "author": cls.tester.pk,
98
            "summary": cls.summary,
99
            "default_tester": cls.tester.pk,
100
            "product": cls.case.category.product.pk,
101
            "category": cls.case.category.pk,
102
            "case_status": cls.case_status_confirmed.pk,
103
            "priority": cls.case.priority.pk,
104
            # specify in human readable format
105
            "setup_duration": "2 20:10:00",
106
            "testing_duration": "00:00:00",
107
            "text": cls.text,
108
            "script": cls.script,
109
            "arguments": cls.arguments,
110
            "requirement": cls.requirement,
111
            "extra_link": cls.link,
112
            "notes": cls.notes,
113
            "email_settings-0-auto_to_case_author": "on",
114
            "email_settings-0-auto_to_run_manager": "on",
115
            "email_settings-0-auto_to_execution_assignee": "on",
116
            "email_settings-0-auto_to_case_tester": "on",
117
            "email_settings-0-auto_to_run_tester": "on",
118
            "email_settings-0-notify_on_case_update": "on",
119
            "email_settings-0-notify_on_case_delete": "on",
120
            "email_settings-0-cc_list": "[email protected]",
121
            "email_settings-0-case": "",
122
            "email_settings-0-id": cls.case.emailing.pk,
123
            "email_settings-TOTAL_FORMS": "1",
124
            "email_settings-INITIAL_FORMS": "1",
125
            "email_settings-MIN_NUM_FORMS": "0",
126
            "email_settings-MAX_NUM_FORMS": "1",
127
        }
128
129
        user_should_have_perm(cls.tester, "testcases.add_testcase")
130
        user_should_have_perm(cls.tester, "testcases.view_testcase")
131
132
    def test_notify_formset_invalid(self):
133
        # Note: Boolean fields are always valid - either False or True
134
        # That's why the only way to make the notify formset invalid is to
135
        # reference a non-existing email_settings ID !!!
136
        data = self.data.copy()
137
        data["email_settings-0-id"] = -1
138
        del data["email_settings-0-auto_to_case_tester"]
139
140
        response = self.client.post(self.new_case_url, data)
141
142
        self.assertContains(response, _("New Test Case"))
143
        self.assertContains(
144
            response,
145
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_tester" '
146
            'type="checkbox"',
147
            html=False,
148
        )
149
150
    def test_create_test_case_successfully(self):
151
        response = self.client.post(self.new_case_url, self.data)
152
153
        test_case = TestCase.objects.get(summary=self.summary)
154
        redirect_url = reverse("testcases-get", args=[test_case.pk])
155
156
        self.assertRedirects(response, redirect_url)
157
        self._assertTestCase(test_case)
158
159
    def test_create_test_case_successfully_from_plan(self):
160
        self.data["from_plan"] = self.plan.pk
161
162
        response = self.client.post(self.new_case_url, self.data)
163
164
        test_case = TestCase.objects.get(summary=self.summary)
165
        redirect_url = reverse("testcases-get", args=[test_case.pk])
166
167
        self.assertRedirects(response, redirect_url)
168
        self.assertEqual(test_case.plan.get(), self.plan)
169
        self.assertEqual(
170
            TestCasePlan.objects.filter(case=test_case, plan=self.plan).count(), 1
171
        )
172
        self._assertTestCase(test_case)
173
174
    def _assertTestCase(self, test_case):
175
        self.assertEqual(test_case.author, self.tester)
176
        self.assertEqual(test_case.summary, self.summary)
177
        self.assertEqual(test_case.category, self.case.category)
178
        self.assertEqual(test_case.default_tester, self.tester)
179
        self.assertEqual(test_case.case_status, self.case_status_confirmed)
180
        self.assertEqual(test_case.priority, self.case.priority)
181
        self.assertEqual(test_case.text, self.text)
182
        self.assertEqual(test_case.script, self.script)
183
        self.assertEqual(test_case.arguments, self.arguments)
184
        self.assertEqual(test_case.requirement, self.requirement)
185
        self.assertEqual(test_case.extra_link, self.link)
186
        self.assertEqual(test_case.notes, self.notes)
187
        self.assertEqual(str(test_case.setup_duration), "2 days, 20:10:00")
188
        self.assertEqual(test_case.testing_duration, timedelta(0))
189
190
191
class TestNewCasePermission(PermissionsTestCase):
192
    permission_label = "testcases.add_testcase"
193
    http_method_names = ["get", "post"]
194
    url = reverse("testcases-new")
195
196 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
197
    def setUpTestData(cls):
198
        cls.summary = "summary"
199
        cls.post_data = {
200
            "summary": cls.summary,
201
        }
202
        super().setUpTestData()
203
        user_should_have_perm(cls.tester, "testcases.view_testcase")
204
205
        case_status_confirmed = TestCaseStatus.objects.filter(is_confirmed=True).first()
206
207
        case = TestCaseFactory(
208
            author=cls.tester,
209
            default_tester=None,
210
            reviewer=cls.tester,
211
            case_status=case_status_confirmed,
212
        )
213
        case.save()  # creates a new case entry in the database
214
215
        cls.post_data.update(
216
            {
217
                "author": cls.tester.pk,
218
                "default_tester": cls.tester.pk,
219
                "priority": case.priority.pk,
220
                "product": case.category.product.pk,
221
                "category": case.category.pk,
222
                "case_status": case_status_confirmed.pk,
223
                "text": "some text description",
224
                "script": "some script",
225
                "arguments": "args1, args2, args3",
226
                "email_settings-0-auto_to_case_author": "on",
227
                "email_settings-0-auto_to_run_manager": "on",
228
                "email_settings-0-auto_to_execution_assignee": "on",
229
                "email_settings-0-auto_to_case_tester": "on",
230
                "email_settings-0-auto_to_run_tester": "on",
231
                "email_settings-0-notify_on_case_update": "on",
232
                "email_settings-0-notify_on_case_delete ": "on",
233
                "email_settings-0-cc_list": "[email protected]",
234
                "email_settings-0-case": "",
235
                "email_settings-0-id": case.emailing.pk,
236
                "email_settings-TOTAL_FORMS": "1",
237
                "email_settings-INITIAL_FORMS": "1",
238
                "email_settings-MIN_NUM_FORMS": "0",
239
                "email_settings-MAX_NUM_FORMS": "1",
240
            }
241
        )
242
243
    def verify_get_with_permission(self):
244
        response = self.client.get(self.url)
245
246
        self.assertEqual(response.status_code, 200)
247
        self.assertContains(response, _("New Test Case"))
248
249
    def verify_post_with_permission(self):
250
        response = self.client.post(self.url, self.post_data, follow=True)
251
        test_case = TestCase.objects.get(summary=self.summary)
252
        redirect_url = reverse("testcases-get", args=[test_case.pk])
253
254
        self.assertRedirects(
255
            response, redirect_url, status_code=302, target_status_code=200
256
        )
257
        self.assertContains(response, self.summary)
258
259
260
class TestEditCaseView(BasePlanCase):
261
    """Test edit view method"""
262
263
    @classmethod
264
    def setUpTestData(cls):
265
        super().setUpTestData()
266
267
        cls.proposed_case = TestCaseFactory(
268
            author=cls.tester,
269
            default_tester=None,
270
            reviewer=cls.tester,
271
            case_status=cls.case_status_proposed,
272
            plan=[cls.plan],
273
        )
274
275
        # test data for https://github.com/kiwitcms/Kiwi/issues/334
276
277
        Priority.objects.filter(value="P4").update(is_active=False)
278
279
        user_should_have_perm(cls.tester, "testcases.change_testcase")
280
        user_should_have_perm(cls.tester, "testcases.view_testcase")
281
        cls.case_edit_url = reverse("testcases-edit", args=[cls.case_1.pk])
282
283
        # Copy, then modify or add new data for specific tests below
284
        cls.edit_data = {
285
            "author": cls.case_1.author.pk,
286
            "from_plan": cls.plan.pk,
287
            "summary": cls.case_1.summary,
288
            "product": cls.case_1.category.product.pk,
289
            "category": cls.case_1.category.pk,
290
            "default_tester": "",
291
            # specify in seconds
292
            "testing_duration": "0",
293
            "setup_duration": "3600",
294
            "case_status": cls.case_status_confirmed.pk,
295
            "arguments": "",
296
            "extra_link": "",
297
            "notes": "",
298
            "is_automated": "0",
299
            "requirement": "",
300
            "script": "",
301
            "priority": cls.case_1.priority.pk,
302
            "tag": "RHEL",
303
            "text": "Given-When-Then",
304
            "email_settings-0-auto_to_case_author": "on",
305
            "email_settings-0-auto_to_run_manager": "on",
306
            "email_settings-0-auto_to_execution_assignee": "on",
307
            "email_settings-0-auto_to_case_tester": "on",
308
            "email_settings-0-auto_to_run_tester": "on",
309
            "email_settings-0-notify_on_case_update": "on",
310
            "email_settings-0-notify_on_case_delete": "on",
311
            "email_settings-0-cc_list": "",
312
            "email_settings-0-case": cls.case_1.pk,
313
            "email_settings-0-id": cls.case_1.emailing.pk,
314
            "email_settings-TOTAL_FORMS": "1",
315
            "email_settings-INITIAL_FORMS": "1",
316
            "email_settings-MIN_NUM_FORMS": "0",
317
            "email_settings-MAX_NUM_FORMS": "1",
318
        }
319
320
    def test_404_if_case_id_not_exist(self):
321
        url = reverse("testcases-edit", args=[99999])
322
        response = self.client.get(url)
323
        self.assertEqual(HTTPStatus.NOT_FOUND, response.status_code)
324
325
    def test_show_edit_page(self):
326
        response = self.client.get(self.case_edit_url)
327
        self.assertEqual(200, response.status_code)
328
        self.assertNotContains(response, ">P4</option")
329
330
    def test_edit_a_case(self):
331
        edit_data = self.edit_data.copy()
332
        new_summary = f"Edited: {self.case_1.summary}"
333
        edit_data["summary"] = new_summary
334
335
        response = self.client.post(self.case_edit_url, edit_data)
336
337
        redirect_url = reverse("testcases-get", args=[self.case_1.pk])
338
        self.assertRedirects(response, redirect_url)
339
340
        self.case_1.refresh_from_db()
341
        self.assertEqual(new_summary, self.case_1.summary)
342
        self.assertEqual(self.case_1.testing_duration, timedelta(0))
343
        self.assertEqual(str(self.case_1.setup_duration), "1:00:00")
344
345
    def test_invalid_notify_formset(self):
346
        # Note: Boolean fields are always valid - either False or True
347
        # That's why the only way to make the notify formset invalid is to
348
        # reference a non-existing email_settings_id
349
        data = self.edit_data.copy()
350
        data["email_settings-0-id"] = -1
351
        del data["email_settings-0-auto_to_case_tester"]
352
353
        response = self.client.post(self.case_edit_url, data)
354
        self.assertContains(response, _("Edit TestCase"))
355
        self.assertContains(
356
            response,
357
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_tester" '
358
            'type="checkbox"',
359
            html=False,
360
        )
361
362
    def test_initial_default_tester(self):
363
        testcase = TestCaseFactory(
364
            author=self.tester,
365
            default_tester=self.tester,
366
            reviewer=self.tester,
367
            case_status=self.case_status_proposed,
368
            plan=[self.plan],
369
        )
370
        response = self.client.get(reverse("testcases-edit", args=[testcase.pk]))
371
        self.assertContains(
372
            response,
373
            '<input type="text" id="id_default_tester" name="default_tester" '
374
            f'value="{self.tester.email}" class="form-control">',
375
        )
376
377
378
class TestEditTestCaseViewPermission(PermissionsTestCase):
379
    permission_label = "testcases.change_testcase"
380
    http_method_names = ["get", "post"]
381
    url = reverse(
382
        "testcases-edit", args=[0]
383
    )  # Workaround for passing check_mandatory_attributes
384
385 View Code Duplication
    @classmethod
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
386
    def setUpTestData(cls):
387
        cls.summary = "updated summary"
388
        cls.post_data = {
389
            "summary": cls.summary,
390
        }
391
        super().setUpTestData()
392
        user_should_have_perm(cls.tester, "testcases.view_testcase")
393
394
        case_status_confirmed = TestCaseStatus.objects.filter(is_confirmed=True).first()
395
396
        cls.case = TestCaseFactory(
397
            author=cls.tester,
398
            default_tester=None,
399
            reviewer=cls.tester,
400
            case_status=case_status_confirmed,
401
        )
402
        cls.url = reverse("testcases-edit", args=[cls.case.pk])
403
        # case.save()  # creates a new case entry in the database
404
405
        cls.post_data.update(
406
            {
407
                "author": cls.tester.pk,
408
                "default_tester": cls.tester.pk,
409
                "priority": cls.case.priority.pk,
410
                "product": cls.case.category.product.pk,
411
                "category": cls.case.category.pk,
412
                "case_status": case_status_confirmed.pk,
413
                "text": "some text description",
414
                "script": "some script",
415
                "arguments": "args1, args2, args3",
416
                "email_settings-0-auto_to_case_author": "on",
417
                "email_settings-0-auto_to_run_manager": "on",
418
                "email_settings-0-auto_to_execution_assignee": "on",
419
                "email_settings-0-auto_to_case_tester": "on",
420
                "email_settings-0-auto_to_run_tester": "on",
421
                "email_settings-0-notify_on_case_update": "on",
422
                "email_settings-0-notify_on_case_delete ": "on",
423
                "email_settings-0-cc_list": "[email protected]",
424
                "email_settings-0-case": "",
425
                "email_settings-0-id": cls.case.emailing.pk,
426
                "email_settings-TOTAL_FORMS": "1",
427
                "email_settings-INITIAL_FORMS": "1",
428
                "email_settings-MIN_NUM_FORMS": "0",
429
                "email_settings-MAX_NUM_FORMS": "1",
430
            }
431
        )
432
433
    def verify_get_with_permission(self):
434
        response = self.client.get(self.url)
435
        self.assertEqual(response.status_code, 200)
436
        self.assertContains(response, _("Edit TestCase"))
437
        self.assertContains(response, self.case.summary)
438
439
    def verify_post_with_permission(self):
440
        response = self.client.post(self.url, self.post_data, follow=True)
441
        redirect_url = reverse("testcases-get", args=[self.case.pk])
442
        self.assertRedirects(
443
            response, redirect_url, status_code=302, target_status_code=200
444
        )
445
        self.assertContains(response, self.summary)
446
447
448
class TestCloneCase(BasePlanCase):
449
    """Test clone view method"""
450
451
    @classmethod
452
    def setUpTestData(cls):
453
        super(TestCloneCase, cls).setUpTestData()
454
455
        user_should_have_perm(cls.tester, "testcases.add_testcase")
456
        cls.clone_url = reverse("testcases-clone")
457
458
    def test_refuse_if_missing_argument(self):
459
        # Refuse to clone cases if missing selectAll and case arguments
460
        response = self.client.get(self.clone_url, {}, follow=True)
461
462
        self.assertContains(response, _("At least one TestCase is required"))
463
464
    def test_show_clone_page_with_selected_cases(self):
465
        response = self.client.get(
466
            self.clone_url, {"c": [self.case_1.pk, self.case_2.pk]}
467
        )
468
469
        self.assertContains(response, f"TP-{self.plan.pk}: {self.plan.name}")
470
471
        for case in [self.case_1, self.case_2]:
472
            self.assertContains(response, f"TC-{case.pk}: {case.summary}")
473
474
    def test_user_without_permission_should_not_be_able_to_clone_a_case(self):
475
        remove_perm_from_user(self.tester, "testcases.add_testcase")
476
        base_url = reverse("tcms-login") + "?next="
477
        expected = base_url + reverse("testcases-clone") + f"?c={self.case_1.pk}"
478
        response = self.client.get(
479
            self.clone_url,
480
            {
481
                "c": [
482
                    self.case_1.pk,
483
                ]
484
            },
485
        )
486
487
        self.assertRedirects(response, expected)
488
489
490
class TestSearchCases(BasePlanCase):
491
    """Test search view method"""
492
493
    @classmethod
494
    def setUpTestData(cls):
495
        super().setUpTestData()
496
497
        cls.search_url = reverse("testcases-search")
498
        user_should_have_perm(cls.tester, "testcases.view_testcase")
499
500
    def test_page_renders(self):
501
        response = self.client.get(self.search_url, {})
502
        self.assertContains(response, '<option value="">----------</option>', html=True)
503
504
    def test_get_parameter_should_be_accepted_for_a_product(self):
505
        response = self.client.get(self.search_url, {"product": self.product.pk})
506
        self.assertContains(
507
            response,
508
            f'<option value="{self.product.pk}" selected>{self.product.name}</option>',
509
            html=True,
510
        )
511