Add and upfate CLI unit tests

Fixes: https://pagure.io/koji/issue/729
This commit is contained in:
Jana Cupova 2021-11-04 10:00:29 +01:00 committed by Tomas Kopecek
parent ae15fa34ba
commit c3e89500f2
26 changed files with 1573 additions and 1537 deletions

View file

@ -2,6 +2,7 @@ from __future__ import absolute_import
import mock
import six
import koji
from koji_cli.commands import handle_block_group
from . import utils
@ -12,123 +13,108 @@ class TestBlockGroup(utils.CliTestCase):
maxDiff = None
def setUp(self):
self.maxDiff = None
self.options = mock.MagicMock()
self.options.debug = False
self.session = mock.MagicMock()
self.session.getAPIVersion.return_value = koji.API_VERSION
self.activate_session_mock = mock.patch('koji_cli.commands.activate_session').start()
self.error_format = """Usage: %s block-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.stderr', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_block_group_nonexistent_tag(self, activate_session_mock, stderr):
def test_handle_block_group_nonexistent_tag(self):
tag = 'nonexistent-tag'
group = 'group'
arguments = [tag, group]
options = mock.MagicMock()
# Mock out the xmlrpc server
session = mock.MagicMock()
session.hasPerm.return_value = True
session.getTag.return_value = None
self.session.hasPerm.return_value = True
self.session.getTag.return_value = None
# Run it and check immediate output
with self.assertRaises(SystemExit) as ex:
handle_block_group(options, session, arguments)
self.assertExitCode(ex, 1)
actual = stderr.getvalue()
expected = 'No such tag: %s\n' % tag
self.assertMultiLineEqual(actual, expected)
self.assert_system_exit(
handle_block_group,
self.options, self.session, arguments,
stderr='No such tag: %s\n' % tag,
stdout='',
activate_session=None,
exit_code=1)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_called_once_with(session, options)
session.hasPerm.assert_called_once_with('admin')
session.getTag.assert_called_once_with(tag)
session.getTagGroups.assert_not_called()
session.groupListBlock.assert_not_called()
self.activate_session_mock.assert_called_once_with(self.session, self.options)
self.session.hasPerm.assert_called_once_with('admin')
self.session.getTag.assert_called_once_with(tag)
self.session.getTagGroups.assert_not_called()
self.session.groupListBlock.assert_not_called()
@mock.patch('sys.stderr', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_block_group_nonexistent_group(self, activate_session_mock, stderr):
def test_handle_block_group_nonexistent_group(self):
tag = 'tag'
group = 'group'
arguments = [tag, group]
options = mock.MagicMock()
# Mock out the xmlrpc server
session = mock.MagicMock()
session.hasPerm.return_value = True
session.getTag.return_value = tag
session.getTagGroups.return_value = []
self.session.hasPerm.return_value = True
self.session.getTag.return_value = tag
self.session.getTagGroups.return_value = []
# Run it and check immediate output
with self.assertRaises(SystemExit) as ex:
handle_block_group(options, session, arguments)
self.assertExitCode(ex, 1)
actual = stderr.getvalue()
expected = "Group %s doesn't exist within tag %s\n" % (group, tag)
self.assertMultiLineEqual(actual, expected)
self.assert_system_exit(
handle_block_group,
self.options, self.session, arguments,
stderr="Group %s doesn't exist within tag %s\n" % (group, tag),
stdout='',
activate_session=None,
exit_code=1)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_called_once_with(session, options)
session.hasPerm.assert_called_once_with('admin')
session.getTag.assert_called_once_with(tag)
session.getTagGroups.assert_called_once_with(tag, inherit=False)
session.groupListBlock.assert_not_called()
self.activate_session_mock.assert_called_once_with(self.session, self.options)
self.session.hasPerm.assert_called_once_with('admin')
self.session.getTag.assert_called_once_with(tag)
self.session.getTagGroups.assert_called_once_with(tag, inherit=False)
self.session.groupListBlock.assert_not_called()
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_block_group(self, activate_session_mock, stdout):
def test_handle_block_group(self, stdout):
tag = 'tag'
group = 'group'
arguments = [tag, group]
options = mock.MagicMock()
# Mock out the xmlrpc server
session = mock.MagicMock()
session.hasPerm.return_value = True
session.getTag.return_value = tag
session.getTagGroups.return_value = [
self.session.hasPerm.return_value = True
self.session.getTag.return_value = tag
self.session.getTagGroups.return_value = [
{'name': 'group', 'group_id': 'groupId'}]
# Run it and check immediate output
rv = handle_block_group(options, session, arguments)
rv = handle_block_group(self.options, self.session, arguments)
actual = stdout.getvalue()
expected = ''
self.assertMultiLineEqual(actual, expected)
# Finally, assert that things were called as we expected.
activate_session_mock.assert_called_once_with(session, options)
session.hasPerm.assert_called_once_with('admin')
session.getTag.assert_called_once_with(tag)
session.getTagGroups.assert_called_once_with(tag, inherit=False)
session.groupListBlock.assert_called_once_with(tag, group)
self.activate_session_mock.assert_called_once_with(self.session, self.options)
self.session.hasPerm.assert_called_once_with('admin')
self.session.getTag.assert_called_once_with(tag)
self.session.getTagGroups.assert_called_once_with(tag, inherit=False)
self.session.groupListBlock.assert_called_once_with(tag, group)
self.assertEqual(rv, None)
@mock.patch('sys.stdout', new_callable=six.StringIO)
@mock.patch('koji_cli.commands.activate_session')
def test_handle_block_group_error_handling(self, activate_session_mock, stdout):
session = mock.MagicMock()
options = mock.MagicMock()
def test_handle_block_group_error_handling(self):
expected = self.format_error_message(
"Please specify a tag name and a group name")
"Please specify a tag name and a group name")
for args in [[], ['tag'], ['tag', 'grp', 'etc']]:
self.assert_system_exit(
handle_block_group,
options,
session,
args,
self.options, self.session, args,
stderr=expected,
stdout='',
activate_session=None,
exit_code=2)
# if we don't have 'admin' permission
session.hasPerm.return_value = False
self.session.hasPerm.return_value = False
self.assert_system_exit(
handle_block_group,
options, session, ['tag', 'grp'],
self.options, self.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)
self.activate_session_mock.assert_called_with(self.session, self.options)