1
|
|
|
# pylint: disable=attribute-defined-outside-init |
2
|
|
|
# pylint: disable=wrong-import-position |
3
|
|
|
import unittest |
4
|
|
|
from xmlrpc.client import Fault as XmlRPCFault |
5
|
|
|
from xmlrpc.client import ProtocolError |
6
|
|
|
|
7
|
|
|
from django.conf import settings |
8
|
|
|
|
9
|
|
|
if "tcms.bugs.apps.AppConfig" not in settings.INSTALLED_APPS: |
10
|
|
|
raise unittest.SkipTest("tcms.bugs is disabled") |
11
|
|
|
|
12
|
|
|
from tcms.bugs.models import Bug # noqa: E402 |
13
|
|
|
from tcms.bugs.tests.factory import BugFactory # noqa: E402 |
14
|
|
|
from tcms.rpc.tests.utils import APITestCase, APIPermissionsTestCase # noqa: E402 |
15
|
|
|
from tcms.tests.factories import TagFactory # noqa: E402 |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
class TestAddTagPermissions(APIPermissionsTestCase): |
19
|
|
|
"""Test Bug.add_tag""" |
20
|
|
|
|
21
|
|
|
permission_label = "bugs.add_bug_tags" |
22
|
|
|
|
23
|
|
|
def _fixture_setup(self): |
24
|
|
|
super()._fixture_setup() |
25
|
|
|
|
26
|
|
|
self.bug = BugFactory() |
27
|
|
|
self.tag = TagFactory() |
28
|
|
|
|
29
|
|
|
def verify_api_with_permission(self): |
30
|
|
|
self.rpc_client.Bug.add_tag(self.bug.pk, self.tag.name) |
31
|
|
|
self.assertIn(self.tag, self.bug.tags.all()) |
32
|
|
|
|
33
|
|
|
def verify_api_without_permission(self): |
34
|
|
|
with self.assertRaisesRegex(ProtocolError, "403 Forbidden"): |
35
|
|
|
self.rpc_client.Bug.add_tag(self.bug.pk, self.tag.name) |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
class TestAddTag(APITestCase): |
39
|
|
|
"""Test Bug.add_tag""" |
40
|
|
|
|
41
|
|
|
def _fixture_setup(self): |
42
|
|
|
super()._fixture_setup() |
43
|
|
|
|
44
|
|
|
self.bug = BugFactory() |
45
|
|
|
self.tag = TagFactory() |
46
|
|
|
|
47
|
|
|
def test_add_tag_to_non_existent_bug(self): |
48
|
|
|
with self.assertRaisesRegex(XmlRPCFault, 'Bug matching query does not exist'): |
49
|
|
|
self.rpc_client.Bug.add_tag(-9, self.tag.name) |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
class TestRemoveTagPermissions(APIPermissionsTestCase): |
53
|
|
|
"""Test Bug.remove_tag""" |
54
|
|
|
|
55
|
|
|
permission_label = "bugs.delete_bug_tags" |
56
|
|
|
|
57
|
|
|
def _fixture_setup(self): |
58
|
|
|
super()._fixture_setup() |
59
|
|
|
|
60
|
|
|
self.bug = BugFactory() |
61
|
|
|
self.tag = TagFactory() |
62
|
|
|
self.bug.tags.add(self.tag) |
63
|
|
|
|
64
|
|
|
def verify_api_with_permission(self): |
65
|
|
|
self.rpc_client.Bug.remove_tag(self.bug.pk, self.tag.name) |
66
|
|
|
self.assertNotIn(self.tag, self.bug.tags.all()) |
67
|
|
|
|
68
|
|
|
def verify_api_without_permission(self): |
69
|
|
|
with self.assertRaisesRegex(ProtocolError, "403 Forbidden"): |
70
|
|
|
self.rpc_client.Bug.remove_tag(self.bug.pk, self.tag.name) |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
class TestRemovePermissions(APIPermissionsTestCase): |
74
|
|
|
"""Test permissions of Bug.remove""" |
75
|
|
|
|
76
|
|
|
permission_label = "bugs.delete_bug" |
77
|
|
|
|
78
|
|
|
def _fixture_setup(self): |
79
|
|
|
super()._fixture_setup() |
80
|
|
|
|
81
|
|
|
self.bug = BugFactory() |
82
|
|
|
self.another_bug = BugFactory() |
83
|
|
|
self.yet_another_bug = BugFactory() |
84
|
|
|
|
85
|
|
|
def verify_api_with_permission(self): |
86
|
|
|
self.rpc_client.Bug.remove({"pk__in": [self.bug.pk, self.another_bug.pk]}) |
87
|
|
|
|
88
|
|
|
bugs = Bug.objects.all() |
89
|
|
|
self.assertNotIn(self.bug, bugs) |
90
|
|
|
self.assertNotIn(self.another_bug, bugs) |
91
|
|
|
self.assertIn(self.yet_another_bug, bugs) |
92
|
|
|
|
93
|
|
|
def verify_api_without_permission(self): |
94
|
|
|
with self.assertRaisesRegex(ProtocolError, "403 Forbidden"): |
95
|
|
|
self.rpc_client.Bug.remove({"pk__in": [self.bug.pk, self.another_bug.pk]}) |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
class TestFilter(APITestCase): |
99
|
|
|
"""Test Bug.filter""" |
100
|
|
|
|
101
|
|
|
def _fixture_setup(self): |
102
|
|
|
super()._fixture_setup() |
103
|
|
|
|
104
|
|
|
self.bug = BugFactory(status=False) |
105
|
|
|
self.another_bug = BugFactory(status=True) |
106
|
|
|
self.yet_another_bug = BugFactory(status=True) |
107
|
|
|
|
108
|
|
|
def test_filter(self): |
109
|
|
|
result = self.rpc_client.Bug.filter({"status": True}) |
110
|
|
|
self.assertIsInstance(result, list) |
111
|
|
|
self.assertEqual(len(result), 2) |
112
|
|
|
|
113
|
|
|
pks = [] |
114
|
|
|
for item in result: |
115
|
|
|
pks.append(item["pk"]) |
116
|
|
|
|
117
|
|
|
self.assertNotIn(self.bug.pk, pks) |
118
|
|
|
self.assertIn(self.another_bug.pk, pks) |
119
|
|
|
self.assertIn(self.yet_another_bug.pk, pks) |
120
|
|
|
|
121
|
|
|
def test_filter_non_existing(self): |
122
|
|
|
result = self.rpc_client.Bug.filter({"pk": -99}) |
123
|
|
|
self.assertEqual(len(result), 0) |
124
|
|
|
|