Skip to content

Commit

Permalink
api: Add tests for tcms.bugs.api.add_tag(). Refs kiwitcms#1597
Browse files Browse the repository at this point in the history
  • Loading branch information
mfonism committed Jun 11, 2020
1 parent b192179 commit 711bba2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
4 changes: 2 additions & 2 deletions tcms/bugs/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)


@permissions_required('bugs.add_bugtag')
@permissions_required('bugs.add_bug_tags')
@rpc_method(name='Bug.add_tag')
def add_tag(bug_id, tag, **kwargs):
"""
Expand All @@ -27,7 +27,7 @@ def add_tag(bug_id, tag, **kwargs):
:param tag: Tag name to add
:type tag: str
:return: None
:raises: PermissionDenied if missing *bugs.add_bugtag* permission
:raises: PermissionDenied if missing *bugs.add_bug_tags* permission
:raises: Bug.DoesNotExist if object specified by PK doesn't exist
:raises: Tag.DoesNotExist if missing *management.add_tag* permission and *tag*
doesn't exist in the database!
Expand Down
51 changes: 51 additions & 0 deletions tcms/bugs/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# pylint: disable=attribute-defined-outside-init

from xmlrpc.client import Fault as XmlRPCFault
from xmlrpc.client import ProtocolError

from tcms.bugs.models import Bug
from tcms.bugs.tests.factory import BugFactory
from tcms.management.models import Tag
from tcms.rpc.tests.utils import APIPermissionsTestCase
from tcms.tests import user_should_have_perm
from tcms.tests.factories import TagFactory


class TestAddTag(APIPermissionsTestCase):
"""Test Bug.add_tag"""

permission_label = 'bugs.add_bug_tags'

def _fixture_setup(self):
super()._fixture_setup()

self.bug = BugFactory()
self.tag = TagFactory(name='rhel')

def verify_api_with_permission(self):
self.rpc_client.Bug.add_tag(self.bug.pk, self.tag.name)
self.assertIn(self.tag, Bug.objects.get(pk=self.bug.pk).tags.all())

def verify_api_without_permission(self):
with self.assertRaisesRegex(ProtocolError, '403 Forbidden'):
self.rpc_client.Bug.add_tag(self.bug.pk, self.tag.name)

def test_add_tag_to_non_existent_bug(self):
user_should_have_perm(self.tester, self.permission_label)
with self.assertRaisesRegex(XmlRPCFault, 'Bug matching query does not exist'):
self.rpc_client.Bug.add_tag(-9, self.tag.name)

def test_add_non_existent_tag_with_add_tag_perm(self):
user_should_have_perm(self.tester, self.permission_label)
user_should_have_perm(self.tester, 'management.add_tag')

self.assertFalse(Tag.objects.filter(name='fedora').exists())
self.rpc_client.Bug.add_tag(self.bug.pk, 'fedora')
self.assertTrue(Tag.objects.filter(name='fedora').exists())
self.assertTrue(self.bug.tags.filter(name='fedora').exists())

def test_add_non_existent_tag_without_add_tags_perm(self):
user_should_have_perm(self.tester, self.permission_label)

with self.assertRaisesRegex(XmlRPCFault, 'Tag matching query does not exist'):
self.rpc_client.Bug.add_tag(self.bug.pk, 'fedora')

0 comments on commit 711bba2

Please sign in to comment.