From 711bba27b0252b54ca8e905a87b2f443fafe418f Mon Sep 17 00:00:00 2001 From: Mfon Eti-mfon Date: Thu, 11 Jun 2020 13:13:10 +0100 Subject: [PATCH] api: Add tests for tcms.bugs.api.add_tag(). Refs #1597 --- tcms/bugs/api.py | 4 +-- tcms/bugs/tests/test_api.py | 51 +++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tcms/bugs/tests/test_api.py diff --git a/tcms/bugs/api.py b/tcms/bugs/api.py index 8cd441bab5..54851097dc 100644 --- a/tcms/bugs/api.py +++ b/tcms/bugs/api.py @@ -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): """ @@ -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! diff --git a/tcms/bugs/tests/test_api.py b/tcms/bugs/tests/test_api.py new file mode 100644 index 0000000000..0b72fa9dd4 --- /dev/null +++ b/tcms/bugs/tests/test_api.py @@ -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')