Passed
Push — master ( 1c16c8...694daa )
by Alexander
03:03
created

EditPlanViewTest.assert_notify_form()   A

Complexity

Conditions 1

Size

Total Lines 21
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 21
rs 9.376
c 0
b 0
f 0
cc 1
nop 2
1
# -*- coding: utf-8 -*-
2
# pylint: disable=too-many-ancestors
3
4
from http import HTTPStatus
5
6
from django.contrib.auth.models import Permission
7
from django.urls import reverse
8
from django.utils.translation import gettext_lazy as _
9
10
from tcms.tests import BasePlanCase, LoggedInTestCase, remove_perm_from_user, user_should_have_perm
11
from tcms.tests.factories import (TagFactory, UserFactory, TestPlanFactory,
12
                                  ProductFactory, VersionFactory, PlanTypeFactory)
13
from tcms.utils.permissions import initiate_user_with_default_setups
14
15
16
class TestViewPlanTags(BasePlanCase):
17
    @classmethod
18
    def setUpTestData(cls):
19
        super().setUpTestData()
20
21
        initiate_user_with_default_setups(cls.tester)
22
        for _i in range(3):
23
            cls.plan.add_tag(TagFactory())
24
25
        cls.unauthorized = UserFactory()
26
        cls.unauthorized.set_password('password')
27
        cls.unauthorized.save()
28
29
        cls.unauthorized.user_permissions.add(*Permission.objects.all())
30
        remove_perm_from_user(cls.unauthorized, 'testplans.add_testplantag')
31
        remove_perm_from_user(cls.unauthorized, 'testplans.delete_testplantag')
32
33
    def test_view_tags_with_permissions(self):
34
        url = reverse('ajax-tags')
35
        response = self.client.get(url, {'plan': self.plan.pk}, follow=True)
36
        self.assertEqual(HTTPStatus.OK, response.status_code)
37
38
        # assert tag actions are shown
39
        self.assertContains(response, _('Add Tag'))
40
        self.assertContains(response,
41
                            'class="remove js-remove-tag" title="remove tag">%s</a>' %
42
                            _('Remove'))
43
44
    def test_view_tags_without_permissions(self):
45
        self.client.logout()
46
47
        self.client.login(  # nosec:B106:hardcoded_password_funcarg
48
            username=self.unauthorized.username,
49
            password='password')
50
51
        url = reverse('ajax-tags')
52
        response = self.client.get(url, {'plan': self.plan.pk}, follow=True)
53
        self.assertEqual(HTTPStatus.OK, response.status_code)
54
55
        # assert tag actions are shown
56
        self.assertNotContains(response, 'Add Tag')
57
        self.assertContains(response, '<span class="disabled grey">%s</span>' % _('Remove'))
58
59
60
class EditPlanViewTest(LoggedInTestCase):
61
    @classmethod
62
    def setUpTestData(cls):
63
        super().setUpTestData()
64
65
        user_should_have_perm(cls.tester, perm='testplans.change_testplan')
66
        cls.product = ProductFactory()
67
        cls.product_version = VersionFactory(product=cls.product)
68
        cls.test_plan_type = PlanTypeFactory()
69
        cls.test_plan_1 = TestPlanFactory()
70
        cls.testplan_edit_url = reverse('plan-edit', args=[cls.test_plan_1.pk])
71
72
        cls.testplan_edit_data = {
73
            'author': cls.tester.pk,
74
            'product': cls.product.pk,
75
            'product_version': cls.product_version.pk,
76
            'type': cls.test_plan_type.pk,
77
            'name': cls.test_plan_1.name,
78
79
            'email_settings-0-auto_to_plan_author': 'on',
80
            'email_settings-0-auto_to_case_owner': 'on',
81
            'email_settings-0-auto_to_case_default_tester': 'on',
82
            'email_settings-0-notify_on_case_update': 'on',
83
            'email_settings-0-notify_on_plan_update': 'on',
84
85
            'email_settings-0-id': cls.test_plan_1.emailing.pk,
86
            'email_settings-TOTAL_FORMS': '1',
87
            'email_settings-INITIAL_FORMS': '1',
88
            'email_settings-MIN_NUM_FORMS': '0',
89
            'email_settings-MAX_NUM_FORMS': '1',
90
            'is_active': True,
91
        }
92
93
    def test_show_testplan_edit_page(self):
94
        response = self.client.get(self.testplan_edit_url)
95
        self.assertContains(response, _('Edit TestPlan'))
96
        self.assert_notify_form(response)
97
        self.assertContains(
98
            response,
99
            '<input class="bootstrap-switch" name="is_active" type="checkbox" checked>',
100
            html=True)
101
102
    def test_edit_testplan_text_field(self):
103
        edit_data = self.testplan_edit_data.copy()
104
        new_text = 'Edited: {0}'.format(self.test_plan_1.text)
105
        edit_data['text'] = new_text
106
107
        response = self.client.post(self.testplan_edit_url, data=edit_data, follow=True)
108
109
        self.assertContains(response, new_text, html=True)
110
        self.test_plan_1.refresh_from_db()
111
        self.assertEqual(new_text, self.test_plan_1.text)
112
113
    # Issue: When there is an invalid value on notify_formset,
114
    # returned response is always the default (ON).
115
    def test_with_invalid_notify_form_value(self):
116
        edit_data = self.testplan_edit_data.copy()
117
        edit_data['email_settings-0-id'] = -1
118
        del edit_data['email_settings-0-auto_to_plan_author']
119
120
        response = self.client.post(self.testplan_edit_url, data=edit_data, follow=True)
121
122
        self.assertContains(
123
            response,
124
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
125
            'type="checkbox">', html=True)
126
127
    def test_with_invalid_product_shows_error(self):
128
        edit_data = self.testplan_edit_data.copy()
129
        edit_data['product'] = -1
130
        del edit_data['email_settings-0-auto_to_plan_author']
131
132
        response = self.client.post(self.testplan_edit_url, data=edit_data, follow=True)
133
134
        self.assertContains(
135
            response,
136
            _('Select a valid choice. That choice is not one of the available choices.'))
137
        self.assertContains(
138
            response,
139
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
140
            'type="checkbox">', html=True)
141
142
    def test_with_invalid_type_shows_error(self):
143
        edit_data = self.testplan_edit_data.copy()
144
        edit_data['type'] = -1
145
        del edit_data['email_settings-0-auto_to_case_owner']
146
147
        response = self.client.post(self.testplan_edit_url, data=edit_data, follow=True)
148
149
        self.assertContains(
150
            response,
151
            _('Select a valid choice. That choice is not one of the available choices.'))
152
        self.assertContains(
153
            response,
154
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_owner" '
155
            'type="checkbox">', html=True)
156
157
    def test_with_invalid_version_shows_error(self):
158
        edit_data = self.testplan_edit_data.copy()
159
        version = VersionFactory()
160
        edit_data['product_version'] = version.pk
161
162
        response = self.client.post(self.testplan_edit_url, data=edit_data, follow=True)
163
164
        self.assertContains(
165
            response,
166
            _('Select a valid choice. That choice is not one of the available choices.'))
167
168
    def assert_notify_form(self, response):
169
        self.assertContains(
170
            response,
171
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
172
            'type="checkbox" checked>', html=True)
173
        self.assertContains(
174
            response,
175
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_owner" '
176
            'type="checkbox" checked>', html=True)
177
        self.assertContains(
178
            response,
179
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_default_tester" '
180
            'type="checkbox" checked>', html=True)
181
        self.assertContains(
182
            response,
183
            '<input class="bootstrap-switch" name="email_settings-0-notify_on_case_update" '
184
            'type="checkbox" checked>', html=True)
185
        self.assertContains(
186
            response,
187
            '<input class="bootstrap-switch" name="email_settings-0-notify_on_plan_update" '
188
            'type="checkbox" checked>', html=True)
189