Beispiel #1
0
    def test_filter_param_by_context(self):
        """
        Test the `ipalib.frontend.HasParam._filter_param_by_context` method.
        """
        api = 'the api instance'

        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']),
                )

        o = Example(api)

        # 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

        o = Missing(api)
        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')

        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' % (
            'NotCallable', 'get_stuff', NotCallable.get_stuff)
Beispiel #2
0
    def test_validate(self):
        """
        Test the `ipalib.frontend.Command.validate` method.
        """

        sub = self.subcls()
        sub.env = config.Env(context='cli')
        sub.finalize()

        # Check with valid values
        okay = dict(
            option0=u'option0',
            option1=u'option1',
            another_option='some value',
            version=API_VERSION,
        )
        sub.validate(**okay)

        # Check with an invalid value
        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.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)
        fail.pop('option1')
        e = raises(errors.RequirementError, sub.validate, **fail)
        assert e.name == 'option1'
Beispiel #3
0
    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(),
        )
Beispiel #4
0
        class api:
            env = config.Env(context='cli')

            @staticmethod
            def is_production_mode():
                return False