Fix --setattr to work on no_update params. https://fedorahosted.org/freeipa/ticket/2616
-- PetrĀ³
From b22c159e4f4c3d411850b30267fce61e56100acd Mon Sep 17 00:00:00 2001 From: Petr Viktorin <pvikt...@redhat.com> Date: Tue, 10 Apr 2012 07:44:21 -0400 Subject: [PATCH] Convert --setattr values for attributes marked no_update Attribute Patrams marked no_update never get cloned to Update commands, and thus never receive the `attribute` flag. This makes their `encode` method a no-op, which meant they don't get properly encoded when used with --setattr, making the --setattr fail. Introduce a `force` argument to encode, which overrides checking for the attribute flag. Use this in set/add/delattr normalization, where we know we are dealing with attributes. https://fedorahosted.org/freeipa/ticket/2616 --- ipalib/parameters.py | 6 ++++-- ipalib/plugins/baseldap.py | 7 ++++++- tests/test_xmlrpc/test_attr.py | 2 +- tests/test_xmlrpc/test_hbac_plugin.py | 21 +++++++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/ipalib/parameters.py b/ipalib/parameters.py index 60fb502365fecb02c30a1081255e8db22ec5481e..5c55d8bcca03607c77be588ce7edb0a27400538d 100644 --- a/ipalib/parameters.py +++ b/ipalib/parameters.py @@ -895,7 +895,7 @@ def _validate_scalar(self, value, index=None): rule=rule, ) - def encode(self, value): + def encode(self, value, force=False): """ Encode Python native type value to chosen backend format. Encoding is applied for parameters representing actual attributes (attribute=True). @@ -909,8 +909,10 @@ def encode(self, value): `Param._encode()`. :param value: Encoded value + :param force: If set to true, encoding takes place even for Params + not marked as attribute """ - if not self.attribute: #pylint: disable=E1101 + if not self.attribute and not force: #pylint: disable=E1101 return value if self.encoder is not None: #pylint: disable=E1101 return self.encoder(value) #pylint: disable=E1101 diff --git a/ipalib/plugins/baseldap.py b/ipalib/plugins/baseldap.py index daf1b07fb60307cf52b593390be4523e70b51d2d..3e7923479519fac937f6e63dd3a0fa44a97d1ee4 100644 --- a/ipalib/plugins/baseldap.py +++ b/ipalib/plugins/baseldap.py @@ -937,7 +937,12 @@ def process_attr_options(self, entry_attrs, dn, keys, options): raise errors.ValidationError(name=attr, error=err.error) except errors.ConversionError, err: raise errors.ConversionError(name=attr, error=err.error) - value = param.encode(value) + # FIXME: We use `force` when encoding because we know this is + # an attribute, even if it does not have the `attribute` flag + # set. This happens with no_update attributes, which are + # not cloned to Update commands. This cloning is where the flag + # gets set. + value = param.encode(value, force=True) entry_attrs[attr] = value else: # unknown attribute: remove duplicite and invalid values diff --git a/tests/test_xmlrpc/test_attr.py b/tests/test_xmlrpc/test_attr.py index e6872a67a1fcfa3f782a2415bb79c416d2f75283..c19a6948c5e4976cbcb4c342e95cf5f7e48d201f 100644 --- a/tests/test_xmlrpc/test_attr.py +++ b/tests/test_xmlrpc/test_attr.py @@ -433,7 +433,7 @@ class test_attr(Declarative): command=( 'user_mod', [user1], dict( addattr=u'nsaccountlock=FaLsE', - delattr=u'nsaccountlock=True') + delattr=u'nsaccountlock=TRUE') ), expected=dict( result=dict( diff --git a/tests/test_xmlrpc/test_hbac_plugin.py b/tests/test_xmlrpc/test_hbac_plugin.py index 78c4973c95d0cc4b49eb7d6b50828931c31b2825..c7cb55bad4309f05fc0d9651f9e97d37ffe866ae 100644 --- a/tests/test_xmlrpc/test_hbac_plugin.py +++ b/tests/test_xmlrpc/test_hbac_plugin.py @@ -430,6 +430,27 @@ def test_e_hbacrule_enabled(self): # FIXME: Should this be 'enabled' or 'TRUE'? assert_attr_equal(entry, 'ipaenabledflag', 'TRUE') + def test_ea_hbacrule_disable_setattr(self): + """ + Test disabling HBAC rule using setattr + """ + command_result = api.Command['hbacrule_mod']( + self.rule_name, setattr=u'ipaenabledflag=false') + assert command_result['result']['ipaenabledflag'] == (u'FALSE',) + entry = api.Command['hbacrule_show'](self.rule_name)['result'] + assert_attr_equal(entry, 'ipaenabledflag', 'FALSE') + + def test_eb_hbacrule_enable_setattr(self): + """ + Test enabling HBAC rule using setattr + """ + command_result = api.Command['hbacrule_mod']( + self.rule_name, setattr=u'ipaenabledflag=1') + assert command_result['result']['ipaenabledflag'] == (u'TRUE',) + # check it's really enabled + entry = api.Command['hbacrule_show'](self.rule_name)['result'] + assert_attr_equal(entry, 'ipaenabledflag', 'TRUE') + @raises(errors.MutuallyExclusiveError) def test_f_hbacrule_exclusiveuser(self): """ -- 1.7.7.6
_______________________________________________ Freeipa-devel mailing list Freeipa-devel@redhat.com https://www.redhat.com/mailman/listinfo/freeipa-devel