2008-08-05 02:39:50 -05:00
|
|
|
# Authors:
|
|
|
|
# Jason Gerard DeRose <jderose@redhat.com>
|
|
|
|
#
|
|
|
|
# Copyright (C) 2008 Red Hat
|
|
|
|
# see file 'COPYING' for use and warranty information
|
|
|
|
#
|
2010-12-09 06:59:11 -06:00
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
2008-08-05 02:39:50 -05:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2010-12-09 06:59:11 -06:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2008-08-05 02:39:50 -05:00
|
|
|
|
|
|
|
"""
|
2008-10-07 22:25:23 -05:00
|
|
|
Test the `ipalib.frontend` module.
|
2008-08-05 02:39:50 -05:00
|
|
|
"""
|
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
import pytest
|
2015-09-11 06:43:28 -05:00
|
|
|
import six
|
|
|
|
|
2014-12-16 07:45:37 -06:00
|
|
|
from ipatests.util import raises, read_only
|
|
|
|
from ipatests.util import ClassChecker, create_test_api
|
2013-05-21 06:40:27 -05:00
|
|
|
from ipatests.util import assert_equal
|
2009-01-14 14:17:30 -06:00
|
|
|
from ipalib.constants import TYPE_ERROR
|
2009-01-22 10:58:35 -06:00
|
|
|
from ipalib.base import NameSpace
|
2009-04-23 07:51:59 -05:00
|
|
|
from ipalib import frontend, backend, plugable, errors, parameters, config
|
2012-12-07 09:54:07 -06:00
|
|
|
from ipalib import output, messages
|
2010-02-10 22:15:47 -06:00
|
|
|
from ipalib.parameters import Str
|
2011-01-13 13:29:16 -06:00
|
|
|
from ipapython.version import API_VERSION
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2015-09-11 06:43:28 -05:00
|
|
|
if six.PY3:
|
|
|
|
unicode = str
|
|
|
|
|
|
|
|
|
2015-04-24 07:39:48 -05:00
|
|
|
pytestmark = pytest.mark.tier0
|
|
|
|
|
|
|
|
|
2008-08-06 23:51:21 -05:00
|
|
|
def test_RULE_FLAG():
|
2008-09-23 19:01:29 -05:00
|
|
|
assert frontend.RULE_FLAG == 'validation_rule'
|
2008-08-06 23:51:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_rule():
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.rule` function.
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
flag = frontend.RULE_FLAG
|
|
|
|
rule = frontend.rule
|
2008-08-08 12:11:29 -05:00
|
|
|
def my_func():
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
assert not hasattr(my_func, flag)
|
|
|
|
rule(my_func)
|
|
|
|
assert getattr(my_func, flag) is True
|
|
|
|
@rule
|
|
|
|
def my_func2():
|
2008-08-08 16:40:03 -05:00
|
|
|
pass
|
2008-08-08 12:11:29 -05:00
|
|
|
assert getattr(my_func2, flag) is True
|
2008-08-06 23:51:21 -05:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_rule():
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.is_rule` function.
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
is_rule = frontend.is_rule
|
|
|
|
flag = frontend.RULE_FLAG
|
2008-08-06 23:51:21 -05:00
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class no_call:
|
2008-08-08 16:40:03 -05:00
|
|
|
def __init__(self, value):
|
|
|
|
if value is not None:
|
|
|
|
assert value in (True, False)
|
|
|
|
setattr(self, flag, value)
|
2008-08-06 23:51:21 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
class call(no_call):
|
2008-08-08 16:40:03 -05:00
|
|
|
def __call__(self):
|
|
|
|
pass
|
2008-08-06 23:51:21 -05:00
|
|
|
|
2008-08-08 12:11:29 -05:00
|
|
|
assert is_rule(call(True))
|
|
|
|
assert not is_rule(no_call(True))
|
|
|
|
assert not is_rule(call(False))
|
|
|
|
assert not is_rule(call(None))
|
2008-08-06 23:51:21 -05:00
|
|
|
|
|
|
|
|
2009-05-20 16:19:09 -05:00
|
|
|
class test_HasParam(ClassChecker):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Command` class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_cls = frontend.HasParam
|
|
|
|
|
|
|
|
def test_get_param_iterable(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.HasParam._get_param_iterable` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-05-20 16:19:09 -05:00
|
|
|
class WithTuple(self.cls):
|
|
|
|
takes_stuff = ('one', 'two')
|
2015-06-22 05:58:43 -05:00
|
|
|
o = WithTuple(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
assert o._get_param_iterable('stuff') is WithTuple.takes_stuff
|
|
|
|
|
|
|
|
junk = ('three', 'four')
|
|
|
|
class WithCallable(self.cls):
|
|
|
|
def takes_stuff(self):
|
|
|
|
return junk
|
2015-06-22 05:58:43 -05:00
|
|
|
o = WithCallable(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
assert o._get_param_iterable('stuff') is junk
|
|
|
|
|
|
|
|
class WithParam(self.cls):
|
|
|
|
takes_stuff = parameters.Str('five')
|
2015-06-22 05:58:43 -05:00
|
|
|
o = WithParam(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
assert o._get_param_iterable('stuff') == (WithParam.takes_stuff,)
|
|
|
|
|
|
|
|
class WithStr(self.cls):
|
|
|
|
takes_stuff = 'six'
|
2015-06-22 05:58:43 -05:00
|
|
|
o = WithStr(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
assert o._get_param_iterable('stuff') == ('six',)
|
|
|
|
|
|
|
|
class Wrong(self.cls):
|
|
|
|
takes_stuff = ['seven', 'eight']
|
2015-06-22 05:58:43 -05:00
|
|
|
o = Wrong(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
e = raises(TypeError, o._get_param_iterable, 'stuff')
|
|
|
|
assert str(e) == '%s.%s must be a tuple, callable, or spec; got %r' % (
|
|
|
|
'Wrong', 'takes_stuff', Wrong.takes_stuff
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filter_param_by_context(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.HasParam._filter_param_by_context` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-05-20 16:19:09 -05:00
|
|
|
class Example(self.cls):
|
|
|
|
def get_stuff(self):
|
|
|
|
return (
|
|
|
|
'one', # Make sure create_param() is called for each spec
|
|
|
|
'two',
|
|
|
|
parameters.Str('three', include='cli'),
|
|
|
|
parameters.Str('four', exclude='server'),
|
|
|
|
parameters.Str('five', exclude=['whatever', 'cli']),
|
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
o = Example(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
|
|
|
|
# Test when env is None:
|
|
|
|
params = list(o._filter_param_by_context('stuff'))
|
|
|
|
assert list(p.name for p in params) == [
|
|
|
|
'one', 'two', 'three', 'four', 'five'
|
|
|
|
]
|
|
|
|
for p in params:
|
|
|
|
assert type(p) is parameters.Str
|
|
|
|
|
|
|
|
# Test when env.context == 'cli':
|
|
|
|
cli = config.Env(context='cli')
|
|
|
|
assert cli.context == 'cli'
|
|
|
|
params = list(o._filter_param_by_context('stuff', cli))
|
|
|
|
assert list(p.name for p in params) == ['one', 'two', 'three', 'four']
|
|
|
|
for p in params:
|
|
|
|
assert type(p) is parameters.Str
|
|
|
|
|
|
|
|
# Test when env.context == 'server'
|
|
|
|
server = config.Env(context='server')
|
|
|
|
assert server.context == 'server'
|
|
|
|
params = list(o._filter_param_by_context('stuff', server))
|
|
|
|
assert list(p.name for p in params) == ['one', 'two', 'five']
|
|
|
|
for p in params:
|
|
|
|
assert type(p) is parameters.Str
|
|
|
|
|
|
|
|
# Test with no get_stuff:
|
|
|
|
class Missing(self.cls):
|
|
|
|
pass
|
2015-06-22 05:58:43 -05:00
|
|
|
o = Missing(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
gen = o._filter_param_by_context('stuff')
|
|
|
|
e = raises(NotImplementedError, list, gen)
|
|
|
|
assert str(e) == 'Missing.get_stuff()'
|
|
|
|
|
|
|
|
# Test when get_stuff is not callable:
|
|
|
|
class NotCallable(self.cls):
|
|
|
|
get_stuff = ('one', 'two')
|
2015-06-22 05:58:43 -05:00
|
|
|
o = NotCallable(api)
|
2009-05-20 16:19:09 -05:00
|
|
|
gen = o._filter_param_by_context('stuff')
|
|
|
|
e = raises(TypeError, list, gen)
|
|
|
|
assert str(e) == '%s.%s must be a callable; got %r' % (
|
|
|
|
'NotCallable', 'get_stuff', NotCallable.get_stuff
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2008-09-02 18:40:44 -05:00
|
|
|
class test_Command(ClassChecker):
|
2008-08-10 19:21:12 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command` class.
|
2008-08-10 19:21:12 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
|
2008-09-23 19:01:29 -05:00
|
|
|
_cls = frontend.Command
|
2008-08-10 19:21:12 -05:00
|
|
|
|
|
|
|
def get_subcls(self):
|
2008-10-08 01:58:53 -05:00
|
|
|
"""
|
|
|
|
Return a standard subclass of `ipalib.frontend.Command`.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class Rule:
|
2008-09-02 17:19:39 -05:00
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
2009-01-14 14:51:37 -06:00
|
|
|
def __call__(self, _, value):
|
2008-08-11 12:37:33 -05:00
|
|
|
if value != self.name:
|
2009-01-14 14:51:37 -06:00
|
|
|
return _('must equal %r') % self.name
|
2018-11-09 04:13:38 -06:00
|
|
|
else:
|
|
|
|
return None
|
2008-09-02 17:19:39 -05:00
|
|
|
|
2009-01-14 14:17:30 -06:00
|
|
|
default_from = parameters.DefaultFrom(
|
2008-09-02 17:19:39 -05:00
|
|
|
lambda arg: arg,
|
|
|
|
'default_from'
|
|
|
|
)
|
2009-01-14 14:17:30 -06:00
|
|
|
normalizer = lambda value: value.lower()
|
2008-08-11 12:37:33 -05:00
|
|
|
|
2008-08-10 19:21:12 -05:00
|
|
|
class example(self.cls):
|
2008-09-10 09:46:20 -05:00
|
|
|
takes_options = (
|
2009-01-14 14:51:37 -06:00
|
|
|
parameters.Str('option0', Rule('option0'),
|
2009-01-14 14:17:30 -06:00
|
|
|
normalizer=normalizer,
|
2008-09-02 17:19:39 -05:00
|
|
|
default_from=default_from,
|
|
|
|
),
|
2009-01-14 14:51:37 -06:00
|
|
|
parameters.Str('option1', Rule('option1'),
|
2009-01-14 14:17:30 -06:00
|
|
|
normalizer=normalizer,
|
2008-09-02 17:19:39 -05:00
|
|
|
default_from=default_from,
|
|
|
|
),
|
|
|
|
)
|
2008-08-10 19:21:12 -05:00
|
|
|
return example
|
|
|
|
|
2008-09-18 14:39:23 -05:00
|
|
|
def get_instance(self, args=tuple(), options=tuple()):
|
2008-09-10 09:46:20 -05:00
|
|
|
"""
|
|
|
|
Helper method used to test args and options.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2008-09-10 09:46:20 -05:00
|
|
|
class example(self.cls):
|
|
|
|
takes_args = args
|
|
|
|
takes_options = options
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
o.finalize()
|
2008-09-21 13:50:00 -05:00
|
|
|
return o
|
2008-09-10 09:46:20 -05:00
|
|
|
|
2008-09-18 18:15:34 -05:00
|
|
|
def test_class(self):
|
2008-10-08 01:58:53 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Command` class.
|
|
|
|
"""
|
2008-09-18 18:15:34 -05:00
|
|
|
assert self.cls.takes_options == tuple()
|
|
|
|
assert self.cls.takes_args == tuple()
|
|
|
|
|
2008-09-09 16:18:44 -05:00
|
|
|
def test_get_args(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.get_args` method.
|
2008-09-09 16:18:44 -05:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
assert list(self.cls(api).get_args()) == []
|
2008-09-09 16:18:44 -05:00
|
|
|
args = ('login', 'stuff')
|
2008-09-18 14:39:23 -05:00
|
|
|
o = self.get_instance(args=args)
|
2009-01-21 14:59:55 -06:00
|
|
|
assert tuple(o.get_args()) == args
|
2008-09-09 16:18:44 -05:00
|
|
|
|
2008-09-10 09:46:20 -05:00
|
|
|
def test_get_options(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.get_options` method.
|
2008-09-10 09:46:20 -05:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
options = list(self.cls(api).get_options())
|
2012-06-21 07:20:26 -05:00
|
|
|
assert len(options) == 1
|
|
|
|
assert options[0].name == 'version'
|
2008-09-10 09:46:20 -05:00
|
|
|
options = ('verbose', 'debug')
|
2008-09-18 14:39:23 -05:00
|
|
|
o = self.get_instance(options=options)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert len(tuple(o.get_options())) == 3
|
|
|
|
assert 'verbose' in tuple(o.get_options())
|
|
|
|
assert 'debug' in tuple(o.get_options())
|
2008-09-09 20:54:48 -05:00
|
|
|
|
2008-09-09 18:46:16 -05:00
|
|
|
def test_args(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the ``ipalib.frontend.Command.args`` instance attribute.
|
2008-09-09 18:46:16 -05:00
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
o.finalize()
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(o.args) is NameSpace
|
2008-09-21 13:50:00 -05:00
|
|
|
assert len(o.args) == 0
|
2008-09-09 20:54:48 -05:00
|
|
|
args = ('destination', 'source?')
|
2008-09-18 14:39:23 -05:00
|
|
|
ns = self.get_instance(args=args).args
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(ns) is NameSpace
|
2008-09-09 20:54:48 -05:00
|
|
|
assert len(ns) == len(args)
|
|
|
|
assert list(ns) == ['destination', 'source']
|
2009-01-14 14:17:30 -06:00
|
|
|
assert type(ns.destination) is parameters.Str
|
|
|
|
assert type(ns.source) is parameters.Str
|
2008-09-09 20:54:48 -05:00
|
|
|
assert ns.destination.required is True
|
|
|
|
assert ns.destination.multivalue is False
|
|
|
|
assert ns.source.required is False
|
|
|
|
assert ns.source.multivalue is False
|
|
|
|
|
2008-09-10 09:54:01 -05:00
|
|
|
# Test TypeError:
|
2015-09-17 11:46:14 -05:00
|
|
|
if six.PY2:
|
|
|
|
e = raises(TypeError, self.get_instance, args=(u'whatever',))
|
|
|
|
assert str(e) == TYPE_ERROR % (
|
|
|
|
'spec', (str, parameters.Param), u'whatever', unicode)
|
|
|
|
else:
|
|
|
|
e = raises(TypeError, self.get_instance, args=(b'whatever',))
|
|
|
|
assert str(e) == TYPE_ERROR % (
|
|
|
|
'spec', (str, parameters.Param), b'whatever', bytes)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-09-10 09:54:01 -05:00
|
|
|
# Test ValueError, required after optional:
|
2008-09-18 14:39:23 -05:00
|
|
|
e = raises(ValueError, self.get_instance, args=('arg1?', 'arg2'))
|
2013-09-26 09:44:37 -05:00
|
|
|
assert str(e) == "arg2: required argument after optional in %s arguments ['arg1?', 'arg2']" % (self.get_instance().name)
|
2008-09-10 09:54:01 -05:00
|
|
|
|
|
|
|
# Test ValueError, scalar after multivalue:
|
2008-09-18 14:39:23 -05:00
|
|
|
e = raises(ValueError, self.get_instance, args=('arg1+', 'arg2'))
|
2008-09-10 09:54:01 -05:00
|
|
|
assert str(e) == 'arg2: only final argument can be multivalue'
|
|
|
|
|
2008-09-18 18:15:34 -05:00
|
|
|
def test_max_args(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the ``ipalib.frontend.Command.max_args`` instance attribute.
|
2008-09-18 18:15:34 -05:00
|
|
|
"""
|
|
|
|
o = self.get_instance()
|
|
|
|
assert o.max_args == 0
|
|
|
|
o = self.get_instance(args=('one?',))
|
|
|
|
assert o.max_args == 1
|
|
|
|
o = self.get_instance(args=('one', 'two?'))
|
|
|
|
assert o.max_args == 2
|
|
|
|
o = self.get_instance(args=('one', 'multi+',))
|
|
|
|
assert o.max_args is None
|
|
|
|
o = self.get_instance(args=('one', 'multi*',))
|
|
|
|
assert o.max_args is None
|
|
|
|
|
2008-09-10 10:14:26 -05:00
|
|
|
def test_options(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the ``ipalib.frontend.Command.options`` instance attribute.
|
2008-09-10 10:14:26 -05:00
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
o.finalize()
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(o.options) is NameSpace
|
2012-06-21 07:20:26 -05:00
|
|
|
assert len(o.options) == 1
|
2008-09-10 10:14:26 -05:00
|
|
|
options = ('target', 'files*')
|
2008-09-18 14:39:23 -05:00
|
|
|
ns = self.get_instance(options=options).options
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(ns) is NameSpace
|
2012-06-21 07:20:26 -05:00
|
|
|
assert len(ns) == len(options) + 1
|
|
|
|
assert list(ns) == ['target', 'files', 'version']
|
2009-01-14 14:17:30 -06:00
|
|
|
assert type(ns.target) is parameters.Str
|
|
|
|
assert type(ns.files) is parameters.Str
|
2008-09-10 10:14:26 -05:00
|
|
|
assert ns.target.required is True
|
|
|
|
assert ns.target.multivalue is False
|
|
|
|
assert ns.files.required is False
|
|
|
|
assert ns.files.multivalue is True
|
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
def test_output(self):
|
|
|
|
"""
|
|
|
|
Test the ``ipalib.frontend.Command.output`` instance attribute.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = self.cls(api)
|
2009-12-09 10:09:53 -06:00
|
|
|
inst.finalize()
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(inst.output) is NameSpace
|
2009-12-09 10:09:53 -06:00
|
|
|
assert list(inst.output) == ['result']
|
|
|
|
assert type(inst.output.result) is output.Output
|
|
|
|
|
|
|
|
def test_iter_output(self):
|
|
|
|
"""
|
|
|
|
Test the ``ipalib.frontend.Command._iter_output`` instance attribute.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2009-12-09 10:09:53 -06:00
|
|
|
class Example(self.cls):
|
|
|
|
pass
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = Example(api)
|
2009-12-09 10:09:53 -06:00
|
|
|
|
|
|
|
inst.has_output = tuple()
|
|
|
|
assert list(inst._iter_output()) == []
|
|
|
|
|
|
|
|
wrong = ['hello', 'world']
|
|
|
|
inst.has_output = wrong
|
|
|
|
e = raises(TypeError, list, inst._iter_output())
|
|
|
|
assert str(e) == 'Example.has_output: need a %r; got a %r: %r' % (
|
|
|
|
tuple, list, wrong
|
|
|
|
)
|
|
|
|
|
|
|
|
wrong = ('hello', 17)
|
|
|
|
inst.has_output = wrong
|
|
|
|
e = raises(TypeError, list, inst._iter_output())
|
|
|
|
assert str(e) == 'Example.has_output[1]: need a %r; got a %r: %r' % (
|
|
|
|
(str, output.Output), int, 17
|
|
|
|
)
|
|
|
|
|
|
|
|
okay = ('foo', output.Output('bar'), 'baz')
|
|
|
|
inst.has_output = okay
|
|
|
|
items = list(inst._iter_output())
|
|
|
|
assert len(items) == 3
|
|
|
|
assert list(o.name for o in items) == ['foo', 'bar', 'baz']
|
|
|
|
for o in items:
|
|
|
|
assert type(o) is output.Output
|
|
|
|
|
2008-09-03 21:30:40 -05:00
|
|
|
def test_convert(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.convert` method.
|
2008-09-03 21:30:40 -05:00
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2008-09-03 21:30:40 -05:00
|
|
|
kw = dict(
|
2009-01-14 14:51:37 -06:00
|
|
|
option0=u'1.5',
|
|
|
|
option1=u'7',
|
2008-09-03 21:30:40 -05:00
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.subcls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
o.finalize()
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
for (key, value) in o.convert(**kw).items():
|
2009-01-14 14:51:37 -06:00
|
|
|
assert_equal(unicode(kw[key]), value)
|
2008-09-03 21:30:40 -05:00
|
|
|
|
2008-08-11 11:29:37 -05:00
|
|
|
def test_normalize(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.normalize` method.
|
2008-08-11 11:29:37 -05:00
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2008-08-11 11:29:37 -05:00
|
|
|
kw = dict(
|
2008-09-02 18:40:44 -05:00
|
|
|
option0=u'OPTION0',
|
|
|
|
option1=u'OPTION1',
|
2008-08-11 11:29:37 -05:00
|
|
|
)
|
|
|
|
norm = dict((k, v.lower()) for (k, v) in kw.items())
|
2015-06-22 05:58:43 -05:00
|
|
|
sub = self.subcls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
sub.finalize()
|
2008-08-11 11:29:37 -05:00
|
|
|
assert sub.normalize(**kw) == norm
|
|
|
|
|
2008-08-26 14:13:55 -05:00
|
|
|
def test_get_default(self):
|
2008-08-11 12:37:33 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.get_default` method.
|
2008-08-11 12:37:33 -05:00
|
|
|
"""
|
2009-01-14 14:17:30 -06:00
|
|
|
# FIXME: Add an updated unit tests for get_default()
|
2008-08-11 12:37:33 -05:00
|
|
|
|
2011-06-21 07:07:19 -05:00
|
|
|
def test_default_from_chaining(self):
|
|
|
|
"""
|
|
|
|
Test chaining of parameters through default_from.
|
|
|
|
"""
|
|
|
|
class my_cmd(self.cls):
|
|
|
|
takes_options = (
|
|
|
|
Str('option0'),
|
|
|
|
Str('option1', default_from=lambda option0: option0),
|
|
|
|
Str('option2', default_from=lambda option1: option1),
|
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, *args, **options):
|
|
|
|
return dict(result=options)
|
|
|
|
|
|
|
|
kw = dict(option0=u'some value')
|
|
|
|
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2011-06-21 07:07:19 -05:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2011-06-21 07:07:19 -05:00
|
|
|
o.finalize()
|
2016-06-21 01:17:17 -05:00
|
|
|
e = o.get_default(**kw) # pylint: disable=not-callable
|
2011-06-21 07:07:19 -05:00
|
|
|
assert type(e) is dict
|
2016-06-21 01:17:17 -05:00
|
|
|
assert 'option2' in e
|
|
|
|
assert e['option2'] == u'some value'
|
2011-06-21 07:07:19 -05:00
|
|
|
|
2008-09-03 21:02:06 -05:00
|
|
|
def test_validate(self):
|
2008-08-11 14:11:26 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.validate` method.
|
2008-08-11 14:11:26 -05:00
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:58:43 -05:00
|
|
|
env = config.Env(context='cli')
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2008-08-13 00:14:12 -05:00
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
sub = self.subcls(api)
|
2008-09-21 16:50:56 -05:00
|
|
|
sub.finalize()
|
2008-08-13 00:14:12 -05:00
|
|
|
|
2009-01-14 14:51:37 -06:00
|
|
|
# Check with valid values
|
2008-08-13 00:14:12 -05:00
|
|
|
okay = dict(
|
2008-09-02 18:40:44 -05:00
|
|
|
option0=u'option0',
|
|
|
|
option1=u'option1',
|
2008-08-13 00:14:12 -05:00
|
|
|
another_option='some value',
|
2011-01-13 13:29:16 -06:00
|
|
|
version=API_VERSION,
|
2008-08-13 00:14:12 -05:00
|
|
|
)
|
|
|
|
sub.validate(**okay)
|
|
|
|
|
2009-01-14 14:51:37 -06:00
|
|
|
# Check with an invalid value
|
2008-08-13 00:14:12 -05:00
|
|
|
fail = dict(okay)
|
2008-09-03 21:02:06 -05:00
|
|
|
fail['option0'] = u'whatever'
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.ValidationError, sub.validate, **fail)
|
2016-06-21 01:17:17 -05:00
|
|
|
assert_equal(e.name, u'option0')
|
2009-01-14 14:51:37 -06:00
|
|
|
assert_equal(e.error, u"must equal 'option0'")
|
2008-08-13 00:14:12 -05:00
|
|
|
|
|
|
|
# Check with a missing required arg
|
|
|
|
fail = dict(okay)
|
|
|
|
fail.pop('option1')
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.RequirementError, sub.validate, **fail)
|
2008-09-03 21:02:06 -05:00
|
|
|
assert e.name == 'option1'
|
2008-08-11 14:11:26 -05:00
|
|
|
|
2008-08-11 14:35:57 -05:00
|
|
|
def test_execute(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.execute` method.
|
2008-08-11 14:35:57 -05:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
o = self.cls(api)
|
2008-10-08 19:18:13 -05:00
|
|
|
e = raises(NotImplementedError, o.execute)
|
|
|
|
assert str(e) == 'Command.execute()'
|
2008-08-11 14:35:57 -05:00
|
|
|
|
2009-01-22 00:39:17 -06:00
|
|
|
def test_args_options_2_params(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Command.args_options_2_params` method.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Test that ZeroArgumentError is raised:
|
|
|
|
o = self.get_instance()
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.ZeroArgumentError, o.args_options_2_params, 1)
|
2009-01-22 00:39:17 -06:00
|
|
|
assert e.name == 'example'
|
|
|
|
|
|
|
|
# Test that MaxArgumentError is raised (count=1)
|
|
|
|
o = self.get_instance(args=('one?',))
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.MaxArgumentError, o.args_options_2_params, 1, 2)
|
2009-01-22 00:39:17 -06:00
|
|
|
assert e.name == 'example'
|
|
|
|
assert e.count == 1
|
|
|
|
assert str(e) == "command 'example' takes at most 1 argument"
|
|
|
|
|
|
|
|
# Test that MaxArgumentError is raised (count=2)
|
|
|
|
o = self.get_instance(args=('one', 'two?'))
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.MaxArgumentError, o.args_options_2_params, 1, 2, 3)
|
2009-01-22 00:39:17 -06:00
|
|
|
assert e.name == 'example'
|
|
|
|
assert e.count == 2
|
|
|
|
assert str(e) == "command 'example' takes at most 2 arguments"
|
|
|
|
|
2012-04-17 11:42:35 -05:00
|
|
|
# Test that OptionError is raised when an extra option is given:
|
|
|
|
o = self.get_instance()
|
|
|
|
e = raises(errors.OptionError, o.args_options_2_params, bad_option=True)
|
|
|
|
assert e.option == 'bad_option'
|
|
|
|
|
2009-01-22 00:39:17 -06:00
|
|
|
# Test that OverlapError is raised:
|
|
|
|
o = self.get_instance(args=('one', 'two'), options=('three', 'four'))
|
2009-04-23 07:51:59 -05:00
|
|
|
e = raises(errors.OverlapError, o.args_options_2_params,
|
2009-01-22 00:39:17 -06:00
|
|
|
1, 2, three=3, two=2, four=4, one=1)
|
2016-06-21 01:17:17 -05:00
|
|
|
assert e.names == "['one', 'two']"
|
2009-01-22 00:39:17 -06:00
|
|
|
|
|
|
|
# Test the permutations:
|
|
|
|
o = self.get_instance(args=('one', 'two*'), options=('three', 'four'))
|
|
|
|
mthd = o.args_options_2_params
|
|
|
|
assert mthd() == dict()
|
|
|
|
assert mthd(1) == dict(one=1)
|
|
|
|
assert mthd(1, 2) == dict(one=1, two=(2,))
|
|
|
|
assert mthd(1, 21, 22, 23) == dict(one=1, two=(21, 22, 23))
|
|
|
|
assert mthd(1, (21, 22, 23)) == dict(one=1, two=(21, 22, 23))
|
|
|
|
assert mthd(three=3, four=4) == dict(three=3, four=4)
|
|
|
|
assert mthd(three=3, four=4, one=1, two=2) == \
|
|
|
|
dict(one=1, two=2, three=3, four=4)
|
|
|
|
assert mthd(1, 21, 22, 23, three=3, four=4) == \
|
|
|
|
dict(one=1, two=(21, 22, 23), three=3, four=4)
|
|
|
|
assert mthd(1, (21, 22, 23), three=3, four=4) == \
|
|
|
|
dict(one=1, two=(21, 22, 23), three=3, four=4)
|
|
|
|
|
2009-02-24 09:47:05 -06:00
|
|
|
def test_args_options_2_entry(self):
|
|
|
|
"""
|
|
|
|
Test `ipalib.frontend.Command.args_options_2_entry` method.
|
|
|
|
"""
|
|
|
|
class my_cmd(self.cls):
|
|
|
|
takes_args = (
|
|
|
|
parameters.Str('one', attribute=True),
|
|
|
|
parameters.Str('two', attribute=False),
|
|
|
|
)
|
|
|
|
takes_options = (
|
|
|
|
parameters.Str('three', attribute=True, multivalue=True),
|
|
|
|
parameters.Str('four', attribute=True, multivalue=False),
|
|
|
|
)
|
|
|
|
|
|
|
|
def run(self, *args, **kw):
|
|
|
|
return self.args_options_2_entry(*args, **kw)
|
|
|
|
|
|
|
|
args = ('one', 'two')
|
|
|
|
kw = dict(three=('three1', 'three2'), four='four')
|
|
|
|
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2009-05-25 03:12:40 -05:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2009-02-24 09:47:05 -06:00
|
|
|
o.finalize()
|
|
|
|
e = o.run(*args, **kw)
|
|
|
|
assert type(e) is dict
|
|
|
|
assert 'one' in e
|
|
|
|
assert 'two' not in e
|
|
|
|
assert 'three' in e
|
|
|
|
assert 'four' in e
|
|
|
|
assert e['one'] == 'one'
|
|
|
|
assert e['three'] == ['three1', 'three2']
|
|
|
|
assert e['four'] == 'four'
|
|
|
|
|
2008-11-12 10:55:11 -06:00
|
|
|
def test_params_2_args_options(self):
|
2008-09-18 14:39:23 -05:00
|
|
|
"""
|
2008-11-12 10:55:11 -06:00
|
|
|
Test the `ipalib.frontend.Command.params_2_args_options` method.
|
2008-09-18 14:39:23 -05:00
|
|
|
"""
|
2009-05-20 16:19:09 -05:00
|
|
|
o = self.get_instance(args='one', options='two')
|
2016-06-21 01:17:17 -05:00
|
|
|
assert o.params_2_args_options() == ((), {})
|
2009-01-22 00:56:55 -06:00
|
|
|
assert o.params_2_args_options(one=1) == ((1,), {})
|
2016-06-21 01:17:17 -05:00
|
|
|
assert o.params_2_args_options(two=2) == ((), dict(two=2))
|
2009-01-22 00:56:55 -06:00
|
|
|
assert o.params_2_args_options(two=2, one=1) == ((1,), dict(two=2))
|
2008-09-18 14:39:23 -05:00
|
|
|
|
2008-09-23 21:52:19 -05:00
|
|
|
def test_run(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Command.run` method.
|
2008-09-23 21:52:19 -05:00
|
|
|
"""
|
|
|
|
class my_cmd(self.cls):
|
|
|
|
def execute(self, *args, **kw):
|
|
|
|
return ('execute', args, kw)
|
|
|
|
|
|
|
|
def forward(self, *args, **kw):
|
|
|
|
return ('forward', args, kw)
|
|
|
|
|
|
|
|
args = ('Hello,', 'world,')
|
2011-01-13 13:29:16 -06:00
|
|
|
kw = dict(how_are='you', on_this='fine day?', version=API_VERSION)
|
2008-09-23 21:52:19 -05:00
|
|
|
|
|
|
|
# Test in server context:
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=True)
|
2008-09-23 21:52:19 -05:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2015-09-18 10:59:16 -05:00
|
|
|
if six.PY2:
|
|
|
|
assert o.run.__func__ is self.cls.run.__func__
|
|
|
|
else:
|
|
|
|
assert o.run.__func__ is self.cls.run
|
2011-01-13 13:29:16 -06:00
|
|
|
out = o.run(*args, **kw)
|
|
|
|
assert ('execute', args, kw) == out
|
2008-09-23 21:52:19 -05:00
|
|
|
|
|
|
|
# Test in non-server context
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=False)
|
2008-09-23 21:52:19 -05:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2015-09-18 10:59:16 -05:00
|
|
|
if six.PY2:
|
|
|
|
assert o.run.__func__ is self.cls.run.__func__
|
|
|
|
else:
|
|
|
|
assert o.run.__func__ is self.cls.run
|
2008-09-23 21:52:19 -05:00
|
|
|
assert ('forward', args, kw) == o.run(*args, **kw)
|
|
|
|
|
2012-12-07 09:54:07 -06:00
|
|
|
def test_messages(self):
|
|
|
|
"""
|
|
|
|
Test correct handling of messages
|
|
|
|
"""
|
|
|
|
class TestMessage(messages.PublicMessage):
|
|
|
|
type = 'info'
|
|
|
|
format = 'This is a message.'
|
|
|
|
errno = 1234
|
|
|
|
|
|
|
|
class my_cmd(self.cls):
|
|
|
|
def execute(self, *args, **kw):
|
|
|
|
result = {'name': 'execute'}
|
|
|
|
messages.add_message(kw['version'], result, TestMessage())
|
|
|
|
return result
|
|
|
|
|
|
|
|
def forward(self, *args, **kw):
|
|
|
|
result = {'name': 'forward'}
|
|
|
|
messages.add_message(kw['version'], result, TestMessage())
|
|
|
|
return result
|
|
|
|
|
|
|
|
args = ('Hello,', 'world,')
|
|
|
|
kw = dict(how_are='you', on_this='fine day?', version=API_VERSION)
|
|
|
|
|
|
|
|
expected = [TestMessage().to_dict()]
|
|
|
|
|
|
|
|
# Test in server context:
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=True)
|
2012-12-07 09:54:07 -06:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2015-09-18 10:59:16 -05:00
|
|
|
if six.PY2:
|
|
|
|
assert o.run.__func__ is self.cls.run.__func__
|
|
|
|
else:
|
|
|
|
assert o.run.__func__ is self.cls.run
|
2012-12-07 09:54:07 -06:00
|
|
|
assert {'name': 'execute', 'messages': expected} == o.run(*args, **kw)
|
|
|
|
|
|
|
|
# Test in non-server context
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=False)
|
2012-12-07 09:54:07 -06:00
|
|
|
api.finalize()
|
2015-06-22 05:58:43 -05:00
|
|
|
o = my_cmd(api)
|
2015-09-18 10:59:16 -05:00
|
|
|
if six.PY2:
|
|
|
|
assert o.run.__func__ is self.cls.run.__func__
|
|
|
|
else:
|
|
|
|
assert o.run.__func__ is self.cls.run
|
2012-12-07 09:54:07 -06:00
|
|
|
assert {'name': 'forward', 'messages': expected} == o.run(*args, **kw)
|
|
|
|
|
2012-06-15 08:08:55 -05:00
|
|
|
def test_validate_output_basic(self):
|
2009-12-09 10:09:53 -06:00
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Command.validate_output` method.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2009-12-09 10:09:53 -06:00
|
|
|
class Example(self.cls):
|
|
|
|
has_output = ('foo', 'bar', 'baz')
|
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = Example(api)
|
2009-12-09 10:09:53 -06:00
|
|
|
inst.finalize()
|
|
|
|
|
|
|
|
# Test with wrong type:
|
|
|
|
wrong = ('foo', 'bar', 'baz')
|
|
|
|
e = raises(TypeError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == '%s.validate_output(): need a %r; got a %r: %r' % (
|
|
|
|
'Example', dict, tuple, wrong
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test with a missing keys:
|
|
|
|
wrong = dict(bar='hello')
|
|
|
|
e = raises(ValueError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == '%s.validate_output(): missing keys %r in %r' % (
|
|
|
|
'Example', ['baz', 'foo'], wrong
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test with extra keys:
|
|
|
|
wrong = dict(foo=1, bar=2, baz=3, fee=4, azz=5)
|
|
|
|
e = raises(ValueError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == '%s.validate_output(): unexpected keys %r in %r' % (
|
|
|
|
'Example', ['azz', 'fee'], wrong
|
|
|
|
)
|
|
|
|
|
2012-06-15 08:08:55 -05:00
|
|
|
# Test with different keys:
|
|
|
|
wrong = dict(baz=1, xyzzy=2, quux=3)
|
|
|
|
e = raises(ValueError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == '%s.validate_output(): missing keys %r in %r' % (
|
|
|
|
'Example', ['bar', 'foo'], wrong
|
|
|
|
), str(e)
|
|
|
|
|
|
|
|
def test_validate_output_per_type(self):
|
|
|
|
"""
|
|
|
|
Test `ipalib.frontend.Command.validate_output` per-type validation.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2012-06-15 08:08:55 -05:00
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
class Complex(self.cls):
|
|
|
|
has_output = (
|
|
|
|
output.Output('foo', int),
|
|
|
|
output.Output('bar', list),
|
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = Complex(api)
|
2009-12-09 10:09:53 -06:00
|
|
|
inst.finalize()
|
|
|
|
|
|
|
|
wrong = dict(foo=17.9, bar=[18])
|
|
|
|
e = raises(TypeError, inst.validate_output, wrong)
|
2016-06-21 01:17:17 -05:00
|
|
|
assert str(e) == '%s:\n output[%r]: need (%r,); got %r: %r' % (
|
2009-12-09 10:09:53 -06:00
|
|
|
'Complex.validate_output()', 'foo', int, float, 17.9
|
|
|
|
)
|
|
|
|
|
|
|
|
wrong = dict(foo=18, bar=17)
|
|
|
|
e = raises(TypeError, inst.validate_output, wrong)
|
2016-06-21 01:17:17 -05:00
|
|
|
assert str(e) == '%s:\n output[%r]: need (%r,); got %r: %r' % (
|
2009-12-09 10:09:53 -06:00
|
|
|
'Complex.validate_output()', 'bar', list, int, 17
|
|
|
|
)
|
|
|
|
|
2012-06-15 08:08:55 -05:00
|
|
|
def test_validate_output_nested(self):
|
|
|
|
"""
|
|
|
|
Test `ipalib.frontend.Command.validate_output` nested validation.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2012-06-15 08:08:55 -05:00
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
class Subclass(output.ListOfEntries):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Test nested validation:
|
|
|
|
class nested(self.cls):
|
|
|
|
has_output = (
|
|
|
|
output.Output('hello', int),
|
|
|
|
Subclass('world'),
|
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = nested(api)
|
2009-12-09 10:09:53 -06:00
|
|
|
inst.finalize()
|
|
|
|
okay = dict(foo='bar')
|
|
|
|
nope = ('aye', 'bee')
|
|
|
|
|
|
|
|
wrong = dict(hello=18, world=[okay, nope, okay])
|
|
|
|
e = raises(TypeError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == output.emsg % (
|
|
|
|
'nested', 'Subclass', 'world', 1, dict, tuple, nope
|
|
|
|
)
|
|
|
|
|
|
|
|
wrong = dict(hello=18, world=[okay, okay, okay, okay, nope])
|
|
|
|
e = raises(TypeError, inst.validate_output, wrong)
|
|
|
|
assert str(e) == output.emsg % (
|
|
|
|
'nested', 'Subclass', 'world', 4, dict, tuple, nope
|
|
|
|
)
|
|
|
|
|
2010-02-10 22:15:47 -06:00
|
|
|
def test_get_output_params(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Command.get_output_params` method.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2010-02-10 22:15:47 -06:00
|
|
|
class example(self.cls):
|
|
|
|
has_output_params = (
|
|
|
|
'one',
|
|
|
|
'two',
|
|
|
|
'three',
|
|
|
|
)
|
|
|
|
takes_args = (
|
|
|
|
'foo',
|
|
|
|
)
|
|
|
|
takes_options = (
|
2016-06-08 09:00:49 -05:00
|
|
|
Str('bar'),
|
2010-02-10 22:15:47 -06:00
|
|
|
'baz',
|
|
|
|
)
|
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
inst = example(api)
|
2010-02-10 22:15:47 -06:00
|
|
|
inst.finalize()
|
|
|
|
assert list(inst.get_output_params()) == [
|
2016-06-08 09:00:49 -05:00
|
|
|
'one', 'two', 'three'
|
2010-02-10 22:15:47 -06:00
|
|
|
]
|
2016-06-08 09:00:49 -05:00
|
|
|
assert list(inst.output_params) == ['one', 'two', 'three']
|
2010-02-10 22:15:47 -06:00
|
|
|
|
2008-09-18 14:39:23 -05:00
|
|
|
|
2008-11-14 22:29:46 -06:00
|
|
|
class test_LocalOrRemote(ClassChecker):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.LocalOrRemote` class.
|
|
|
|
"""
|
|
|
|
_cls = frontend.LocalOrRemote
|
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.LocalOrRemote.__init__` method.
|
|
|
|
"""
|
2018-09-26 04:59:50 -05:00
|
|
|
class api:
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2008-11-14 22:29:46 -06:00
|
|
|
o.finalize()
|
|
|
|
assert list(o.args) == []
|
2012-06-21 07:20:26 -05:00
|
|
|
assert list(o.options) == ['server', 'version']
|
2008-11-14 22:29:46 -06:00
|
|
|
op = o.options.server
|
2008-11-14 22:58:39 -06:00
|
|
|
assert op.required is False
|
2008-11-14 22:29:46 -06:00
|
|
|
assert op.default is False
|
|
|
|
|
|
|
|
def test_run(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.LocalOrRemote.run` method.
|
|
|
|
"""
|
|
|
|
class example(self.cls):
|
2009-05-20 16:19:09 -05:00
|
|
|
takes_args = 'key?'
|
2008-11-14 22:29:46 -06:00
|
|
|
|
|
|
|
def forward(self, *args, **options):
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=('forward', args, options))
|
2008-11-14 22:29:46 -06:00
|
|
|
|
|
|
|
def execute(self, *args, **options):
|
2009-12-09 10:09:53 -06:00
|
|
|
return dict(result=('execute', args, options))
|
2008-11-14 22:29:46 -06:00
|
|
|
|
|
|
|
# Test when in_server=False:
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=False)
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(example)
|
2008-11-14 22:29:46 -06:00
|
|
|
api.finalize()
|
|
|
|
cmd = api.Command.example
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(version=u'2.47') == dict(
|
2016-08-17 09:37:29 -05:00
|
|
|
result=('execute', (), dict(version=u'2.47'))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(u'var', version=u'2.47') == dict(
|
2016-08-17 09:37:29 -05:00
|
|
|
result=('execute', (u'var',), dict(version=u'2.47'))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(server=True, version=u'2.47') == dict(
|
2016-08-17 09:37:29 -05:00
|
|
|
result=('forward', (), dict(version=u'2.47', server=True))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(u'var', server=True, version=u'2.47') == dict(
|
|
|
|
result=('forward', (u'var',), dict(version=u'2.47', server=True))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2008-11-14 22:29:46 -06:00
|
|
|
|
|
|
|
# Test when in_server=True (should always call execute):
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api(in_server=True)
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(example)
|
2008-11-14 22:29:46 -06:00
|
|
|
api.finalize()
|
|
|
|
cmd = api.Command.example
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(version=u'2.47') == dict(
|
2016-08-17 09:37:29 -05:00
|
|
|
result=('execute', (), dict(version=u'2.47', server=False))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(u'var', version=u'2.47') == dict(
|
|
|
|
result=('execute', (u'var',), dict(version=u'2.47', server=False))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(server=True, version=u'2.47') == dict(
|
2016-08-17 09:37:29 -05:00
|
|
|
result=('execute', (), dict(version=u'2.47', server=True))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2012-06-21 07:20:26 -05:00
|
|
|
assert cmd(u'var', server=True, version=u'2.47') == dict(
|
|
|
|
result=('execute', (u'var',), dict(version=u'2.47', server=True))
|
2009-12-09 10:09:53 -06:00
|
|
|
)
|
2008-11-14 22:29:46 -06:00
|
|
|
|
|
|
|
|
2008-08-22 16:50:53 -05:00
|
|
|
class test_Object(ClassChecker):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Object` class.
|
2008-08-22 16:50:53 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
_cls = frontend.Object
|
2008-08-22 16:50:53 -05:00
|
|
|
|
|
|
|
def test_class(self):
|
2008-10-08 01:58:53 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Object` class.
|
|
|
|
"""
|
2008-09-25 21:43:11 -05:00
|
|
|
assert self.cls.backend is None
|
2008-09-24 18:19:34 -05:00
|
|
|
assert self.cls.methods is None
|
2008-09-24 18:29:15 -05:00
|
|
|
assert self.cls.params is None
|
2008-09-24 22:27:40 -05:00
|
|
|
assert self.cls.params_minus_pk is None
|
2008-09-24 18:19:34 -05:00
|
|
|
assert self.cls.takes_params == tuple()
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-22 16:50:53 -05:00
|
|
|
def test_init(self):
|
2008-08-22 17:49:56 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Object.__init__` method.
|
2008-08-22 17:49:56 -05:00
|
|
|
"""
|
|
|
|
# Setup for test:
|
2018-09-26 04:59:50 -05:00
|
|
|
class DummyAttribute:
|
2008-08-22 17:49:56 -05:00
|
|
|
def __init__(self, obj_name, attr_name, name=None):
|
|
|
|
self.obj_name = obj_name
|
|
|
|
self.attr_name = attr_name
|
|
|
|
if name is None:
|
|
|
|
self.name = '%s_%s' % (obj_name, attr_name)
|
|
|
|
else:
|
|
|
|
self.name = name
|
2016-08-18 03:04:59 -05:00
|
|
|
self.bases = (DummyAttribute,)
|
|
|
|
self.version = '1'
|
|
|
|
self.full_name = '{}/{}'.format(self.name, self.version)
|
2008-09-24 19:00:58 -05:00
|
|
|
self.param = frontend.create_param(attr_name)
|
|
|
|
|
2008-08-22 17:49:56 -05:00
|
|
|
def __clone__(self, attr_name):
|
|
|
|
return self.__class__(
|
|
|
|
self.obj_name,
|
|
|
|
self.attr_name,
|
|
|
|
getattr(self, attr_name)
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_attributes(cnt, format):
|
|
|
|
for name in ['other', 'user', 'another']:
|
2015-08-12 08:23:56 -05:00
|
|
|
for i in range(cnt):
|
2008-08-22 17:49:56 -05:00
|
|
|
yield DummyAttribute(name, format % i)
|
|
|
|
|
|
|
|
cnt = 10
|
2013-01-16 09:26:37 -06:00
|
|
|
methods_format = 'method_%d'
|
2008-09-25 21:43:11 -05:00
|
|
|
|
2018-09-26 04:59:50 -05:00
|
|
|
class FakeAPI:
|
2016-08-18 03:04:59 -05:00
|
|
|
def __init__(self):
|
|
|
|
self._API__plugins = get_attributes(cnt, methods_format)
|
|
|
|
self._API__default_map = {}
|
|
|
|
self.Method = plugable.APINameSpace(self, DummyAttribute)
|
2015-06-22 05:58:43 -05:00
|
|
|
def __contains__(self, key):
|
|
|
|
return hasattr(self, key)
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return getattr(self, key)
|
2015-06-22 05:59:35 -05:00
|
|
|
def is_production_mode(self):
|
|
|
|
return False
|
2016-08-18 03:04:59 -05:00
|
|
|
def _get(self, plugin):
|
|
|
|
return plugin
|
2015-06-22 05:58:43 -05:00
|
|
|
api = FakeAPI()
|
2008-08-22 17:49:56 -05:00
|
|
|
assert len(api.Method) == cnt * 3
|
|
|
|
|
|
|
|
class user(self.cls):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Actually perform test:
|
2015-06-22 05:58:43 -05:00
|
|
|
o = user(api)
|
2008-08-22 17:49:56 -05:00
|
|
|
assert read_only(o, 'api') is api
|
2013-01-16 09:26:37 -06:00
|
|
|
|
|
|
|
namespace = o.methods
|
2016-06-08 07:38:23 -05:00
|
|
|
assert isinstance(namespace, NameSpace)
|
2013-01-16 09:26:37 -06:00
|
|
|
assert len(namespace) == cnt
|
|
|
|
f = methods_format
|
2015-08-12 08:23:56 -05:00
|
|
|
for i in range(cnt):
|
2013-01-16 09:26:37 -06:00
|
|
|
attr_name = f % i
|
|
|
|
attr = namespace[attr_name]
|
|
|
|
assert isinstance(attr, DummyAttribute)
|
|
|
|
assert attr is getattr(namespace, attr_name)
|
|
|
|
assert attr.obj_name == 'user'
|
|
|
|
assert attr.attr_name == attr_name
|
|
|
|
assert attr.name == '%s_%s' % ('user', attr_name)
|
2008-08-22 17:49:56 -05:00
|
|
|
|
2008-09-24 18:29:15 -05:00
|
|
|
# Test params instance attribute
|
2015-06-22 05:58:43 -05:00
|
|
|
o = self.cls(api)
|
2008-09-24 18:29:15 -05:00
|
|
|
ns = o.params
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(ns) is NameSpace
|
2008-09-21 19:37:01 -05:00
|
|
|
assert len(ns) == 0
|
|
|
|
class example(self.cls):
|
|
|
|
takes_params = ('banana', 'apple')
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example(api)
|
2008-09-24 18:29:15 -05:00
|
|
|
ns = o.params
|
2016-06-08 07:38:23 -05:00
|
|
|
assert type(ns) is NameSpace
|
2008-09-21 19:37:01 -05:00
|
|
|
assert len(ns) == 2, repr(ns)
|
|
|
|
assert list(ns) == ['banana', 'apple']
|
|
|
|
for p in ns():
|
2009-01-14 14:17:30 -06:00
|
|
|
assert type(p) is parameters.Str
|
2008-09-21 19:37:01 -05:00
|
|
|
assert p.required is True
|
|
|
|
assert p.multivalue is False
|
|
|
|
|
2008-09-24 20:44:53 -05:00
|
|
|
def test_primary_key(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Object.primary_key` attribute.
|
2008-09-24 20:44:53 -05:00
|
|
|
"""
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2008-09-24 20:44:53 -05:00
|
|
|
api.finalize()
|
|
|
|
|
|
|
|
# Test with no primary keys:
|
|
|
|
class example1(self.cls):
|
|
|
|
takes_params = (
|
|
|
|
'one',
|
|
|
|
'two',
|
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example1(api)
|
2008-09-24 20:44:53 -05:00
|
|
|
assert o.primary_key is None
|
|
|
|
|
|
|
|
# Test with 1 primary key:
|
|
|
|
class example2(self.cls):
|
|
|
|
takes_params = (
|
|
|
|
'one',
|
|
|
|
'two',
|
2009-01-14 14:17:30 -06:00
|
|
|
parameters.Str('three', primary_key=True),
|
2008-09-24 20:44:53 -05:00
|
|
|
'four',
|
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example2(api)
|
2008-09-24 20:44:53 -05:00
|
|
|
pk = o.primary_key
|
2009-01-14 14:17:30 -06:00
|
|
|
assert type(pk) is parameters.Str
|
2008-09-24 20:44:53 -05:00
|
|
|
assert pk.name == 'three'
|
|
|
|
assert pk.primary_key is True
|
|
|
|
assert o.params[2] is o.primary_key
|
2016-06-08 07:38:23 -05:00
|
|
|
assert isinstance(o.params_minus_pk, NameSpace)
|
2008-09-24 22:27:40 -05:00
|
|
|
assert list(o.params_minus_pk) == ['one', 'two', 'four']
|
2008-09-24 20:44:53 -05:00
|
|
|
|
|
|
|
# Test with multiple primary_key:
|
|
|
|
class example3(self.cls):
|
|
|
|
takes_params = (
|
2009-01-14 14:17:30 -06:00
|
|
|
parameters.Str('one', primary_key=True),
|
|
|
|
parameters.Str('two', primary_key=True),
|
2008-09-24 20:44:53 -05:00
|
|
|
'three',
|
2009-01-14 14:17:30 -06:00
|
|
|
parameters.Str('four', primary_key=True),
|
2008-09-24 20:44:53 -05:00
|
|
|
)
|
2015-06-22 05:58:43 -05:00
|
|
|
o = example3(api)
|
2011-11-03 05:42:17 -05:00
|
|
|
e = raises(ValueError, o.finalize)
|
2008-09-24 20:44:53 -05:00
|
|
|
assert str(e) == \
|
|
|
|
'example3 (Object) has multiple primary keys: one, two, four'
|
|
|
|
|
2008-09-25 21:43:11 -05:00
|
|
|
def test_backend(self):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Object.backend` attribute.
|
2008-09-25 21:43:11 -05:00
|
|
|
"""
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2008-09-25 21:43:11 -05:00
|
|
|
class ldap(backend.Backend):
|
|
|
|
whatever = 'It worked!'
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(ldap)
|
2008-09-25 21:43:11 -05:00
|
|
|
class user(frontend.Object):
|
|
|
|
backend_name = 'ldap'
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(user)
|
2008-09-25 21:43:11 -05:00
|
|
|
api.finalize()
|
|
|
|
b = api.Object.user.backend
|
|
|
|
assert isinstance(b, ldap)
|
|
|
|
assert b.whatever == 'It worked!'
|
|
|
|
|
2008-10-14 00:26:24 -05:00
|
|
|
def test_get_dn(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Object.get_dn` method.
|
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
|
|
|
o = self.cls(api)
|
2008-10-14 00:26:24 -05:00
|
|
|
e = raises(NotImplementedError, o.get_dn, 'primary key')
|
|
|
|
assert str(e) == 'Object.get_dn()'
|
|
|
|
class user(self.cls):
|
|
|
|
pass
|
2015-06-22 05:58:43 -05:00
|
|
|
o = user(api)
|
2008-10-14 00:26:24 -05:00
|
|
|
e = raises(NotImplementedError, o.get_dn, 'primary key')
|
|
|
|
assert str(e) == 'user.get_dn()'
|
|
|
|
|
2009-01-22 10:58:35 -06:00
|
|
|
def test_params_minus(self):
|
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Object.params_minus` method.
|
|
|
|
"""
|
|
|
|
class example(self.cls):
|
|
|
|
takes_params = ('one', 'two', 'three', 'four')
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2015-06-22 05:58:43 -05:00
|
|
|
api.finalize()
|
|
|
|
o = example(api)
|
2009-01-22 10:58:35 -06:00
|
|
|
p = o.params
|
|
|
|
assert tuple(o.params_minus()) == tuple(p())
|
|
|
|
assert tuple(o.params_minus([])) == tuple(p())
|
|
|
|
assert tuple(o.params_minus('two', 'three')) == (p.one, p.four)
|
|
|
|
assert tuple(o.params_minus(['two', 'three'])) == (p.one, p.four)
|
|
|
|
assert tuple(o.params_minus(p.two, p.three)) == (p.one, p.four)
|
|
|
|
assert tuple(o.params_minus([p.two, p.three])) == (p.one, p.four)
|
|
|
|
ns = NameSpace([p.two, p.three])
|
|
|
|
assert tuple(o.params_minus(ns)) == (p.one, p.four)
|
|
|
|
|
2008-08-06 22:38:49 -05:00
|
|
|
|
2008-08-22 16:27:25 -05:00
|
|
|
class test_Attribute(ClassChecker):
|
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Attribute` class.
|
2008-08-22 16:27:25 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
_cls = frontend.Attribute
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-22 16:27:25 -05:00
|
|
|
def test_class(self):
|
2008-10-08 01:58:53 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Attribute` class.
|
|
|
|
"""
|
2008-08-22 16:27:25 -05:00
|
|
|
assert self.cls.__bases__ == (plugable.Plugin,)
|
|
|
|
assert type(self.cls.obj) is property
|
|
|
|
assert type(self.cls.obj_name) is property
|
|
|
|
assert type(self.cls.attr_name) is property
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-08-22 16:27:25 -05:00
|
|
|
def test_init(self):
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Attribute.__init__` method.
|
2008-09-02 19:15:20 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
user_obj = 'The user frontend.Object instance'
|
2018-09-26 04:59:50 -05:00
|
|
|
|
|
|
|
class api:
|
2016-08-17 09:37:29 -05:00
|
|
|
Object = {("user", "1"): user_obj}
|
2015-06-22 05:59:35 -05:00
|
|
|
@staticmethod
|
|
|
|
def is_production_mode():
|
|
|
|
return False
|
2018-09-26 04:59:50 -05:00
|
|
|
|
2008-08-22 16:27:25 -05:00
|
|
|
class user_add(self.cls):
|
|
|
|
pass
|
2018-09-26 04:59:50 -05:00
|
|
|
|
2015-06-22 05:58:43 -05:00
|
|
|
o = user_add(api)
|
2008-08-22 16:27:25 -05:00
|
|
|
assert read_only(o, 'api') is api
|
|
|
|
assert read_only(o, 'obj') is user_obj
|
2015-06-22 05:58:43 -05:00
|
|
|
assert read_only(o, 'obj_name') == 'user'
|
|
|
|
assert read_only(o, 'attr_name') == 'add'
|
2008-08-11 17:12:23 -05:00
|
|
|
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-09-02 19:01:45 -05:00
|
|
|
class test_Method(ClassChecker):
|
2008-08-11 16:14:07 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Method` class.
|
2008-08-11 16:14:07 -05:00
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
_cls = frontend.Method
|
2008-08-11 16:14:07 -05:00
|
|
|
|
2009-12-09 10:09:53 -06:00
|
|
|
def get_api(self, args=tuple(), options=tuple()):
|
|
|
|
"""
|
|
|
|
Return a finalized `ipalib.plugable.API` instance.
|
|
|
|
"""
|
2016-10-06 13:28:59 -05:00
|
|
|
api, _home = create_test_api()
|
2009-12-09 10:09:53 -06:00
|
|
|
class user(frontend.Object):
|
|
|
|
takes_params = (
|
|
|
|
'givenname',
|
|
|
|
'sn',
|
|
|
|
frontend.Param('uid', primary_key=True),
|
|
|
|
'initials',
|
|
|
|
)
|
|
|
|
class user_verb(self.cls):
|
|
|
|
takes_args = args
|
|
|
|
takes_options = options
|
2015-06-22 05:16:34 -05:00
|
|
|
api.add_plugin(user)
|
|
|
|
api.add_plugin(user_verb)
|
2009-12-09 10:09:53 -06:00
|
|
|
api.finalize()
|
|
|
|
return api
|
|
|
|
|
2008-08-11 16:14:07 -05:00
|
|
|
def test_class(self):
|
2008-10-08 01:58:53 -05:00
|
|
|
"""
|
|
|
|
Test the `ipalib.frontend.Method` class.
|
|
|
|
"""
|
2008-09-23 19:01:29 -05:00
|
|
|
assert self.cls.__bases__ == (frontend.Attribute, frontend.Command)
|
2008-08-05 02:39:50 -05:00
|
|
|
|
2008-09-24 20:52:34 -05:00
|
|
|
def test_init(self):
|
2008-08-11 17:12:23 -05:00
|
|
|
"""
|
2008-10-08 01:58:53 -05:00
|
|
|
Test the `ipalib.frontend.Method.__init__` method.
|
2008-08-11 17:12:23 -05:00
|
|
|
"""
|
2015-06-22 05:58:43 -05:00
|
|
|
api = 'the api instance'
|
2008-09-24 20:52:34 -05:00
|
|
|
class user_add(self.cls):
|
|
|
|
pass
|
2015-06-22 05:58:43 -05:00
|
|
|
o = user_add(api)
|
2008-09-24 20:52:34 -05:00
|
|
|
assert o.name == 'user_add'
|
|
|
|
assert o.obj_name == 'user'
|
|
|
|
assert o.attr_name == 'add'
|