Fixed some AttributeValue problems

This commit is contained in:
Roland Hedberg 2013-01-12 11:30:03 +01:00
parent 321f17382a
commit 4e0e8b952f
2 changed files with 34 additions and 20 deletions

View File

@ -99,14 +99,6 @@ def _verify_value_type(typ, val):
return base64.decodestring(val)
class AttributeValueBase(SamlBase):
def __setattr__(self, key, value):
if key == "text":
self.set_text(value)
else:
SamlBase.__setattr__(self,key, value)
def __init__(self,
text=None,
extension_elements=None,
@ -127,6 +119,12 @@ class AttributeValueBase(SamlBase):
else:
self.set_text(text)
def __setattr__(self, key, value):
if key == "text":
self.set_text(value)
else:
SamlBase.__setattr__(self,key, value)
def verify(self):
if not self.text:
assert self.extension_attributes
@ -150,6 +148,16 @@ class AttributeValueBase(SamlBase):
except KeyError:
return ""
def clear_type(self):
try:
del self.extension_attributes[XSI_TYPE]
except KeyError:
pass
try:
del self._extatt[XSI_TYPE]
except KeyError:
pass
def set_text(self, val, base64encode=False):
typ = self.get_type()
if base64encode:
@ -161,7 +169,20 @@ class AttributeValueBase(SamlBase):
if not typ:
self.set_type("xs:string")
else:
assert typ == "xs:string"
try:
assert typ == "xs:string"
except AssertionError:
if typ == "xs:int":
_ = int(val)
elif typ == "xs:boolean":
if val.lower() not in ["true", "false"]:
raise ValueError("Not a boolean")
elif typ == "xs:float":
_ = float(val)
elif typ == "xs:base64Binary":
pass
else:
ValueError("Type and value doesn't match")
elif isinstance(val, bool):
if val:
val = "true"
@ -206,6 +227,8 @@ class AttributeValueBase(SamlBase):
self._convert_element_attribute_to_member(attribute, value)
if tree.text:
#print "set_text:", tree.text
# clear type
#self.clear_type()
self.set_text(tree.text)
try:
typ = self.extension_attributes[XSI_TYPE]

View File

@ -243,8 +243,8 @@ class TestSAMLBase:
assert av.text == "true"
av.set_text(False)
assert av.text == "false"
av.set_text(491)
assert av.text == "491"
# can't change value to another type
raises(AssertionError, "av.set_text(491)")
av = AttributeValue()
av.set_text(None)
@ -698,15 +698,6 @@ class TestAttribute:
print attribute
assert attribute.attribute_value[0].text == "23"
def test_basic_not_int(self):
# attr = saml.attribute_from_string(BASIC_NOT_INT_AV)
# print attr.__dict__.keys()
# print attr.attribute_value[0].__dict__.keys()
# print attr.attribute_value[0].type
# print attr.attribute_value[0].extension_attributes
# print attr
raises(ValueError, "saml.attribute_from_string(BASIC_NOT_INT_AV)")
def test_basic_base64(self):
attribute = saml.attribute_from_string(BASIC_BASE64_AV)
print attribute