Fixing param-{find,show} and output-{find,show} commands

Now, the criteria option is working for both commands
and the commands are able to handle with wrong input values.

https://pagure.io/freeipa/issue/7134

Reviewed-By: Rob Crittenden <rcritten@redhat.com>
Reviewed-By: Fraser Tweedale <ftweedal@redhat.com>
This commit is contained in:
Felipe Barreto 2017-10-02 13:41:06 -03:00 committed by Tibor Dudlák
parent 7ab49dda6d
commit 3822120077
2 changed files with 148 additions and 11 deletions

View File

@ -92,6 +92,9 @@ class BaseMetaObject(Object):
args, criteria = self._split_search_args(*args)
result = self._search(*args, **kwargs)
if not result:
return tuple()
result = (self._get_obj(r, **kwargs) for r in result)
if criteria:
@ -125,8 +128,8 @@ class BaseMetaSearch(Search):
"(\"%s\")") % 'name',
)
def execute(self, criteria=None, **options):
result = list(self.obj.search(criteria, **options))
def execute(self, command, criteria=None, **options):
result = list(self.obj.search(command, criteria, **options))
return dict(result=result, count=len(result), truncated=False)
@ -627,18 +630,21 @@ class param(BaseParam):
def _retrieve(self, metaobjectfull_name, name, **kwargs):
found = False
try:
metaobj = self.api.Command[metaobjectfull_name]
plugin = self.api.Object['command']
except KeyError:
try:
metaobj = self.api.Object[metaobjectfull_name]
plugin = self.api.Object['class']
except KeyError:
pass
else:
found = True
else:
raise errors.NotFound(
reason=_("%(metaobject)s: %(oname)s not found") % {
'metaobject': metaobjectfull_name, 'oname': self.name,
}
)
if 'command' in self.api.Object:
plugin = self.api.Object['command']
found = True
elif 'class' in self.api.Object:
plugin = self.api.Object['class']
found = True
if found:
@ -728,6 +734,13 @@ class output(BaseParam):
return obj
def _retrieve(self, commandfull_name, name, **kwargs):
if not commandfull_name in self.api.Command:
raise errors.NotFound(
reason=_("%(command_name)s: %(oname)s not found") % {
'command_name': commandfull_name, 'oname': self.name,
}
)
cmd = self.api.Command[commandfull_name]
try:
return (cmd, cmd.output[name])
@ -739,6 +752,9 @@ class output(BaseParam):
)
def _search(self, commandfull_name, **kwargs):
if not commandfull_name in self.api.Command:
return None
cmd = self.api.Command[commandfull_name]
return ((cmd, output) for output in cmd.output())

View File

@ -0,0 +1,121 @@
#
# Copyright (C) 2017 FreeIPA Contributors see COPYING for license
#
"""
Test the `ipaserver/plugins/schema.py` module.
"""
import pytest
from ipalib import errors
from ipatests.test_xmlrpc.tracker.base import Tracker
from ipatests.test_xmlrpc.xmlrpc_test import XMLRPC_test
@pytest.mark.tier1
class TestParamFindAndShowCommand(XMLRPC_test):
"""Test functionality of the ipa param-{find,show} command"""
tracker = Tracker()
def test_param_find(self):
"""Test param-find command"""
# right command without criteria
result = self.tracker.run_command('param_find', u'user-add')
assert len(result['result']) != 0, result
assert result['result'][0]['name'] == 'uid', result
assert result['result'][0]['cli_name'] == 'login', result
assert result['result'][0]['label'] == 'User login', result
# right command, right criteria
criteria = u'postalcode'
result = self.tracker.run_command('param_find', u'user-add', criteria)
assert len(result['result']) != 0, result
for item in result['result']:
assert (criteria in item['name'].lower() or
criteria in item['doc'].lower()), item
# right command, wrong criteria
result = self.tracker.run_command('param_find', u'user-add', u'fake')
assert len(result['result']) == 0, result
# wrong command, wrong criteria
result = self.tracker.run_command('param_find', u'fake', u'fake')
assert len(result['result']) == 0, result
def test_param_show(self):
"""Test param-show command"""
# right command, right criteria
criteria = u'uid'
result = self.tracker.run_command('param_show', u'user-add', criteria)
assert result['result'] is not None, result
assert result['result']['name'] == 'uid', result
assert result['result']['cli_name'] == 'login', result
assert result['result']['label'] == 'User login', result
# right command without criteria
with pytest.raises(errors.RequirementError):
self.tracker.run_command('param_show', u'user-add')
# right command, wrong criteria
with pytest.raises(errors.NotFound):
self.tracker.run_command('param_show', u'user-add', u'fake')
# wrong command, wrong criteria
with pytest.raises(errors.NotFound):
self.tracker.run_command('param_show', u'fake', u'fake')
class TestOutputFindAndShowCommand(XMLRPC_test):
"""Test functionality of the ipa output-{find,show} command"""
tracker = Tracker()
def test_output_find(self):
"""Test output-find command"""
# right command without criteria
result = self.tracker.run_command('output_find', u'user-add')
assert len(result['result']) != 0, result
assert result['result'][0]['name'] == 'summary', result
assert result['result'][0]['doc'] == \
'User-friendly description of action performed', result
# right command, right criteria
criteria = u'result'
result = self.tracker.run_command('output_find', u'user-add', criteria)
assert len(result['result']) == 1, result
assert criteria in result['result'][0]['name'].lower(), result
# right command, wrong criteria
result = self.tracker.run_command('output_find', u'user-add', u'fake')
assert len(result['result']) == 0, result
# wrong command, wrong criteria
result = self.tracker.run_command('output_find', u'fake', u'fake')
assert len(result['result']) == 0, result
def test_output_show(self):
"""Test output-show command"""
# right command, right criteria
criteria = u'value'
result = self.tracker.run_command('output_show', u'user-add', criteria)
assert len(result['result']) != 0, result
assert criteria in result['result']['name'].lower(), result
assert result['result']['doc'] == \
"The primary_key value of the entry, e.g. 'jdoe' for a user", result
# right command without criteria
with pytest.raises(errors.RequirementError):
self.tracker.run_command('output_show', u'user-add')
# right command, wrong criteria
with pytest.raises(errors.NotFound):
self.tracker.run_command('output_show', u'user-add', u'fake')
# wrong command, wrong criteria
with pytest.raises(errors.NotFound):
self.tracker.run_command('output_show', u'fake', u'fake')