summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Tate <joseph@crunch.io>2015-04-13 12:20:27 -0400
committerJoseph Tate <joseph@crunch.io>2015-04-20 03:02:58 -0400
commit1be2f1cde1ee003d33f5445bb60642801ff7102b (patch)
tree4e6b95239667a0e0257055175601fcabbd43b425
parent514cad74bde4cd7781b496155058a79507245798 (diff)
downloadoauthlib-1be2f1cde1ee003d33f5445bb60642801ff7102b.tar.gz
Allow tuples for list_to_scope and scope_to_list in addition to sets and lists. Treat sets the same as lists/tuples instead of recursing, add tests for sets.
-rw-r--r--oauthlib/oauth2/rfc6749/utils.py8
-rw-r--r--tests/oauth2/rfc6749/test_utils.py12
2 files changed, 14 insertions, 6 deletions
diff --git a/oauthlib/oauth2/rfc6749/utils.py b/oauthlib/oauth2/rfc6749/utils.py
index 6a8e24b..49c3a2f 100644
--- a/oauthlib/oauth2/rfc6749/utils.py
+++ b/oauthlib/oauth2/rfc6749/utils.py
@@ -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.")
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:
diff --git a/tests/oauth2/rfc6749/test_utils.py b/tests/oauth2/rfc6749/test_utils.py
index 858cf1f..573b187 100644
--- a/tests/oauth2/rfc6749/test_utils.py
+++ b/tests/oauth2/rfc6749/test_utils.py
@@ -79,6 +79,12 @@ class UtilsTests(TestCase):
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
+
def test_scope_to_list(self):
expected = ['foo', 'bar', 'baz']
@@ -88,9 +94,15 @@ class UtilsTests(TestCase):
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))