From 49c74bab10237676c09ee4ec97af74f58fe291af Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Sep 13 2018 12:11:29 +0000 Subject: hub: [hasPerm] add strict behavior --- diff --git a/hub/kojihub.py b/hub/kojihub.py index 2364d0a..624d720 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -10876,9 +10876,11 @@ class RootExports(object): listBuildroots = staticmethod(query_buildroots) - def hasPerm(self, perm): + def hasPerm(self, perm, strict=False): """Check if the logged-in user has the given permission. Return False if they do not have the permission, or if they are not logged-in.""" + if strict and not lookup_perm(perm): + raise koji.GenericError('No such permission %s defined' % perm) return context.session.hasPerm(perm) def getPerms(self): diff --git a/tests/test_hub/test_perm_operations.py b/tests/test_hub/test_perm_operations.py new file mode 100644 index 0000000..fb8a584 --- /dev/null +++ b/tests/test_hub/test_perm_operations.py @@ -0,0 +1,30 @@ +from __future__ import absolute_import + +import mock + +try: + import unittest2 as unittest +except ImportError: + import unittest + +import koji +import kojihub + + +class TestPerm(unittest.TestCase): + + @mock.patch('kojihub.context') + @mock.patch('kojihub.lookup_perm', return_value=None) + def test_has_perm(self, lookup_perm, context): + rv = kojihub.RootExports().hasPerm('perm') + self.assertEqual(rv, context.session.hasPerm.return_value) + lookup_perm.assert_not_called() + context.session.hasPerm.assert_called_once_with('perm') + + context.session.hasPerm.reset_mock() + with self.assertRaises(koji.GenericError) as cm: + kojihub.RootExports().hasPerm('perm', strict=True) + lookup_perm.assert_called_once_with('perm') + context.session.hasPerm.assert_not_called() + self.assertEqual(cm.exception.args[0], + 'No such permission perm defined')