def test_run(self): """ Test the `ipalib.frontend.Command.run` method. """ 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,') 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.finalize() o = my_cmd() o.set_api(api) assert o.run.im_func is self.cls.run.im_func out = o.run(*args, **kw) del kw['version'] assert ('execute', args, kw) == out # Test in non-server context (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 assert ('forward', args, kw) == o.run(*args, **kw)
def test_run(self): """ Test the `ipalib.frontend.LocalOrRemote.run` method. """ class example(self.cls): takes_args = 'key?' def forward(self, *args, **options): return dict(result=('forward', args, options)) def execute(self, *args, **options): return dict(result=('execute', args, options)) # Test when in_server=False: (api, home) = create_test_api(in_server=False) api.register(example) api.finalize() cmd = api.Command.example assert cmd() == dict( result=('execute', (None,), dict(server=False)) ) assert cmd(u'var') == dict( result=('execute', (u'var',), dict(server=False)) ) assert cmd(server=True) == dict( result=('forward', (None,), dict(server=True)) ) assert cmd(u'var', server=True) == dict( result=('forward', (u'var',), dict(server=True)) ) # Test when in_server=True (should always call execute): (api, home) = create_test_api(in_server=True) api.register(example) api.finalize() cmd = api.Command.example assert cmd() == dict( result=('execute', (None,), dict(server=False)) ) assert cmd(u'var') == dict( result=('execute', (u'var',), dict(server=False)) ) assert cmd(server=True) == dict( result=('execute', (None,), dict(server=True)) ) assert cmd(u'var', server=True) == dict( result=('execute', (u'var',), dict(server=True)) )
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') (api, home) = create_test_api() api.finalize() o = my_cmd() o.set_api(api) o.finalize() e = o(**kw) assert type(e) is dict assert 'result' in e assert 'option2' in e['result'] assert e['result']['option2'] == u'some value'
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') (api, home) = create_test_api() api.finalize() o = my_cmd() o.set_api(api) 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'
def test_load_plugins(self): """ Test the `ipalib.plugable.API.load_plugins` method. """ (o, home) = create_test_api() assert o.isdone('bootstrap') is False assert o.isdone('load_plugins') is False o.load_plugins() assert o.isdone('bootstrap') is True assert o.isdone('load_plugins') is True e = raises(StandardError, o.load_plugins) assert str(e) == 'API.load_plugins() already called'
def test_primary_key(self): """ Test the `ipalib.frontend.Object.primary_key` attribute. """ (api, home) = create_test_api() api.finalize() # Test with no primary keys: class example1(self.cls): takes_params = ( 'one', 'two', ) o = example1() o.set_api(api) assert o.primary_key is None # Test with 1 primary key: class example2(self.cls): takes_params = ( 'one', 'two', parameters.Str('three', primary_key=True), 'four', ) o = example2() o.set_api(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 list(o.params_minus_pk) == ['one', 'two', 'four'] # Test with multiple primary_key: class example3(self.cls): takes_params = ( parameters.Str('one', primary_key=True), parameters.Str('two', primary_key=True), 'three', parameters.Str('four', primary_key=True), ) o = example3() o.set_api(api) e = raises(ValueError, o.finalize) assert str(e) == \ 'example3 (Object) has multiple primary keys: one, two, four'
def test_run(self): """ Test the `ipalib.frontend.LocalOrRemote.run` method. """ class example(self.cls): takes_args = 'key?' def forward(self, *args, **options): return dict(result=('forward', args, options)) def execute(self, *args, **options): return dict(result=('execute', args, options)) # Test when in_server=False: (api, home) = create_test_api(in_server=False) api.register(example) api.finalize() cmd = api.Command.example assert cmd() == dict(result=('execute', (None, ), dict(server=False))) assert cmd(u'var') == dict(result=('execute', (u'var', ), dict(server=False))) assert cmd(server=True) == dict(result=('forward', (None, ), dict(server=True))) assert cmd(u'var', server=True) == dict(result=('forward', (u'var', ), dict(server=True))) # Test when in_server=True (should always call execute): (api, home) = create_test_api(in_server=True) api.register(example) api.finalize() cmd = api.Command.example assert cmd() == dict(result=('execute', (None, ), dict(server=False))) assert cmd(u'var') == dict(result=('execute', (u'var', ), dict(server=False))) assert cmd(server=True) == dict(result=('execute', (None, ), dict(server=True))) assert cmd(u'var', server=True) == dict(result=('execute', (u'var', ), dict(server=True)))
def test_backend(self): """ Test the `ipalib.frontend.Object.backend` attribute. """ (api, home) = create_test_api() class ldap(backend.Backend): whatever = 'It worked!' api.register(ldap) class user(frontend.Object): backend_name = 'ldap' api.register(user) api.finalize() b = api.Object.user.backend assert isinstance(b, ldap) assert b.whatever == 'It worked!'
def test_bootstrap(self): """ Test the `ipalib.plugable.API.bootstrap` method. """ (o, home) = create_test_api() assert o.env._isdone('_bootstrap') is False assert o.env._isdone('_finalize_core') is False assert o.isdone('bootstrap') is False o.bootstrap(my_test_override='Hello, world!') assert o.isdone('bootstrap') is True assert o.env._isdone('_bootstrap') is True assert o.env._isdone('_finalize_core') is True assert o.env.my_test_override == 'Hello, world!' e = raises(StandardError, o.bootstrap) assert str(e) == 'API.bootstrap() already called'
def test_params_minus(self): """ Test the `ipalib.frontend.Object.params_minus` method. """ class example(self.cls): takes_params = ('one', 'two', 'three', 'four') o = example() (api, home) = create_test_api() o.set_api(api) 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)
def get_api(self, args=tuple(), options=tuple()): """ Return a finalized `ipalib.plugable.API` instance. """ (api, home) = create_test_api() 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 api.register(user) api.register(user_verb) api.finalize() return api
def test_execute(self): """ Test the `ipalib.backend.Executioner.execute` method. """ (api, home) = create_test_api(in_server=True) class echo(Command): takes_args = ('arg1', 'arg2+') takes_options = ('option1?', 'option2?') def execute(self, *args, **options): assert type(args[1]) is tuple return dict(result=args + (options, )) api.register(echo) class good(Command): def execute(self): raise errors.ValidationError( name='nurse', error=u'Not naughty!', ) api.register(good) class bad(Command): def execute(self): raise ValueError('This is private.') api.register(bad) class with_name(Command): """ Test that a kwarg named 'name' can be used. """ takes_options = 'name' def execute(self, **options): return dict(result=options['name'].upper()) api.register(with_name) api.finalize() o = self.cls() o.set_api(api) o.finalize() # Test that CommandError is raised: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn print str(context.__dict__.keys()) e = raises(errors.CommandError, o.execute, 'nope') assert e.name == 'nope' assert conn.disconnect.called is True # Make sure destroy_context() was called print str(context.__dict__.keys()) assert context.__dict__.keys() == [] # Test with echo command: arg1 = unicode_str arg2 = (u'Hello', unicode_str, u'world!') args = (arg1, ) + arg2 options = dict(option1=u'How are you?', option2=unicode_str) conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('echo', arg1, arg2, **options) == dict(result=(arg1, arg2, options)) assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('echo', *args, **options) == dict(result=(arg1, arg2, options)) assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with good command: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn e = raises(errors.ValidationError, o.execute, 'good') assert e.name == 'nurse' assert e.error == u'Not naughty!' assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with bad command: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn e = raises(errors.InternalError, o.execute, 'bad') assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with option 'name': conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('with_name', name=u'test') == dict(result=u'TEST')
def test_execute(self): """ Test the `ipalib.backend.Executioner.execute` method. """ (api, home) = create_test_api(in_server=True) class echo(Command): takes_args = ('arg1', 'arg2+') takes_options = ('option1?', 'option2?') def execute(self, *args, **options): assert type(args[1]) is tuple return dict(result=args + (options,)) api.register(echo) class good(Command): def execute(self): raise errors.ValidationError( name='nurse', error=u'Not naughty!', ) api.register(good) class bad(Command): def execute(self): raise ValueError('This is private.') api.register(bad) class with_name(Command): """ Test that a kwarg named 'name' can be used. """ takes_options = 'name' def execute(self, **options): return dict(result=options['name'].upper()) api.register(with_name) api.finalize() o = self.cls() o.set_api(api) o.finalize() # Test that CommandError is raised: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn print str(context.__dict__.keys()) e = raises(errors.CommandError, o.execute, 'nope') assert e.name == 'nope' assert conn.disconnect.called is True # Make sure destroy_context() was called print str(context.__dict__.keys()) assert context.__dict__.keys() == [] # Test with echo command: arg1 = unicode_str arg2 = (u'Hello', unicode_str, u'world!') args = (arg1,) + arg2 options = dict(option1=u'How are you?', option2=unicode_str) conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('echo', arg1, arg2, **options) == dict( result=(arg1, arg2, options) ) assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('echo', *args, **options) == dict( result=(arg1, arg2, options) ) assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with good command: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn e = raises(errors.ValidationError, o.execute, 'good') assert e.name == 'nurse' assert e.error == u'Not naughty!' assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with bad command: conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn e = raises(errors.InternalError, o.execute, 'bad') assert conn.disconnect.called is True # Make sure destroy_context() was called assert context.__dict__.keys() == [] # Test with option 'name': conn = Connection('The connection.', Disconnect('someconn')) context.someconn = conn assert o.execute('with_name', name=u'test') == dict(result=u'TEST')