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

tcms/xmlrpc/tests/test_testplan.py (2 issues)

1
# -*- coding: utf-8 -*-
2
# pylint: disable=attribute-defined-outside-init, invalid-name, objects-update-used
3
from xmlrpc.client import ProtocolError
4
from django.contrib.auth.models import Permission
5
6
from tcms_api import xmlrpc
7
8
from tcms.testplans.models import TestPlan
9
from tcms.testcases.models import TestCasePlan
10
11
from tcms.tests import remove_perm_from_user
12
from tcms.tests.factories import ProductFactory
13
from tcms.tests.factories import TestCaseFactory
14
from tcms.tests.factories import TestPlanFactory
15
from tcms.tests.factories import PlanTypeFactory
16
from tcms.tests.factories import TagFactory
17
from tcms.tests.factories import UserFactory
18
from tcms.tests.factories import VersionFactory
19
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
20
21
22
class TestFilter(XmlrpcAPIBaseTest):
23
24
    def _fixture_setup(self):
25
        super(TestFilter, self)._fixture_setup()
26
27
        self.product = ProductFactory()
28
        self.version = VersionFactory(product=self.product)
29
        self.tester = UserFactory()
30
        self.plan_type = PlanTypeFactory(name='manual smoking')
31
        self.plan_1 = TestPlanFactory(product_version=self.version,
32
                                      product=self.product,
33
                                      author=self.tester,
34
                                      type=self.plan_type)
35
        self.plan_2 = TestPlanFactory(product_version=self.version,
36
                                      product=self.product,
37
                                      author=self.tester,
38
                                      type=self.plan_type)
39
40
    def test_filter_plans(self):
41
        plans = self.rpc_client.exec.TestPlan.filter({'pk__in': [self.plan_1.pk, self.plan_2.pk]})
42
        plan = plans[0]
43
        self.assertEqual(self.plan_1.name, plan['name'])
44
        self.assertEqual(self.plan_1.product_version.pk, plan['product_version_id'])
45
        self.assertEqual(self.plan_1.author.pk, plan['author_id'])
46
47
    def test_filter_out_all_plans(self):
48
        plans_total = TestPlan.objects.all().count()
49
        self.assertEqual(plans_total, len(self.rpc_client.exec.TestPlan.filter()))
50
        self.assertEqual(plans_total, len(self.rpc_client.exec.TestPlan.filter({})))
51
52
53
class TestAddTag(XmlrpcAPIBaseTest):
54
55
    def _fixture_setup(self):
56
        super(TestAddTag, self)._fixture_setup()
57
58
        self.product = ProductFactory()
59
        self.plans = [
60
            TestPlanFactory(author=self.api_user, product=self.product),
61
            TestPlanFactory(author=self.api_user, product=self.product),
62
        ]
63
64
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
65
        self.tag2 = TagFactory(name='xmlrpc_test_tag_2')
66
67
    def test_add_tag(self):
68
        self.rpc_client.exec.TestPlan.add_tag(self.plans[0].pk, self.tag1.name)
69
        tag_exists = TestPlan.objects.filter(pk=self.plans[0].pk, tag__pk=self.tag1.pk).exists()
70
        self.assertTrue(tag_exists)
71
72 View Code Duplication
    def test_add_tag_without_permissions(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
73
        unauthorized_user = UserFactory()
74
        unauthorized_user.set_password('api-testing')
75
        unauthorized_user.save()
76
77
        unauthorized_user.user_permissions.add(*Permission.objects.all())
78
        remove_perm_from_user(unauthorized_user, 'testplans.add_testplantag')
79
80
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
81
                                       'api-testing',
82
                                       '%s/xml-rpc/' % self.live_server_url).server
83
84
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
85
            rpc_client.TestPlan.add_tag(self.plans[0].pk, self.tag1.name)
86
87
        # tags were not modified
88
        tag_exists = TestPlan.objects.filter(pk=self.plans[0].pk, tag__pk=self.tag1.pk).exists()
89
        self.assertFalse(tag_exists)
90
91
92
class TestRemoveTag(XmlrpcAPIBaseTest):
93
94
    def _fixture_setup(self):
95
        super(TestRemoveTag, self)._fixture_setup()
96
97
        self.product = ProductFactory()
98
        self.plans = [
99
            TestPlanFactory(author=self.api_user, product=self.product),
100
            TestPlanFactory(author=self.api_user, product=self.product),
101
        ]
102
103
        self.tag0 = TagFactory(name='xmlrpc_test_tag_0')
104
        self.tag1 = TagFactory(name='xmlrpc_test_tag_1')
105
106
        self.plans[0].add_tag(self.tag0)
107
        self.plans[1].add_tag(self.tag1)
108
109
    def test_remove_tag(self):
110
        self.rpc_client.exec.TestPlan.remove_tag(self.plans[0].pk, self.tag0.name)
111
        tag_exists = TestPlan.objects.filter(pk=self.plans[0].pk, tag__pk=self.tag0.pk).exists()
112
        self.assertFalse(tag_exists)
113
114 View Code Duplication
    def test_remove_tag_without_permissions(self):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
115
        unauthorized_user = UserFactory()
116
        unauthorized_user.set_password('api-testing')
117
        unauthorized_user.save()
118
119
        unauthorized_user.user_permissions.add(*Permission.objects.all())
120
        remove_perm_from_user(unauthorized_user, 'testplans.delete_testplantag')
121
122
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
123
                                       'api-testing',
124
                                       '%s/xml-rpc/' % self.live_server_url).server
125
126
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
127
            rpc_client.TestPlan.remove_tag(self.plans[0].pk, self.tag0.name)
128
129
        # tags were not modified
130
        tag_exists = TestPlan.objects.filter(pk=self.plans[0].pk, tag__pk=self.tag0.pk).exists()
131
        self.assertTrue(tag_exists)
132
133
        tag_exists = TestPlan.objects.filter(pk=self.plans[0].pk, tag__pk=self.tag1.pk).exists()
134
        self.assertFalse(tag_exists)
135
136
137
class TestUpdate(XmlrpcAPIBaseTest):  # pylint: disable=too-many-instance-attributes
138
    """ Tests the XMLRPM testplan.update method """
139
140
    def _fixture_setup(self):
141
        super(TestUpdate, self)._fixture_setup()
142
143
        self.product = ProductFactory()
144
        self.version = VersionFactory(product=self.product)
145
        self.tester = UserFactory()
146
        self.plan_type = PlanTypeFactory(name='manual smoking')
147
        self.plan_1 = TestPlanFactory(product_version=self.version,
148
                                      product=self.product,
149
                                      author=self.tester,
150
                                      type=self.plan_type)
151
        self.plan_2 = TestPlanFactory(product_version=self.version,
152
                                      product=self.product,
153
                                      author=self.tester,
154
                                      type=self.plan_type)
155
156
    def test_update_text(self):
157
        self.rpc_client.exec.TestPlan.update(self.plan_1.pk, {'text': 'This has been updated'})
158
        # reload from db
159
        self.plan_1.refresh_from_db()
160
        # assert
161
        self.assertEqual('This has been updated', self.plan_1.text)
162
163
164
class TestRemoveCase(XmlrpcAPIBaseTest):
165
    """ Test the XML-RPC method TestPlan.remove_case() """
166
167
    def _fixture_setup(self):
168
        super(TestRemoveCase, self)._fixture_setup()
169
        self.testcase_1 = TestCaseFactory()
170
        self.testcase_2 = TestCaseFactory()
171
        self.plan_1 = TestPlanFactory()
172
        self.plan_2 = TestPlanFactory()
173
174
        self.plan_1.add_case(self.testcase_1)
175
        self.plan_1.add_case(self.testcase_2)
176
177
        self.plan_2.add_case(self.testcase_2)
178
179
    def test_remove_case_with_single_plan(self):
180
        self.rpc_client.exec.TestPlan.remove_case(self.plan_1.pk, self.testcase_1.pk)
181
        self.assertEqual(0, self.testcase_1.plan.count())  # pylint: disable=no-member
182
183
    def test_remove_case_with_two_plans(self):
184
        self.assertEqual(2, self.testcase_2.plan.count())  # pylint: disable=no-member
185
186
        self.rpc_client.exec.TestPlan.remove_case(self.plan_1.pk, self.testcase_2.pk)
187
        self.assertEqual(1, self.testcase_2.plan.count())  # pylint: disable=no-member
188
189
190
class TestAddCase(XmlrpcAPIBaseTest):
191
    """ Test the XML-RPC method TestPlan.add_case() """
192
193
    def _fixture_setup(self):
194
        super()._fixture_setup()
195
196
        self.testcase_1 = TestCaseFactory()
197
        self.testcase_2 = TestCaseFactory()
198
        self.testcase_3 = TestCaseFactory()
199
200
        self.plan_1 = TestPlanFactory()
201
        self.plan_2 = TestPlanFactory()
202
        self.plan_3 = TestPlanFactory()
203
204
        # case 1 is already linked to plan 1
205
        self.plan_1.add_case(self.testcase_1)
206
207
    def test_ignores_existing_mappings(self):
208
        plans = [self.plan_1.pk, self.plan_2.pk, self.plan_3.pk]
209
        cases = [self.testcase_1.pk, self.testcase_2.pk, self.testcase_3.pk]
210
211
        for plan_id in plans:
212
            for case_id in cases:
213
                self.rpc_client.exec.TestPlan.add_case(plan_id, case_id)
214
215
        # no duplicates for plan1/case1 were created
216
        self.assertEqual(
217
            1,
218
            TestCasePlan.objects.filter(
219
                plan=self.plan_1.pk,
220
                case=self.testcase_1.pk
221
            ).count()
222
        )
223
224
        # verify all case/plan combinations exist
225
        for plan_id in plans:
226
            for case_id in cases:
227
                self.assertEqual(
228
                    1,
229
                    TestCasePlan.objects.filter(
230
                        plan=plan_id,
231
                        case=case_id
232
                    ).count()
233
                )
234