Imported Upstream version 4.7.2
This commit is contained in:
@@ -21,8 +21,13 @@
|
||||
Test the `ipalib.frontend` module.
|
||||
"""
|
||||
|
||||
from ipatests.util import raises, getitem, no_set, no_del, read_only
|
||||
from ipatests.util import check_TypeError, ClassChecker, create_test_api
|
||||
# FIXME: Pylint errors
|
||||
# pylint: disable=no-member
|
||||
import pytest
|
||||
import six
|
||||
|
||||
from ipatests.util import raises, read_only
|
||||
from ipatests.util import ClassChecker, create_test_api
|
||||
from ipatests.util import assert_equal
|
||||
from ipalib.constants import TYPE_ERROR
|
||||
from ipalib.base import NameSpace
|
||||
@@ -31,6 +36,13 @@ from ipalib import output, messages
|
||||
from ipalib.parameters import Str
|
||||
from ipapython.version import API_VERSION
|
||||
|
||||
if six.PY3:
|
||||
unicode = str
|
||||
|
||||
|
||||
pytestmark = pytest.mark.tier0
|
||||
|
||||
|
||||
def test_RULE_FLAG():
|
||||
assert frontend.RULE_FLAG == 'validation_rule'
|
||||
|
||||
@@ -86,31 +98,32 @@ class test_HasParam(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.HasParam._get_param_iterable` method.
|
||||
"""
|
||||
api = 'the api instance'
|
||||
class WithTuple(self.cls):
|
||||
takes_stuff = ('one', 'two')
|
||||
o = WithTuple()
|
||||
o = WithTuple(api)
|
||||
assert o._get_param_iterable('stuff') is WithTuple.takes_stuff
|
||||
|
||||
junk = ('three', 'four')
|
||||
class WithCallable(self.cls):
|
||||
def takes_stuff(self):
|
||||
return junk
|
||||
o = WithCallable()
|
||||
o = WithCallable(api)
|
||||
assert o._get_param_iterable('stuff') is junk
|
||||
|
||||
class WithParam(self.cls):
|
||||
takes_stuff = parameters.Str('five')
|
||||
o = WithParam()
|
||||
o = WithParam(api)
|
||||
assert o._get_param_iterable('stuff') == (WithParam.takes_stuff,)
|
||||
|
||||
class WithStr(self.cls):
|
||||
takes_stuff = 'six'
|
||||
o = WithStr()
|
||||
o = WithStr(api)
|
||||
assert o._get_param_iterable('stuff') == ('six',)
|
||||
|
||||
class Wrong(self.cls):
|
||||
takes_stuff = ['seven', 'eight']
|
||||
o = Wrong()
|
||||
o = Wrong(api)
|
||||
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
|
||||
@@ -120,6 +133,7 @@ class test_HasParam(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.HasParam._filter_param_by_context` method.
|
||||
"""
|
||||
api = 'the api instance'
|
||||
class Example(self.cls):
|
||||
def get_stuff(self):
|
||||
return (
|
||||
@@ -129,7 +143,7 @@ class test_HasParam(ClassChecker):
|
||||
parameters.Str('four', exclude='server'),
|
||||
parameters.Str('five', exclude=['whatever', 'cli']),
|
||||
)
|
||||
o = Example()
|
||||
o = Example(api)
|
||||
|
||||
# Test when env is None:
|
||||
params = list(o._filter_param_by_context('stuff'))
|
||||
@@ -158,7 +172,7 @@ class test_HasParam(ClassChecker):
|
||||
# Test with no get_stuff:
|
||||
class Missing(self.cls):
|
||||
pass
|
||||
o = Missing()
|
||||
o = Missing(api)
|
||||
gen = o._filter_param_by_context('stuff')
|
||||
e = raises(NotImplementedError, list, gen)
|
||||
assert str(e) == 'Missing.get_stuff()'
|
||||
@@ -166,7 +180,7 @@ class test_HasParam(ClassChecker):
|
||||
# Test when get_stuff is not callable:
|
||||
class NotCallable(self.cls):
|
||||
get_stuff = ('one', 'two')
|
||||
o = NotCallable()
|
||||
o = NotCallable(api)
|
||||
gen = o._filter_param_by_context('stuff')
|
||||
e = raises(TypeError, list, gen)
|
||||
assert str(e) == '%s.%s must be a callable; got %r' % (
|
||||
@@ -192,6 +206,8 @@ class test_Command(ClassChecker):
|
||||
def __call__(self, _, value):
|
||||
if value != self.name:
|
||||
return _('must equal %r') % self.name
|
||||
else:
|
||||
return None
|
||||
|
||||
default_from = parameters.DefaultFrom(
|
||||
lambda arg: arg,
|
||||
@@ -216,10 +232,14 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Helper method used to test args and options.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
class example(self.cls):
|
||||
takes_args = args
|
||||
takes_options = options
|
||||
o = example()
|
||||
o = example(api)
|
||||
o.finalize()
|
||||
return o
|
||||
|
||||
@@ -234,7 +254,8 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.get_args` method.
|
||||
"""
|
||||
assert list(self.cls().get_args()) == []
|
||||
api = 'the api instance'
|
||||
assert list(self.cls(api).get_args()) == []
|
||||
args = ('login', 'stuff')
|
||||
o = self.get_instance(args=args)
|
||||
assert tuple(o.get_args()) == args
|
||||
@@ -243,7 +264,8 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.get_options` method.
|
||||
"""
|
||||
options = list(self.cls().get_options())
|
||||
api = 'the api instance'
|
||||
options = list(self.cls(api).get_options())
|
||||
assert len(options) == 1
|
||||
assert options[0].name == 'version'
|
||||
options = ('verbose', 'debug')
|
||||
@@ -256,14 +278,17 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the ``ipalib.frontend.Command.args`` instance attribute.
|
||||
"""
|
||||
assert self.cls().args is None
|
||||
o = self.cls()
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
o = self.cls(api)
|
||||
o.finalize()
|
||||
assert type(o.args) is plugable.NameSpace
|
||||
assert type(o.args) is NameSpace
|
||||
assert len(o.args) == 0
|
||||
args = ('destination', 'source?')
|
||||
ns = self.get_instance(args=args).args
|
||||
assert type(ns) is plugable.NameSpace
|
||||
assert type(ns) is NameSpace
|
||||
assert len(ns) == len(args)
|
||||
assert list(ns) == ['destination', 'source']
|
||||
assert type(ns.destination) is parameters.Str
|
||||
@@ -274,9 +299,14 @@ class test_Command(ClassChecker):
|
||||
assert ns.source.multivalue is False
|
||||
|
||||
# Test TypeError:
|
||||
e = raises(TypeError, self.get_instance, args=(u'whatever',))
|
||||
assert str(e) == TYPE_ERROR % (
|
||||
'spec', (str, parameters.Param), u'whatever', unicode)
|
||||
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)
|
||||
|
||||
# Test ValueError, required after optional:
|
||||
e = raises(ValueError, self.get_instance, args=('arg1?', 'arg2'))
|
||||
@@ -305,14 +335,17 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the ``ipalib.frontend.Command.options`` instance attribute.
|
||||
"""
|
||||
assert self.cls().options is None
|
||||
o = self.cls()
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
o = self.cls(api)
|
||||
o.finalize()
|
||||
assert type(o.options) is plugable.NameSpace
|
||||
assert type(o.options) is NameSpace
|
||||
assert len(o.options) == 1
|
||||
options = ('target', 'files*')
|
||||
ns = self.get_instance(options=options).options
|
||||
assert type(ns) is plugable.NameSpace
|
||||
assert type(ns) is NameSpace
|
||||
assert len(ns) == len(options) + 1
|
||||
assert list(ns) == ['target', 'files', 'version']
|
||||
assert type(ns.target) is parameters.Str
|
||||
@@ -326,10 +359,13 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the ``ipalib.frontend.Command.output`` instance attribute.
|
||||
"""
|
||||
inst = self.cls()
|
||||
assert inst.output is None
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
inst = self.cls(api)
|
||||
inst.finalize()
|
||||
assert type(inst.output) is plugable.NameSpace
|
||||
assert type(inst.output) is NameSpace
|
||||
assert list(inst.output) == ['result']
|
||||
assert type(inst.output.result) is output.Output
|
||||
|
||||
@@ -337,9 +373,10 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the ``ipalib.frontend.Command._iter_output`` instance attribute.
|
||||
"""
|
||||
api = 'the api instance'
|
||||
class Example(self.cls):
|
||||
pass
|
||||
inst = Example()
|
||||
inst = Example(api)
|
||||
|
||||
inst.has_output = tuple()
|
||||
assert list(inst._iter_output()) == []
|
||||
@@ -366,54 +403,37 @@ class test_Command(ClassChecker):
|
||||
for o in items:
|
||||
assert type(o) is output.Output
|
||||
|
||||
def test_soft_validate(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.soft_validate` method.
|
||||
"""
|
||||
class user_add(frontend.Command):
|
||||
takes_args = parameters.Str('uid',
|
||||
normalizer=lambda value: value.lower(),
|
||||
default_from=lambda givenname, sn: givenname[0] + sn,
|
||||
)
|
||||
|
||||
takes_options = ('givenname', 'sn')
|
||||
|
||||
cmd = user_add()
|
||||
cmd.env = config.Env(context='cli')
|
||||
cmd.finalize()
|
||||
assert list(cmd.params) == ['givenname', 'sn', 'uid', 'version']
|
||||
ret = cmd.soft_validate({})
|
||||
assert sorted(ret['values']) == ['version']
|
||||
assert sorted(ret['errors']) == ['givenname', 'sn', 'uid']
|
||||
assert cmd.soft_validate(dict(givenname=u'First', sn=u'Last')) == dict(
|
||||
values=dict(givenname=u'First', sn=u'Last', uid=u'flast',
|
||||
version=None),
|
||||
errors=dict(),
|
||||
)
|
||||
|
||||
def test_convert(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.convert` method.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
kw = dict(
|
||||
option0=u'1.5',
|
||||
option1=u'7',
|
||||
)
|
||||
o = self.subcls()
|
||||
o = self.subcls(api)
|
||||
o.finalize()
|
||||
for (key, value) in o.convert(**kw).iteritems():
|
||||
for (key, value) in o.convert(**kw).items():
|
||||
assert_equal(unicode(kw[key]), value)
|
||||
|
||||
def test_normalize(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.normalize` method.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
kw = dict(
|
||||
option0=u'OPTION0',
|
||||
option1=u'OPTION1',
|
||||
)
|
||||
norm = dict((k, v.lower()) for (k, v) in kw.items())
|
||||
sub = self.subcls()
|
||||
sub = self.subcls(api)
|
||||
sub.finalize()
|
||||
assert sub.normalize(**kw) == norm
|
||||
|
||||
@@ -439,24 +459,26 @@ class test_Command(ClassChecker):
|
||||
|
||||
kw = dict(option0=u'some value')
|
||||
|
||||
(api, home) = create_test_api()
|
||||
api, _home = create_test_api()
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
o = my_cmd(api)
|
||||
o.finalize()
|
||||
e = o(**kw)
|
||||
e = o.get_default(**kw) # pylint: disable=not-callable
|
||||
assert type(e) is dict
|
||||
assert 'result' in e
|
||||
assert 'option2' in e['result']
|
||||
assert e['result']['option2'] == u'some value'
|
||||
assert 'option2' in e
|
||||
assert e['option2'] == u'some value'
|
||||
|
||||
def test_validate(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.validate` method.
|
||||
"""
|
||||
class api(object):
|
||||
env = config.Env(context='cli')
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
|
||||
sub = self.subcls()
|
||||
sub.env = config.Env(context='cli')
|
||||
sub = self.subcls(api)
|
||||
sub.finalize()
|
||||
|
||||
# Check with valid values
|
||||
@@ -472,11 +494,8 @@ class test_Command(ClassChecker):
|
||||
fail = dict(okay)
|
||||
fail['option0'] = u'whatever'
|
||||
e = raises(errors.ValidationError, sub.validate, **fail)
|
||||
assert_equal(e.name, 'option0')
|
||||
assert_equal(e.value, u'whatever')
|
||||
assert_equal(e.name, u'option0')
|
||||
assert_equal(e.error, u"must equal 'option0'")
|
||||
assert e.rule.__class__.__name__ == 'Rule'
|
||||
assert e.index is None
|
||||
|
||||
# Check with a missing required arg
|
||||
fail = dict(okay)
|
||||
@@ -488,7 +507,8 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.execute` method.
|
||||
"""
|
||||
o = self.cls()
|
||||
api = 'the api instance'
|
||||
o = self.cls(api)
|
||||
e = raises(NotImplementedError, o.execute)
|
||||
assert str(e) == 'Command.execute()'
|
||||
|
||||
@@ -525,7 +545,7 @@ class test_Command(ClassChecker):
|
||||
o = self.get_instance(args=('one', 'two'), options=('three', 'four'))
|
||||
e = raises(errors.OverlapError, o.args_options_2_params,
|
||||
1, 2, three=3, two=2, four=4, one=1)
|
||||
assert e.names == ['one', 'two']
|
||||
assert e.names == "['one', 'two']"
|
||||
|
||||
# Test the permutations:
|
||||
o = self.get_instance(args=('one', 'two*'), options=('three', 'four'))
|
||||
@@ -563,10 +583,9 @@ class test_Command(ClassChecker):
|
||||
args = ('one', 'two')
|
||||
kw = dict(three=('three1', 'three2'), four='four')
|
||||
|
||||
(api, home) = create_test_api()
|
||||
api, _home = create_test_api()
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
o = my_cmd(api)
|
||||
o.finalize()
|
||||
e = o.run(*args, **kw)
|
||||
assert type(e) is dict
|
||||
@@ -583,9 +602,9 @@ class test_Command(ClassChecker):
|
||||
Test the `ipalib.frontend.Command.params_2_args_options` method.
|
||||
"""
|
||||
o = self.get_instance(args='one', options='two')
|
||||
assert o.params_2_args_options() == ((None,), {})
|
||||
assert o.params_2_args_options() == ((), {})
|
||||
assert o.params_2_args_options(one=1) == ((1,), {})
|
||||
assert o.params_2_args_options(two=2) == ((None,), dict(two=2))
|
||||
assert o.params_2_args_options(two=2) == ((), dict(two=2))
|
||||
assert o.params_2_args_options(two=2, one=1) == ((1,), dict(two=2))
|
||||
|
||||
def test_run(self):
|
||||
@@ -603,20 +622,24 @@ class test_Command(ClassChecker):
|
||||
kw = dict(how_are='you', on_this='fine day?', version=API_VERSION)
|
||||
|
||||
# Test in server context:
|
||||
(api, home) = create_test_api(in_server=True)
|
||||
api, _home = create_test_api(in_server=True)
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
assert o.run.im_func is self.cls.run.im_func
|
||||
o = my_cmd(api)
|
||||
if six.PY2:
|
||||
assert o.run.__func__ is self.cls.run.__func__
|
||||
else:
|
||||
assert o.run.__func__ is self.cls.run
|
||||
out = o.run(*args, **kw)
|
||||
assert ('execute', args, kw) == out
|
||||
|
||||
# Test in non-server context
|
||||
(api, home) = create_test_api(in_server=False)
|
||||
api, _home = create_test_api(in_server=False)
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
assert o.run.im_func is self.cls.run.im_func
|
||||
o = my_cmd(api)
|
||||
if six.PY2:
|
||||
assert o.run.__func__ is self.cls.run.__func__
|
||||
else:
|
||||
assert o.run.__func__ is self.cls.run
|
||||
assert ('forward', args, kw) == o.run(*args, **kw)
|
||||
|
||||
def test_messages(self):
|
||||
@@ -645,29 +668,37 @@ class test_Command(ClassChecker):
|
||||
expected = [TestMessage().to_dict()]
|
||||
|
||||
# Test in server context:
|
||||
(api, home) = create_test_api(in_server=True)
|
||||
api, _home = create_test_api(in_server=True)
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
assert o.run.im_func is self.cls.run.im_func
|
||||
o = my_cmd(api)
|
||||
if six.PY2:
|
||||
assert o.run.__func__ is self.cls.run.__func__
|
||||
else:
|
||||
assert o.run.__func__ is self.cls.run
|
||||
assert {'name': 'execute', 'messages': expected} == o.run(*args, **kw)
|
||||
|
||||
# Test in non-server context
|
||||
(api, home) = create_test_api(in_server=False)
|
||||
api, _home = create_test_api(in_server=False)
|
||||
api.finalize()
|
||||
o = my_cmd()
|
||||
o.set_api(api)
|
||||
assert o.run.im_func is self.cls.run.im_func
|
||||
o = my_cmd(api)
|
||||
if six.PY2:
|
||||
assert o.run.__func__ is self.cls.run.__func__
|
||||
else:
|
||||
assert o.run.__func__ is self.cls.run
|
||||
assert {'name': 'forward', 'messages': expected} == o.run(*args, **kw)
|
||||
|
||||
def test_validate_output_basic(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.validate_output` method.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
class Example(self.cls):
|
||||
has_output = ('foo', 'bar', 'baz')
|
||||
|
||||
inst = Example()
|
||||
inst = Example(api)
|
||||
inst.finalize()
|
||||
|
||||
# Test with wrong type:
|
||||
@@ -702,24 +733,28 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test `ipalib.frontend.Command.validate_output` per-type validation.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
|
||||
class Complex(self.cls):
|
||||
has_output = (
|
||||
output.Output('foo', int),
|
||||
output.Output('bar', list),
|
||||
)
|
||||
inst = Complex()
|
||||
inst = Complex(api)
|
||||
inst.finalize()
|
||||
|
||||
wrong = dict(foo=17.9, bar=[18])
|
||||
e = raises(TypeError, inst.validate_output, wrong)
|
||||
assert str(e) == '%s:\n output[%r]: need %r; got %r: %r' % (
|
||||
assert str(e) == '%s:\n output[%r]: need (%r,); got %r: %r' % (
|
||||
'Complex.validate_output()', 'foo', int, float, 17.9
|
||||
)
|
||||
|
||||
wrong = dict(foo=18, bar=17)
|
||||
e = raises(TypeError, inst.validate_output, wrong)
|
||||
assert str(e) == '%s:\n output[%r]: need %r; got %r: %r' % (
|
||||
assert str(e) == '%s:\n output[%r]: need (%r,); got %r: %r' % (
|
||||
'Complex.validate_output()', 'bar', list, int, 17
|
||||
)
|
||||
|
||||
@@ -727,6 +762,10 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test `ipalib.frontend.Command.validate_output` nested validation.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
|
||||
class Subclass(output.ListOfEntries):
|
||||
pass
|
||||
@@ -737,7 +776,7 @@ class test_Command(ClassChecker):
|
||||
output.Output('hello', int),
|
||||
Subclass('world'),
|
||||
)
|
||||
inst = nested()
|
||||
inst = nested(api)
|
||||
inst.finalize()
|
||||
okay = dict(foo='bar')
|
||||
nope = ('aye', 'bee')
|
||||
@@ -758,6 +797,10 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Command.get_output_params` method.
|
||||
"""
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
class example(self.cls):
|
||||
has_output_params = (
|
||||
'one',
|
||||
@@ -768,17 +811,16 @@ class test_Command(ClassChecker):
|
||||
'foo',
|
||||
)
|
||||
takes_options = (
|
||||
Str('bar', flags='no_output'),
|
||||
Str('bar'),
|
||||
'baz',
|
||||
)
|
||||
|
||||
inst = example()
|
||||
assert list(inst.get_output_params()) == ['one', 'two', 'three']
|
||||
inst = example(api)
|
||||
inst.finalize()
|
||||
assert list(inst.get_output_params()) == [
|
||||
'one', 'two', 'three', inst.params.foo, inst.params.baz
|
||||
'one', 'two', 'three'
|
||||
]
|
||||
assert list(inst.output_params) == ['one', 'two', 'three', 'foo', 'baz']
|
||||
assert list(inst.output_params) == ['one', 'two', 'three']
|
||||
|
||||
|
||||
class test_LocalOrRemote(ClassChecker):
|
||||
@@ -791,7 +833,11 @@ class test_LocalOrRemote(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.LocalOrRemote.__init__` method.
|
||||
"""
|
||||
o = self.cls()
|
||||
class api(object):
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
o = self.cls(api)
|
||||
o.finalize()
|
||||
assert list(o.args) == []
|
||||
assert list(o.options) == ['server', 'version']
|
||||
@@ -813,36 +859,36 @@ class test_LocalOrRemote(ClassChecker):
|
||||
return dict(result=('execute', args, options))
|
||||
|
||||
# Test when in_server=False:
|
||||
(api, home) = create_test_api(in_server=False)
|
||||
api.register(example)
|
||||
api, _home = create_test_api(in_server=False)
|
||||
api.add_plugin(example)
|
||||
api.finalize()
|
||||
cmd = api.Command.example
|
||||
assert cmd(version=u'2.47') == dict(
|
||||
result=('execute', (None,), dict(version=u'2.47', server=False))
|
||||
result=('execute', (), dict(version=u'2.47'))
|
||||
)
|
||||
assert cmd(u'var', version=u'2.47') == dict(
|
||||
result=('execute', (u'var',), dict(version=u'2.47', server=False))
|
||||
result=('execute', (u'var',), dict(version=u'2.47'))
|
||||
)
|
||||
assert cmd(server=True, version=u'2.47') == dict(
|
||||
result=('forward', (None,), dict(version=u'2.47', server=True))
|
||||
result=('forward', (), dict(version=u'2.47', server=True))
|
||||
)
|
||||
assert cmd(u'var', server=True, version=u'2.47') == dict(
|
||||
result=('forward', (u'var',), dict(version=u'2.47', server=True))
|
||||
)
|
||||
|
||||
# Test when in_server=True (should always call execute):
|
||||
(api, home) = create_test_api(in_server=True)
|
||||
api.register(example)
|
||||
api, _home = create_test_api(in_server=True)
|
||||
api.add_plugin(example)
|
||||
api.finalize()
|
||||
cmd = api.Command.example
|
||||
assert cmd(version=u'2.47') == dict(
|
||||
result=('execute', (None,), dict(version=u'2.47', server=False))
|
||||
result=('execute', (), dict(version=u'2.47', server=False))
|
||||
)
|
||||
assert cmd(u'var', version=u'2.47') == dict(
|
||||
result=('execute', (u'var',), dict(version=u'2.47', server=False))
|
||||
)
|
||||
assert cmd(server=True, version=u'2.47') == dict(
|
||||
result=('execute', (None,), dict(version=u'2.47', server=True))
|
||||
result=('execute', (), dict(version=u'2.47', server=True))
|
||||
)
|
||||
assert cmd(u'var', server=True, version=u'2.47') == dict(
|
||||
result=('execute', (u'var',), dict(version=u'2.47', server=True))
|
||||
@@ -869,16 +915,6 @@ class test_Object(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Object.__init__` method.
|
||||
"""
|
||||
o = self.cls()
|
||||
assert o.backend is None
|
||||
assert o.methods is None
|
||||
assert o.params is None
|
||||
assert o.params_minus_pk is None
|
||||
|
||||
def test_set_api(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Object.set_api` method.
|
||||
"""
|
||||
# Setup for test:
|
||||
class DummyAttribute(object):
|
||||
def __init__(self, obj_name, attr_name, name=None):
|
||||
@@ -888,6 +924,9 @@ class test_Object(ClassChecker):
|
||||
self.name = '%s_%s' % (obj_name, attr_name)
|
||||
else:
|
||||
self.name = name
|
||||
self.bases = (DummyAttribute,)
|
||||
self.version = '1'
|
||||
self.full_name = '{}/{}'.format(self.name, self.version)
|
||||
self.param = frontend.create_param(attr_name)
|
||||
|
||||
def __clone__(self, attr_name):
|
||||
@@ -899,33 +938,40 @@ class test_Object(ClassChecker):
|
||||
|
||||
def get_attributes(cnt, format):
|
||||
for name in ['other', 'user', 'another']:
|
||||
for i in xrange(cnt):
|
||||
for i in range(cnt):
|
||||
yield DummyAttribute(name, format % i)
|
||||
|
||||
cnt = 10
|
||||
methods_format = 'method_%d'
|
||||
|
||||
_d = dict(
|
||||
Method=plugable.NameSpace(
|
||||
get_attributes(cnt, methods_format)
|
||||
),
|
||||
)
|
||||
api = plugable.MagicDict(_d)
|
||||
class FakeAPI(object):
|
||||
def __init__(self):
|
||||
self._API__plugins = get_attributes(cnt, methods_format)
|
||||
self._API__default_map = {}
|
||||
self.Method = plugable.APINameSpace(self, DummyAttribute)
|
||||
def __contains__(self, key):
|
||||
return hasattr(self, key)
|
||||
def __getitem__(self, key):
|
||||
return getattr(self, key)
|
||||
def is_production_mode(self):
|
||||
return False
|
||||
def _get(self, plugin):
|
||||
return plugin
|
||||
api = FakeAPI()
|
||||
assert len(api.Method) == cnt * 3
|
||||
|
||||
class user(self.cls):
|
||||
pass
|
||||
|
||||
# Actually perform test:
|
||||
o = user()
|
||||
o.set_api(api)
|
||||
o = user(api)
|
||||
assert read_only(o, 'api') is api
|
||||
|
||||
namespace = o.methods
|
||||
assert isinstance(namespace, plugable.NameSpace)
|
||||
assert isinstance(namespace, NameSpace)
|
||||
assert len(namespace) == cnt
|
||||
f = methods_format
|
||||
for i in xrange(cnt):
|
||||
for i in range(cnt):
|
||||
attr_name = f % i
|
||||
attr = namespace[attr_name]
|
||||
assert isinstance(attr, DummyAttribute)
|
||||
@@ -935,17 +981,15 @@ class test_Object(ClassChecker):
|
||||
assert attr.name == '%s_%s' % ('user', attr_name)
|
||||
|
||||
# Test params instance attribute
|
||||
o = self.cls()
|
||||
o.set_api(api)
|
||||
o = self.cls(api)
|
||||
ns = o.params
|
||||
assert type(ns) is plugable.NameSpace
|
||||
assert type(ns) is NameSpace
|
||||
assert len(ns) == 0
|
||||
class example(self.cls):
|
||||
takes_params = ('banana', 'apple')
|
||||
o = example()
|
||||
o.set_api(api)
|
||||
o = example(api)
|
||||
ns = o.params
|
||||
assert type(ns) is plugable.NameSpace
|
||||
assert type(ns) is NameSpace
|
||||
assert len(ns) == 2, repr(ns)
|
||||
assert list(ns) == ['banana', 'apple']
|
||||
for p in ns():
|
||||
@@ -957,7 +1001,7 @@ class test_Object(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Object.primary_key` attribute.
|
||||
"""
|
||||
(api, home) = create_test_api()
|
||||
api, _home = create_test_api()
|
||||
api.finalize()
|
||||
|
||||
# Test with no primary keys:
|
||||
@@ -966,8 +1010,7 @@ class test_Object(ClassChecker):
|
||||
'one',
|
||||
'two',
|
||||
)
|
||||
o = example1()
|
||||
o.set_api(api)
|
||||
o = example1(api)
|
||||
assert o.primary_key is None
|
||||
|
||||
# Test with 1 primary key:
|
||||
@@ -978,14 +1021,13 @@ class test_Object(ClassChecker):
|
||||
parameters.Str('three', primary_key=True),
|
||||
'four',
|
||||
)
|
||||
o = example2()
|
||||
o.set_api(api)
|
||||
o = example2(api)
|
||||
pk = o.primary_key
|
||||
assert type(pk) is parameters.Str
|
||||
assert pk.name == 'three'
|
||||
assert pk.primary_key is True
|
||||
assert o.params[2] is o.primary_key
|
||||
assert isinstance(o.params_minus_pk, plugable.NameSpace)
|
||||
assert isinstance(o.params_minus_pk, NameSpace)
|
||||
assert list(o.params_minus_pk) == ['one', 'two', 'four']
|
||||
|
||||
# Test with multiple primary_key:
|
||||
@@ -996,8 +1038,7 @@ class test_Object(ClassChecker):
|
||||
'three',
|
||||
parameters.Str('four', primary_key=True),
|
||||
)
|
||||
o = example3()
|
||||
o.set_api(api)
|
||||
o = example3(api)
|
||||
e = raises(ValueError, o.finalize)
|
||||
assert str(e) == \
|
||||
'example3 (Object) has multiple primary keys: one, two, four'
|
||||
@@ -1006,13 +1047,13 @@ class test_Object(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Object.backend` attribute.
|
||||
"""
|
||||
(api, home) = create_test_api()
|
||||
api, _home = create_test_api()
|
||||
class ldap(backend.Backend):
|
||||
whatever = 'It worked!'
|
||||
api.register(ldap)
|
||||
api.add_plugin(ldap)
|
||||
class user(frontend.Object):
|
||||
backend_name = 'ldap'
|
||||
api.register(user)
|
||||
api.add_plugin(user)
|
||||
api.finalize()
|
||||
b = api.Object.user.backend
|
||||
assert isinstance(b, ldap)
|
||||
@@ -1022,12 +1063,13 @@ class test_Object(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Object.get_dn` method.
|
||||
"""
|
||||
o = self.cls()
|
||||
api = 'the api instance'
|
||||
o = self.cls(api)
|
||||
e = raises(NotImplementedError, o.get_dn, 'primary key')
|
||||
assert str(e) == 'Object.get_dn()'
|
||||
class user(self.cls):
|
||||
pass
|
||||
o = user()
|
||||
o = user(api)
|
||||
e = raises(NotImplementedError, o.get_dn, 'primary key')
|
||||
assert str(e) == 'user.get_dn()'
|
||||
|
||||
@@ -1037,9 +1079,9 @@ class test_Object(ClassChecker):
|
||||
"""
|
||||
class example(self.cls):
|
||||
takes_params = ('one', 'two', 'three', 'four')
|
||||
o = example()
|
||||
(api, home) = create_test_api()
|
||||
o.set_api(api)
|
||||
api, _home = create_test_api()
|
||||
api.finalize()
|
||||
o = example(api)
|
||||
p = o.params
|
||||
assert tuple(o.params_minus()) == tuple(p())
|
||||
assert tuple(o.params_minus([])) == tuple(p())
|
||||
@@ -1070,28 +1112,19 @@ class test_Attribute(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Attribute.__init__` method.
|
||||
"""
|
||||
class user_add(self.cls):
|
||||
pass
|
||||
o = user_add()
|
||||
assert read_only(o, 'obj') is None
|
||||
assert read_only(o, 'obj_name') == 'user'
|
||||
assert read_only(o, 'attr_name') == 'add'
|
||||
|
||||
def test_set_api(self):
|
||||
"""
|
||||
Test the `ipalib.frontend.Attribute.set_api` method.
|
||||
"""
|
||||
user_obj = 'The user frontend.Object instance'
|
||||
class api(object):
|
||||
Object = dict(user=user_obj)
|
||||
Object = {("user", "1"): user_obj}
|
||||
@staticmethod
|
||||
def is_production_mode():
|
||||
return False
|
||||
class user_add(self.cls):
|
||||
pass
|
||||
o = user_add()
|
||||
assert read_only(o, 'api') is None
|
||||
assert read_only(o, 'obj') is None
|
||||
o.set_api(api)
|
||||
o = user_add(api)
|
||||
assert read_only(o, 'api') is api
|
||||
assert read_only(o, 'obj') is user_obj
|
||||
assert read_only(o, 'obj_name') == 'user'
|
||||
assert read_only(o, 'attr_name') == 'add'
|
||||
|
||||
|
||||
class test_Method(ClassChecker):
|
||||
@@ -1104,7 +1137,7 @@ class test_Method(ClassChecker):
|
||||
"""
|
||||
Return a finalized `ipalib.plugable.API` instance.
|
||||
"""
|
||||
(api, home) = create_test_api()
|
||||
api, _home = create_test_api()
|
||||
class user(frontend.Object):
|
||||
takes_params = (
|
||||
'givenname',
|
||||
@@ -1115,8 +1148,8 @@ class test_Method(ClassChecker):
|
||||
class user_verb(self.cls):
|
||||
takes_args = args
|
||||
takes_options = options
|
||||
api.register(user)
|
||||
api.register(user_verb)
|
||||
api.add_plugin(user)
|
||||
api.add_plugin(user_verb)
|
||||
api.finalize()
|
||||
return api
|
||||
|
||||
@@ -1130,9 +1163,10 @@ class test_Method(ClassChecker):
|
||||
"""
|
||||
Test the `ipalib.frontend.Method.__init__` method.
|
||||
"""
|
||||
api = 'the api instance'
|
||||
class user_add(self.cls):
|
||||
pass
|
||||
o = user_add()
|
||||
o = user_add(api)
|
||||
assert o.name == 'user_add'
|
||||
assert o.obj_name == 'user'
|
||||
assert o.attr_name == 'add'
|
||||
|
||||
Reference in New Issue
Block a user