Skip to content

Commit

Permalink
Merge pull request #10 from btimby/unknown-attributes
Browse files Browse the repository at this point in the history
Allow use of unknown radius codes (but not names).
  • Loading branch information
btimby authored May 7, 2018
2 parents 4e663b7 + ba642d2 commit 3e949d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions radius.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ def __init__(self, initialdata={}):
def __getkeys(self, value):
"""Return tuple of code, name for given code or name."""
if isinstance(value, int):
return value, ATTRS[value]
return value, ATTRS.get(value, None)

else:
id = ATTR_NAMES[value.lower()]
return id, ATTRS[id]
Expand Down Expand Up @@ -355,8 +356,13 @@ def __setitem__(self, key, value):
"""
try:
code, name = self.__getkeys(key)

except KeyError:
raise ValueError('Invalid radius attribute: %s' % key)
raise ValueError('Unknown radius attribute: %s' % key)

if name is None:
LOGGER.warning('Unknown radius attribute code %s' % code)

values = self.get(code, [])
values.append(value)
UserDict.__setitem__(self, code, values)
Expand Down
9 changes: 5 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def test_set_get_item(self):
"""Test setting and getting items."""
a = radius.Attributes()

# Cannot use invalid radius codes or names.
with self.assertRaises(ValueError):
a[128] = b'bar'
# Can use unknown radius codes.
a[128] = b'bar'

# Cannot use invalid radius names.
with self.assertRaises(ValueError):
a['foo'] = b'bar'

Expand All @@ -66,7 +66,8 @@ def test_set_get_item(self):
self.assertEqual([b'foobar'], a[radius.ATTR_USER_NAME])
self.assertEqual([b'foobar'], a['user-name'])
self.assertEqual([b'foobar'], a['user-Name'])
self.assertEqual([('User-Name', ['foobar'])], list(a.nameditems()))
self.assertEqual(
[(None, ['bar']), ('User-Name', ['foobar'])], list(a.nameditems()))

def test_init_update(self):
"""Test __init__ and update."""
Expand Down

0 comments on commit 3e949d5

Please sign in to comment.