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

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

1
# -*- coding: utf-8 -*-
2
# pylint: disable=attribute-defined-outside-init
3
4
from xmlrpc.client import Fault, ProtocolError
5
from django.contrib.auth.models import Permission
6
7
from tcms_api import xmlrpc
8
9
from tcms.testcases.models import TestCase
10
11
from tcms.tests import remove_perm_from_user
12
from tcms.tests.factories import TestCaseFactory
13
from tcms.tests.factories import CategoryFactory
14
from tcms.tests.factories import ComponentFactory
15
from tcms.tests.factories import TestPlanFactory
16
from tcms.tests.factories import ProductFactory
17
from tcms.tests.factories import TagFactory
18
from tcms.tests.factories import UserFactory
19
from tcms.tests.factories import VersionFactory
20
from tcms.xmlrpc.tests.utils import XmlrpcAPIBaseTest
21
22
23
class TestNotificationRemoveCC(XmlrpcAPIBaseTest):
24
    """ Tests the XML-RPC testcase.notication_remove_cc method """
25
26
    def _fixture_setup(self):
27
        super(TestNotificationRemoveCC, self)._fixture_setup()
28
29
        self.default_cc = '[email protected]'
30
        self.testcase = TestCaseFactory()
31
        self.testcase.emailing.add_cc(self.default_cc)
32
33
    def tearDown(self):
34
        super(TestNotificationRemoveCC, self).tearDown()
35
        self.rpc_client.exec.Auth.logout()
36
37
    def test_remove_existing_cc(self):
38
        # initially testcase has the default CC listed
39
        # and we issue XMLRPC request to remove the cc
40
        self.rpc_client.exec.TestCase.remove_notification_cc(self.testcase.pk, [self.default_cc])
41
42
        # now verify that the CC email has been removed
43
        self.testcase.emailing.refresh_from_db()
44
        self.assertEqual([], self.testcase.emailing.get_cc_list())
45
46
47
class TestFilterCases(XmlrpcAPIBaseTest):
48
49
    def _fixture_setup(self):
50
        super(TestFilterCases, self)._fixture_setup()
51
52
        self.tester = UserFactory(username='great tester')
53
        self.product = ProductFactory(name='StarCraft')
54
        self.version = VersionFactory(value='0.1', product=self.product)
55
        self.plan = TestPlanFactory(name='Test product.get_cases',
56
                                    author=self.tester,
57
                                    product=self.product,
58
                                    product_version=self.version)
59
        self.case_category = CategoryFactory(product=self.product)
60
        self.cases_count = 10
61
        self.cases = []
62
        for _ in range(self.cases_count):
63
            test_case = TestCaseFactory(
64
                category=self.case_category,
65
                author=self.tester,
66
                reviewer=self.tester,
67
                default_tester=None,
68
                plan=[self.plan])
69
            self.cases.append(test_case)
70
71
    def test_filter_by_product_id(self):
72
        cases = self.rpc_client.exec.TestCase.filter({'category__product': self.product.pk})
73
        self.assertIsNotNone(cases)
74
        self.assertEqual(len(cases), self.cases_count)
75
76
77
class TestUpdate(XmlrpcAPIBaseTest):
78
79
    def _fixture_setup(self):
80
        super(TestUpdate, self)._fixture_setup()
81
82
        self.testcase = TestCaseFactory(text='')
83
84
    def test_update_text_and_product(self):
85
        self.assertEqual('', self.testcase.text)
86
87
        # update the test case
88
        updated = self.rpc_client.exec.TestCase.update(  # pylint: disable=objects-update-used
89
            self.testcase.pk,
90
            {
91
                'summary': 'This was updated',
92
                'text': 'new TC text',
93
            }
94
        )
95
        self.testcase.refresh_from_db()  # refresh before assertions
96
97
        self.assertEqual(updated['case_id'], self.testcase.pk)
98
        self.assertEqual('This was updated', self.testcase.summary)
99
        self.assertEqual('new TC text', self.testcase.text)
100
101
102 View Code Duplication
class TestAddTag(XmlrpcAPIBaseTest):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
103
104
    def _fixture_setup(self):
105
        super()._fixture_setup()
106
107
        self.testcase = TestCaseFactory()
108
109
        self.tag1 = TagFactory()
110
        self.tag2 = TagFactory()
111
112
    def test_add_tag(self):
113
        self.rpc_client.exec.TestCase.add_tag(self.testcase.pk, self.tag1.name)
114
        tag_exists = TestCase.objects.filter(pk=self.testcase.pk, tag__pk=self.tag1.pk).exists()
115
        self.assertTrue(tag_exists)
116
117
    def test_add_tag_without_permissions(self):
118
        unauthorized_user = UserFactory()
119
        unauthorized_user.set_password('api-testing')
120
        unauthorized_user.save()
121
122
        unauthorized_user.user_permissions.add(*Permission.objects.all())
123
        remove_perm_from_user(unauthorized_user, 'testcases.add_testcasetag')
124
125
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
126
                                       'api-testing',
127
                                       '%s/xml-rpc/' % self.live_server_url).server
128
129
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
130
            rpc_client.TestCase.add_tag(self.testcase.pk, self.tag1.name)
131
132
        # tags were not modified
133
        tag_exists = TestCase.objects.filter(pk=self.testcase.pk, tag__pk=self.tag1.pk).exists()
134
        self.assertFalse(tag_exists)
135
136
137 View Code Duplication
class TestRemoveTag(XmlrpcAPIBaseTest):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
138
139
    def _fixture_setup(self):
140
        super()._fixture_setup()
141
142
        self.tag0 = TagFactory()
143
        self.tag1 = TagFactory()
144
145
        self.testcase = TestCaseFactory()
146
        self.testcase.add_tag(self.tag0)
147
148
    def test_remove_tag(self):
149
        self.rpc_client.exec.TestCase.remove_tag(self.testcase.pk, self.tag0.name)
150
        tag_exists = TestCase.objects.filter(pk=self.testcase.pk, tag__pk=self.tag0.pk).exists()
151
        self.assertFalse(tag_exists)
152
153
    def test_remove_tag_without_permissions(self):
154
        unauthorized_user = UserFactory()
155
        unauthorized_user.set_password('api-testing')
156
        unauthorized_user.save()
157
158
        unauthorized_user.user_permissions.add(*Permission.objects.all())
159
        remove_perm_from_user(unauthorized_user, 'testcases.delete_testcasetag')
160
161
        rpc_client = xmlrpc.TCMSXmlrpc(unauthorized_user.username,
162
                                       'api-testing',
163
                                       '%s/xml-rpc/' % self.live_server_url).server
164
165
        with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
166
            rpc_client.TestCase.remove_tag(self.testcase.pk, self.tag0.name)
167
168
        # tags were not modified
169
        tag_exists = TestCase.objects.filter(pk=self.testcase.pk, tag__pk=self.tag0.pk).exists()
170
        self.assertTrue(tag_exists)
171
172
        tag_exists = TestCase.objects.filter(pk=self.testcase.pk, tag__pk=self.tag1.pk).exists()
173
        self.assertFalse(tag_exists)
174
175
176
class TestAddComponent(XmlrpcAPIBaseTest):
177
178
    def _fixture_setup(self):
179
        super()._fixture_setup()
180
        self.test_case = TestCaseFactory()
181
        self.good_component = ComponentFactory(product=self.test_case.category.product)
182
        self.bad_component = ComponentFactory()
183
184
    def test_add_component_from_same_product_is_allowed(self):
185
        result = self.rpc_client.exec.TestCase.add_component(self.test_case.pk,
186
                                                             self.good_component.name)
187
        self.assertEqual(result['component'][0], self.good_component.pk)
188
189
    def test_add_component_from_another_product_is_not_allowed(self):
190
        with self.assertRaisesRegex(Fault, 'Component matching query does not exist'):
191
            self.rpc_client.exec.TestCase.add_component(self.test_case.pk, self.bad_component.name)
192