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

tcms.testplans.tests.test_views   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 59
Duplicated Lines 71.19 %

Importance

Changes 0
Metric Value
wmc 4
eloc 41
dl 42
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A TestViewPlanTags.setUpTestData() 15 15 2
A TestViewPlanTags.test_view_tags_with_permissions() 10 10 1
A TestViewPlanTags.test_view_tags_without_permissions() 14 14 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=too-many-ancestors
3
4
from http import HTTPStatus
5
6
from django.urls import reverse
7
from django.contrib.auth.models import Permission
8
from django.utils.translation import ugettext_lazy as _
9
10
from tcms.tests.factories import TagFactory
11
from tcms.tests.factories import UserFactory
12
from tcms.tests import remove_perm_from_user
13
from tcms.tests import BasePlanCase
14
from tcms.utils.permissions import initiate_user_with_default_setups
15
16
17 View Code Duplication
class TestViewPlanTags(BasePlanCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
18
    @classmethod
19
    def setUpTestData(cls):
20
        super().setUpTestData()
21
22
        initiate_user_with_default_setups(cls.tester)
23
        for _i in range(3):
24
            cls.plan.add_tag(TagFactory())
25
26
        cls.unauthorized = UserFactory()
27
        cls.unauthorized.set_password('password')
28
        cls.unauthorized.save()
29
30
        cls.unauthorized.user_permissions.add(*Permission.objects.all())
31
        remove_perm_from_user(cls.unauthorized, 'testplans.add_testplantag')
32
        remove_perm_from_user(cls.unauthorized, 'testplans.delete_testplantag')
33
34
    def test_view_tags_with_permissions(self):
35
        url = reverse('ajax-tags')
36
        response = self.client.get(url, {'plan': self.plan.pk}, follow=True)
37
        self.assertEqual(HTTPStatus.OK, response.status_code)
38
39
        # assert tag actions are shown
40
        self.assertContains(response, _('Add Tag'))
41
        self.assertContains(response,
42
                            'class="remove js-remove-tag" title="remove tag">%s</a>' %
43
                            _('Remove'))
44
45
    def test_view_tags_without_permissions(self):
46
        self.client.logout()
47
48
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
49
            username=self.unauthorized.username,
50
            password='password')
51
52
        url = reverse('ajax-tags')
53
        response = self.client.get(url, {'plan': self.plan.pk}, follow=True)
54
        self.assertEqual(HTTPStatus.OK, response.status_code)
55
56
        # assert tag actions are shown
57
        self.assertNotContains(response, 'Add Tag')
58
        self.assertContains(response, '<span class="disabled grey">%s</span>' % _('Remove'))
59