Skip to content

Commit

Permalink
Merge pull request #75 from tarunpaul/handle_user_attributes_of_type_…
Browse files Browse the repository at this point in the history
…list

Made changes to add sub element for each value in the list
  • Loading branch information
jbittel authored Feb 7, 2019
2 parents 775543b + 2944f2a commit 03935d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions mama_cas/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,13 @@ def render_content(self, context):
if attributes:
attribute_set = etree.SubElement(auth_success, self.ns('attributes'))
for name, value in attributes.items():
attr = etree.SubElement(attribute_set, self.ns(name))
attr.text = force_text(value)
if isinstance(value, list):
for v in value:
attr = etree.SubElement(attribute_set, self.ns(name))
attr.text = force_text(v)
else:
attr = etree.SubElement(attribute_set, self.ns(name))
attr.text = force_text(value)
if pgt:
proxy_granting_ticket = etree.SubElement(auth_success, self.ns('proxyGrantingTicket'))
proxy_granting_ticket.text = pgt.iou
Expand Down
27 changes: 27 additions & 0 deletions mama_cas/tests/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,33 @@ def test_validation_response_attributes(self):
del attrs[child.tag]
self.assertEqual(len(attrs), 0)

def test_validation_response_list_type_attributes(self):
"""
When given a custom user attribute of type list, a ``ValidationResponse``
should include all the list items as values in the resposne.
"""
attrs = {'givenName': 'Ellen', 'sn': 'Cohen', 'email': '[email protected]',
'groups': ['group1', 'group2', 'group3']}
resp = ValidationResponse(context={'ticket': self.st, 'error': None,
'attributes': attrs},
content_type='text/xml')
attributes = parse(resp.content).find('./authenticationSuccess/attributes')
self.assertIsNotNone(attributes)
total_attributes = 0
for attr_key in attrs.keys():
attr_values = attributes.findall(attr_key)
if(len(attr_values) > 1):
self.assertEqual(len(attr_values), len(attrs[attr_key]))
for attr_value in attr_values:
self.assertTrue(attr_value.text in attrs[attr_key])
total_attributes += len(attrs[attr_key])
else:
attr_value = attr_values[0]
self.assertTrue(attr_value.tag in attrs)
self.assertEqual(attr_value.text, attrs[attr_value.tag])
total_attributes += 1
self.assertEqual(len(attributes), total_attributes)

def test_validation_response_nonstring_attributes(self):
"""
When given non-string attributes, the values should be
Expand Down

0 comments on commit 03935d9

Please sign in to comment.