Skip to content

Commit

Permalink
Merge pull request #336 from josephtate/betterscopesupport
Browse files Browse the repository at this point in the history
Allow tuples for list_to_scope as well as sets and lists
  • Loading branch information
thedrow committed Jul 1, 2015
2 parents 110d96b + a41f4fb commit 70879b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 3 additions & 7 deletions oauthlib/oauth2/rfc6749/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ def list_to_scope(scope):
"""Convert a list of scopes to a space separated string."""
if isinstance(scope, unicode_type) or scope is None:
return scope
elif isinstance(scope, (tuple, list)):
elif isinstance(scope, (set, tuple, list)):
return " ".join([unicode_type(s) for s in scope])
elif isinstance(scope, set):
return list_to_scope(list(scope))
else:
raise ValueError("Invalid scope, must be string or list.")
raise ValueError("Invalid scope (%s), must be string, tuple, set, or list." % scope)


def scope_to_list(scope):
"""Convert a space separated string to a list of scopes."""
if isinstance(scope, list):
if isinstance(scope, (tuple, list, set)):
return [unicode_type(s) for s in scope]
if isinstance(scope, set):
scope_to_list(list(scope))
elif scope is None:
return None
else:
Expand Down
14 changes: 14 additions & 0 deletions tests/oauth2/rfc6749/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ def test_list_to_scope(self):
obj_list = [ScopeObject('foo'), ScopeObject('bar'), ScopeObject('baz')]
self.assertEqual(list_to_scope(obj_list), expected)

set_list = set(string_list)
set_scope = list_to_scope(set_list)
assert len(set_scope.split(' ')) == 3
for x in string_list:
assert x in set_scope

self.assertRaises(ValueError, list_to_scope, object())

def test_scope_to_list(self):
expected = ['foo', 'bar', 'baz']

Expand All @@ -88,9 +96,15 @@ def test_scope_to_list(self):
string_list_scopes = ['foo', 'bar', 'baz']
self.assertEqual(scope_to_list(string_list_scopes), expected)

tuple_list_scopes = ('foo', 'bar', 'baz')
self.assertEqual(scope_to_list(tuple_list_scopes), expected)

obj_list_scopes = [ScopeObject('foo'), ScopeObject('bar'), ScopeObject('baz')]
self.assertEqual(scope_to_list(obj_list_scopes), expected)

set_list_scopes = set(string_list_scopes)
set_list = scope_to_list(set_list_scopes)
self.assertEqual(sorted(set_list), sorted(string_list_scopes))



0 comments on commit 70879b5

Please sign in to comment.