Skip to content

Commit 52a45af

Browse files
committed
Added tests for scopes backwards compatibility.
1 parent 334f54f commit 52a45af

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

oauth2_provider/tests/test_scopes.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django.core.urlresolvers import reverse
88

99
from .test_utils import TestCaseUtils
10-
from ..compat import urlparse, parse_qs, get_user_model
10+
from ..compat import urlparse, parse_qs, get_user_model, urlencode
1111
from ..models import get_application_model, Grant, AccessToken
1212
from ..settings import oauth2_settings
1313
from ..views import ScopedProtectedResourceView, ReadWriteScopedResourceView
@@ -64,6 +64,60 @@ def tearDown(self):
6464
self.dev_user.delete()
6565

6666

67+
class TestScopesQueryParameterBackwardsCompatibility(BaseTest):
68+
def setUp(self):
69+
super(TestScopesQueryParameterBackwardsCompatibility, self).setUp()
70+
oauth2_settings._SCOPES = ['read', 'write']
71+
72+
def test_scopes_query_parameter_is_supported_on_post(self):
73+
"""
74+
Tests support for plural `scopes` query parameter on POST requests.
75+
76+
"""
77+
self.client.login(username="test_user", password="123456")
78+
79+
# retrieve a valid authorization code
80+
authcode_data = {
81+
'client_id': self.application.client_id,
82+
'state': 'random_state_string',
83+
'scopes': 'read write', # using plural `scopes`
84+
'redirect_uri': 'http://example.it',
85+
'response_type': 'code',
86+
'allow': True,
87+
}
88+
response = self.client.post(reverse('oauth2_provider:authorize'), data=authcode_data)
89+
query_dict = parse_qs(urlparse(response['Location']).query)
90+
authorization_code = query_dict['code'].pop()
91+
92+
grant = Grant.objects.get(code=authorization_code)
93+
self.assertEqual(grant.scope, "read write")
94+
95+
def test_scopes_query_parameter_is_supported_on_get(self):
96+
"""
97+
Tests support for plural `scopes` query parameter on GET requests.
98+
99+
"""
100+
self.client.login(username="test_user", password="123456")
101+
102+
query_string = urlencode({
103+
'client_id': self.application.client_id,
104+
'state': 'random_state_string',
105+
'scopes': 'read write', # using plural `scopes`
106+
'redirect_uri': 'http://example.it',
107+
'response_type': 'code',
108+
})
109+
url = "{url}?{qs}".format(url=reverse('oauth2_provider:authorize'), qs=query_string)
110+
111+
response = self.client.get(url)
112+
self.assertEqual(response.status_code, 200)
113+
114+
# check form is in context
115+
self.assertIn("form", response.context)
116+
117+
form = response.context["form"]
118+
self.assertEqual(form['scope'].value(), "read write")
119+
120+
67121
class TestScopesSave(BaseTest):
68122
def test_scopes_saved_in_grant(self):
69123
"""

0 commit comments

Comments
 (0)