unify return values for permission denied

Fixes: https://pagure.io/koji/issue/1776
This commit is contained in:
Tomas Kopecek 2019-11-08 09:06:11 -05:00
parent 5c1c25db7e
commit bf45d6927f
5 changed files with 78 additions and 72 deletions

View file

@ -1,21 +1,27 @@
from __future__ import absolute_import
import mock
import os
import six
import sys
try:
import unittest2 as unittest
except ImportError:
import unittest
from koji_cli.commands import handle_add_group
from . import utils
class TestAddGroup(unittest.TestCase):
class TestAddGroup(utils.CliTestCase):
# Show long diffs in error output...
maxDiff = None
def setUp(self):
self.error_format = """Usage: %s add-group <tag> <group>
(Specify the --help global option for a list of other help options)
%s: error: {message}
""" % (self.progname, self.progname)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_add_group(self, activate_session_mock, stdout):
@ -89,19 +95,13 @@ class TestAddGroup(unittest.TestCase):
session = mock.MagicMock()
# Run it and check immediate output
with self.assertRaises(SystemExit) as cm:
rv = handle_add_group(options, session, arguments)
actual_stdout = stdout.getvalue()
actual_stderr = stderr.getvalue()
expected_stdout = ''
progname = os.path.basename(sys.argv[0]) or 'koji'
expected_stderr = """Usage: %s add-group <tag> <group>
(Specify the --help global option for a list of other help options)
%s: error: Please specify a tag name and a group name
""" % (progname, progname)
self.assertMultiLineEqual(actual_stdout, expected_stdout)
self.assertMultiLineEqual(actual_stderr, expected_stderr)
self.assert_system_exit(
handle_add_group,
options, session, arguments,
stdout='',
stderr=self.format_error_message("Please specify a tag name and a group name"),
exit_code=2,
activate_session=None)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_not_called()
@ -109,10 +109,6 @@ class TestAddGroup(unittest.TestCase):
session.getTag.assert_not_called()
session.getTagGroups.assert_not_called()
session.groupListAdd.assert_not_called()
if isinstance(cm.exception, int):
self.assertEqual(cm.exception, 2)
else:
self.assertEqual(cm.exception.code, 2)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
@ -127,10 +123,13 @@ class TestAddGroup(unittest.TestCase):
session.hasPerm.return_value = False
# Run it and check immediate output
rv = handle_add_group(options, session, arguments)
actual = stdout.getvalue()
expected = 'This action requires tag or admin privileges\n'
self.assertMultiLineEqual(actual, expected)
self.assert_system_exit(
handle_add_group,
options, session, arguments,
stdout='',
stderr=self.format_error_message('This action requires tag or admin privileges'),
activate_session=None,
exit_code=2)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_called_once_with(session, options)
@ -139,7 +138,6 @@ class TestAddGroup(unittest.TestCase):
session.getTag.assert_not_called()
session.getTagGroups.assert_not_called()
session.groupListAdd.assert_not_called()
self.assertEqual(rv, 1)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')

View file

@ -44,10 +44,13 @@ class TestAddTag(utils.CliTestCase):
activate_session=None)
# Case 2. not admin account
expected = "This action requires tag or admin privileges\n"
session.hasPerm.return_value = None
handle_add_tag(options, session, ['test-tag'])
self.assert_console_message(stdout, expected)
self.assert_system_exit(
handle_add_tag,
options, session, ['test-tag'],
stdout='',
stderr=self.format_error_message("This action requires tag or admin privileges"),
)
# Case 3. options test
arguments = ['test-tag',

View file

@ -10,13 +10,21 @@ except ImportError:
import koji
from koji_cli.commands import handle_assign_task
from . import utils
class TestAssignTask(unittest.TestCase):
class TestAssignTask(utils.CliTestCase):
# Show long diffs in error output...
maxDiff = None
def setUp(self):
self.error_format = """Usage: %s assign-task <task_id> <hostname>
(Specify the --help global option for a list of other help options)
%s: error: {message}
""" % (self.progname, self.progname)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_assign_task(
@ -43,10 +51,11 @@ class TestAssignTask(unittest.TestCase):
arguments.append("--force")
session.getHost.return_value = hostname
session.hasPerm.return_value = False
handle_assign_task(options, session, arguments)
actual = stdout.getvalue()
expected = "This action requires admin privileges\n"
self.assertMultiLineEqual(actual, expected)
self.assert_system_exit(
handle_assign_task,
options, session, arguments,
stderr=self.format_error_message("This action requires admin privileges")
)
# Clean stdout buffer
stdout.truncate(0)
@ -82,33 +91,24 @@ class TestAssignTask(unittest.TestCase):
self, activate_session_mock, stderr, stdout):
arguments = []
options = mock.MagicMock()
progname = os.path.basename(sys.argv[0]) or 'koji'
# Mock out the xmlrpc server
session = mock.MagicMock()
# Run it and check immediate output
with self.assertRaises(SystemExit) as cm:
handle_assign_task(options, session, arguments)
actual_stdout = stdout.getvalue()
actual_stderr = stderr.getvalue()
expected_stdout = ''
expected_stderr = """Usage: %s assign-task task_id hostname
(Specify the --help global option for a list of other help options)
%s: error: please specify a task id and a hostname
""" % (progname, progname)
self.assertMultiLineEqual(actual_stdout, expected_stdout)
self.assertMultiLineEqual(actual_stderr, expected_stderr)
self.assert_system_exit(
handle_assign_task,
options, session, arguments,
stdout='',
stderr=self.format_error_message('please specify a task id and a hostname'),
activate_session=None,
exit_code=2
)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_not_called()
session.hasHost.assert_not_called()
session.addHost.assert_not_called()
if isinstance(cm.exception, int):
self.assertEqual(cm.exception, 2)
else:
self.assertEqual(cm.exception.code, 2)
if __name__ == '__main__':

View file

@ -117,12 +117,16 @@ class TestBlockGroup(utils.CliTestCase):
session,
args,
stderr=expected,
activate_session=None)
activate_session=None,
exit_code=2)
# if we don't have 'admin' permission
session.hasPerm.return_value = False
rv = handle_block_group(options, session, ['tag', 'grp'])
self.assert_console_message(
stdout, 'This action requires tag or admin privileges\n')
self.assertEqual(rv, 1)
self.assert_system_exit(
handle_block_group,
options, session, ['tag', 'grp'],
stderr=self.format_error_message('This action requires tag or admin privileges'),
stdout='',
exit_code=2,
activate_session=None)
activate_session_mock.assert_called_with(session, options)