Issues (87)

tcms/testplans/tests/test_new_plan.py (2 issues)

1
from http import HTTPStatus
2
3
from django.urls import reverse
4
from django.utils.translation import gettext_lazy as _
5
from uuslug import slugify
6
7
from tcms.testplans.models import TestPlan
8
from tcms.tests import LoggedInTestCase, PermissionsTestCase, user_should_have_perm
9
from tcms.tests.factories import (
10
    PlanTypeFactory,
11
    ProductFactory,
12
    TestPlanFactory,
13
    VersionFactory,
14
)
15
16
17
class TestNewTestPlanView(LoggedInTestCase):
18 View Code Duplication
    @classmethod
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
19
    def setUpTestData(cls):
20
        super().setUpTestData()
21
22
        cls.plan_name = "TestNewTestPlanView"
23
        cls.product = ProductFactory()
24
        cls.product_version = VersionFactory(product=cls.product)
25
        cls.plan_type = PlanTypeFactory()
26
        cls.test_plan = TestPlanFactory()
27
28
        user_should_have_perm(cls.tester, perm="testplans.add_testplan")
29
        user_should_have_perm(cls.tester, perm="testplans.view_testplan")
30
31
        cls.location = reverse("plans-new")
32
33
        cls.request = {
34
            "author": cls.tester.pk,
35
            "product": cls.product.pk,
36
            "product_version": cls.product_version.pk,
37
            "type": cls.plan_type.pk,
38
            "name": cls.plan_name,
39
            "email_settings-0-auto_to_plan_author": "on",
40
            "email_settings-0-auto_to_case_owner": "on",
41
            "email_settings-0-auto_to_case_default_tester": "on",
42
            "email_settings-0-notify_on_case_update": "on",
43
            "email_settings-0-notify_on_plan_update": "on",
44
            "email_settings-0-id": cls.test_plan.emailing.pk,
45
            "email_settings-TOTAL_FORMS": "1",
46
            "email_settings-INITIAL_FORMS": "1",
47
            "email_settings-MIN_NUM_FORMS": "0",
48
            "email_settings-MAX_NUM_FORMS": "1",
49
            "is_active": True,
50
        }
51
52
    def test_plan_new_get(self):
53
        response = self.client.get(self.location)
54
55
        self.assertContains(
56
            response,
57
            '<input class="bootstrap-switch" name="is_active" type="checkbox" checked',
58
            html=False,
59
        )
60
        self.assertContains(
61
            response,
62
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
63
            'type="checkbox" checked',
64
            html=False,
65
        )
66
        self.assertContains(
67
            response,
68
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_owner" '
69
            'type="checkbox" checked',
70
            html=False,
71
        )
72
        self.assertContains(
73
            response,
74
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_case_default_tester" '
75
            'type="checkbox" checked',
76
            html=False,
77
        )
78
        self.assertContains(
79
            response,
80
            '<input class="bootstrap-switch" name="email_settings-0-notify_on_plan_update" '
81
            'type="checkbox" checked',
82
            html=False,
83
        )
84
        self.assertContains(
85
            response,
86
            '<input class="bootstrap-switch" name="email_settings-0-notify_on_case_update" '
87
            'type="checkbox" checked',
88
            html=False,
89
        )
90
91
    def test_notify_formset_invalid(self):
92
        # Note: Boolean fields are always valid - either False or True
93
        # That's why the only way to make the notify formset invalid is to
94
        # reference a non-existing email_settings ID !!!
95
        data = self.request.copy()
96
        data["email_settings-0-id"] = -1
97
        del data["email_settings-0-auto_to_plan_author"]
98
99
        response = self.client.post(self.location, data)
100
101
        self.assertContains(response, _("Create new TestPlan"))
102
        self.assertContains(
103
            response,
104
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
105
            'type="checkbox"',
106
            html=False,
107
        )
108
109
    def test_plan_create_new_active(self):
110
        self._test_plan_create_new(is_active=True)
111
112
    def test_plan_create_new_inactive(self):
113
        self._test_plan_create_new(is_active=False)
114
115
    def _test_plan_create_new(self, is_active):
116
        self.request["is_active"] = is_active
117
118
        response = self.client.post(self.location, self.request)
119
        self.assertEqual(response.status_code, HTTPStatus.FOUND)
120
121
        plan = TestPlan.objects.get(
122
            name=self.plan_name,
123
            is_active=is_active,
124
        )
125
        self.assertEqual(plan.author, self.tester)
126
        self.assertEqual(plan.product, self.product)
127
        self.assertEqual(plan.product_version, self.product_version)
128
        self.assertEqual(plan.type, self.plan_type)
129
        self.assertEqual(plan.is_active, is_active)
130
        self.assertTrue(plan.emailing.auto_to_plan_author)
131
        self.assertTrue(plan.emailing.auto_to_case_owner)
132
        self.assertTrue(plan.emailing.auto_to_case_default_tester)
133
        self.assertTrue(plan.emailing.notify_on_plan_update)
134
        self.assertTrue(plan.emailing.notify_on_case_update)
135
136
    def test_with_invalid_product_shows_error(self):
137
        new_data = self.request.copy()
138
        new_data["product"] = -1
139
        del new_data["email_settings-0-auto_to_plan_author"]
140
141
        response = self.client.post(self.location, data=new_data, follow=True)
142
143
        self.assertContains(response, _("Create new TestPlan"))
144
        self.assertContains(
145
            response,
146
            _(
147
                "Select a valid choice. That choice is not one of the available choices."
148
            ),
149
        )
150
        self.assertContains(
151
            response,
152
            '<input class="bootstrap-switch" name="email_settings-0-auto_to_plan_author" '
153
            'type="checkbox"',
154
            html=False,
155
        )
156
157
158
class TestNewTestPlanViewPermissions(
159
    PermissionsTestCase
160
):  # pylint: disable=too-many-ancestors
161
162
    add_testplan_permission = "testplans.add_testplan"
163
    permission_label = add_testplan_permission
164
    http_method_names = ["get", "post"]
165
    url = reverse("plans-new")
166
167 View Code Duplication
    @classmethod
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
168
    def setUpTestData(cls):
169
        cls.post_data = {"description": "test"}
170
        super().setUpTestData()
171
172
        cls.plan_name = "TestNewTestPlanViewPermissions"
173
        cls.product = ProductFactory()
174
        cls.product_version = VersionFactory(product=cls.product)
175
        cls.plan_type = PlanTypeFactory()
176
        cls.test_plan = TestPlanFactory()
177
178
        user_should_have_perm(cls.tester, perm="testplans.view_testplan")
179
180
        cls.post_data.update(  # pylint: disable=objects-update-used
181
            {
182
                "author": cls.tester.pk,
183
                "product": cls.product.pk,
184
                "product_version": cls.product_version.pk,
185
                "type": cls.plan_type.pk,
186
                "name": cls.plan_name,
187
                "email_settings-0-auto_to_plan_author": "on",
188
                "email_settings-0-auto_to_case_owner": "on",
189
                "email_settings-0-auto_to_case_default_tester": "on",
190
                "email_settings-0-notify_on_case_update": "on",
191
                "email_settings-0-notify_on_plan_update": "on",
192
                "email_settings-0-id": cls.test_plan.emailing.pk,
193
                "email_settings-TOTAL_FORMS": "1",
194
                "email_settings-INITIAL_FORMS": "1",
195
                "email_settings-MIN_NUM_FORMS": "0",
196
                "email_settings-MAX_NUM_FORMS": "1",
197
                "is_active": True,
198
            }
199
        )
200
201
    def verify_get_with_permission(self):
202
        response = self.client.get(self.url)
203
204
        self.assertEqual(response.status_code, 200)
205
        self.assertContains(response, _("Create new TestPlan"))
206
207
    def verify_post_with_permission(self):
208
        response = self.client.post(self.url, self.post_data, follow=True)
209
        test_plan = TestPlan.objects.get(
210
            name=self.post_data["name"],
211
        )
212
        redirect_url = reverse(
213
            "test_plan_url", args=[test_plan.pk, slugify(test_plan.name)]
214
        )
215
216
        self.assertRedirects(response, redirect_url)
217
        self.assertContains(response, self.post_data["name"])
218