mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
143: Added errors.RequirementError exception; cmd.validate() now raises RequirementError if a required option is missing
This commit is contained in:
parent
47fed6c4c2
commit
b4ad681f41
@ -64,11 +64,23 @@ class NormalizationError(ValidationError):
|
||||
|
||||
|
||||
class RuleError(ValidationError):
|
||||
"""
|
||||
Raised when a required option was not provided.
|
||||
"""
|
||||
def __init__(self, name, value, rule, error):
|
||||
self.rule = rule
|
||||
ValidationError.__init__(self, name, value, error)
|
||||
|
||||
|
||||
class RequirementError(ValidationError):
|
||||
"""
|
||||
Raised when a required option was not provided.
|
||||
"""
|
||||
def __init__(self, name):
|
||||
ValidationError.__init__(self, name, None,
|
||||
'missing required value'
|
||||
)
|
||||
|
||||
|
||||
class SetError(IPAError):
|
||||
msg = 'setting %r, but NameSpace does not allow attribute setting'
|
||||
|
@ -199,9 +199,13 @@ class cmd(plugable.Plugin):
|
||||
|
||||
def validate(self, **kw):
|
||||
self.print_call('validate', kw, 1)
|
||||
for (key, value) in kw.items():
|
||||
if key in self.options:
|
||||
self.options[key].validate(value)
|
||||
for opt in self.options:
|
||||
value = kw.get(opt.name, None)
|
||||
if value is None:
|
||||
if opt.required:
|
||||
raise errors.RequirementError(opt.name)
|
||||
continue
|
||||
opt.validate(value)
|
||||
|
||||
def execute(self, **kw):
|
||||
self.print_call('execute', kw, 1)
|
||||
|
@ -177,7 +177,7 @@ class test_cmd(ClassChecker):
|
||||
class option0(my_option):
|
||||
pass
|
||||
class option1(my_option):
|
||||
pass
|
||||
required = True
|
||||
class example(self.cls):
|
||||
option_classes = (option0, option1)
|
||||
return example
|
||||
@ -254,18 +254,30 @@ class test_cmd(ClassChecker):
|
||||
Tests the `validate` method.
|
||||
"""
|
||||
assert 'validate' in self.cls.__public__ # Public
|
||||
|
||||
sub = self.subcls()
|
||||
for name in ('option0', 'option1'):
|
||||
okay = {
|
||||
name: name,
|
||||
'another_option': 'some value',
|
||||
}
|
||||
fail = {
|
||||
name: 'whatever',
|
||||
'another_option': 'some value',
|
||||
}
|
||||
sub.validate(**okay)
|
||||
raises(errors.RuleError, sub.validate, **fail)
|
||||
|
||||
# Check with valid args
|
||||
okay = dict(
|
||||
option0='option0',
|
||||
option1='option1',
|
||||
another_option='some value',
|
||||
)
|
||||
sub.validate(**okay)
|
||||
|
||||
# Check with an invalid arg
|
||||
fail = dict(okay)
|
||||
fail['option0'] = 'whatever'
|
||||
raises(errors.RuleError, sub.validate, **fail)
|
||||
|
||||
# Check with a missing required arg
|
||||
fail = dict(okay)
|
||||
fail.pop('option1')
|
||||
raises(errors.RequirementError, sub.validate, **fail)
|
||||
|
||||
# Check with missing *not* required arg
|
||||
okay.pop('option0')
|
||||
sub.validate(**okay)
|
||||
|
||||
def test_execute(self):
|
||||
"""
|
||||
@ -274,7 +286,6 @@ class test_cmd(ClassChecker):
|
||||
assert 'execute' in self.cls.__public__ # Public
|
||||
|
||||
|
||||
|
||||
def test_obj():
|
||||
cls = public.obj
|
||||
assert issubclass(cls, plugable.Plugin)
|
||||
|
Loading…
Reference in New Issue
Block a user