mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2024-12-24 16:10:02 -06:00
Use context to decide which name to return on RequirementsErrors
When a Requirement fails we throw an exception including the name of the field that is missing. To make the command-line friendlier we have a cli_name defined which may or may not match the LDAP attribute. This can be confusing if you are using ipalib directly because the attribute name missing may not match what is actually required (desc vs description is a good example). If you use the context 'cli' then it will throw exceptions using cli_name. If you use any other context it will use the name of the attribute. ticket 187
This commit is contained in:
parent
ff636984ab
commit
33802ab712
@ -656,7 +656,7 @@ class Command(HasParam):
|
||||
"""
|
||||
for param in self.params():
|
||||
value = kw.get(param.name, None)
|
||||
param.validate(value)
|
||||
param.validate(value, self.env.context)
|
||||
|
||||
def run(self, *args, **options):
|
||||
"""
|
||||
|
@ -476,7 +476,10 @@ class Param(ReadOnly):
|
||||
value = self.get_default(**kw)
|
||||
else:
|
||||
value = self.convert(self.normalize(value))
|
||||
self.validate(value)
|
||||
if hasattr(self, 'env'):
|
||||
self.validate(value, self.env.context)
|
||||
else:
|
||||
self.validate(value)
|
||||
return value
|
||||
|
||||
def kw(self):
|
||||
@ -696,15 +699,19 @@ class Param(ReadOnly):
|
||||
error=ugettext(self.type_error),
|
||||
)
|
||||
|
||||
def validate(self, value):
|
||||
def validate(self, value, context=None):
|
||||
"""
|
||||
Check validity of ``value``.
|
||||
|
||||
:param value: A proposed value for this parameter.
|
||||
:param context: The context we are running in.
|
||||
"""
|
||||
if value is None:
|
||||
if self.required:
|
||||
raise RequirementError(name=self.cli_name)
|
||||
if context == 'cli':
|
||||
raise RequirementError(name=self.cli_name)
|
||||
else:
|
||||
raise RequirementError(name=self.name)
|
||||
return
|
||||
if self.query:
|
||||
return
|
||||
@ -1324,9 +1331,9 @@ class StrEnum(Enum):
|
||||
For example:
|
||||
|
||||
>>> enum = StrEnum('my_enum', values=(u'One', u'Two', u'Three'))
|
||||
>>> enum.validate(u'Two') is None
|
||||
>>> enum.validate(u'Two', 'cli') is None
|
||||
True
|
||||
>>> enum.validate(u'Four')
|
||||
>>> enum.validate(u'Four', 'cli')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: invalid 'my_enum': must be one of (u'One', u'Two', u'Three')
|
||||
|
@ -374,6 +374,7 @@ class test_Command(ClassChecker):
|
||||
takes_options = ('givenname', 'sn')
|
||||
|
||||
cmd = user_add()
|
||||
cmd.env = config.Env(context='cli')
|
||||
cmd.finalize()
|
||||
assert list(cmd.params) == ['givenname', 'sn', 'uid']
|
||||
ret = cmd.soft_validate({})
|
||||
@ -422,6 +423,7 @@ class test_Command(ClassChecker):
|
||||
"""
|
||||
|
||||
sub = self.subcls()
|
||||
sub.env = config.Env(context='cli')
|
||||
sub.finalize()
|
||||
|
||||
# Check with valid values
|
||||
|
@ -408,32 +408,38 @@ class test_Param(ClassChecker):
|
||||
|
||||
# Test in default state (with no rules, no kwarg):
|
||||
o = self.cls('my_param')
|
||||
e = raises(errors.RequirementError, o.validate, None)
|
||||
e = raises(errors.RequirementError, o.validate, None, 'cli')
|
||||
assert e.name == 'my_param'
|
||||
|
||||
# Test in default state that cli_name gets returned in the exception
|
||||
# when context == 'cli'
|
||||
o = self.cls('my_param', cli_name='short')
|
||||
e = raises(errors.RequirementError, o.validate, None, 'cli')
|
||||
assert e.name == 'short'
|
||||
|
||||
# Test with required=False
|
||||
o = self.cls('my_param', required=False)
|
||||
assert o.required is False
|
||||
assert o.validate(None) is None
|
||||
assert o.validate(None, 'cli') is None
|
||||
|
||||
# Test with query=True:
|
||||
o = self.cls('my_param', query=True)
|
||||
assert o.query is True
|
||||
e = raises(errors.RequirementError, o.validate, None)
|
||||
e = raises(errors.RequirementError, o.validate, None, 'cli')
|
||||
assert_equal(e.name, 'my_param')
|
||||
|
||||
# Test with multivalue=True:
|
||||
o = self.cls('my_param', multivalue=True)
|
||||
e = raises(TypeError, o.validate, [])
|
||||
e = raises(TypeError, o.validate, [], 'cli')
|
||||
assert str(e) == TYPE_ERROR % ('value', tuple, [], list)
|
||||
e = raises(ValueError, o.validate, tuple())
|
||||
e = raises(ValueError, o.validate, tuple(), 'cli')
|
||||
assert str(e) == 'value: empty tuple must be converted to None'
|
||||
|
||||
# Test with wrong (scalar) type:
|
||||
e = raises(ValidationError, o.validate, (None, None, 42, None))
|
||||
e = raises(ValidationError, o.validate, (None, None, 42, None), 'cli')
|
||||
assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', NoneType, 42, int))
|
||||
o = self.cls('my_param')
|
||||
e = raises(ValidationError, o.validate, 'Hello')
|
||||
e = raises(ValidationError, o.validate, 'Hello', 'cli')
|
||||
assert str(e) == 'invalid %s' % (TYPE_ERROR % ('\'my_param\'', NoneType, 'Hello', str))
|
||||
|
||||
class Example(self.cls):
|
||||
@ -445,13 +451,13 @@ class test_Param(ClassChecker):
|
||||
fail = DummyRule(u'no good')
|
||||
o = Example('example', pass1, pass2)
|
||||
assert o.multivalue is False
|
||||
assert o.validate(11) is None
|
||||
assert o.validate(11, 'cli') is None
|
||||
assert pass1.calls == [(request.ugettext, 11)]
|
||||
assert pass2.calls == [(request.ugettext, 11)]
|
||||
pass1.reset()
|
||||
pass2.reset()
|
||||
o = Example('example', pass1, pass2, fail)
|
||||
e = raises(errors.ValidationError, o.validate, 42)
|
||||
e = raises(errors.ValidationError, o.validate, 42, 'cli')
|
||||
assert e.name == 'example'
|
||||
assert e.error == u'no good'
|
||||
assert e.index is None
|
||||
@ -465,7 +471,7 @@ class test_Param(ClassChecker):
|
||||
fail = DummyRule(u'this one is not good')
|
||||
o = Example('example', pass1, pass2, multivalue=True)
|
||||
assert o.multivalue is True
|
||||
assert o.validate((3, 9)) is None
|
||||
assert o.validate((3, 9), 'cli') is None
|
||||
assert pass1.calls == [
|
||||
(request.ugettext, 3),
|
||||
(request.ugettext, 9),
|
||||
@ -478,7 +484,7 @@ class test_Param(ClassChecker):
|
||||
pass2.reset()
|
||||
o = Example('multi_example', pass1, pass2, fail, multivalue=True)
|
||||
assert o.multivalue is True
|
||||
e = raises(errors.ValidationError, o.validate, (3, 9))
|
||||
e = raises(errors.ValidationError, o.validate, (3, 9), 'cli')
|
||||
assert e.name == 'multi_example'
|
||||
assert e.error == u'this one is not good'
|
||||
assert e.index == 0
|
||||
|
Loading…
Reference in New Issue
Block a user