|
7 | 7 | from django.core.urlresolvers import reverse
|
8 | 8 |
|
9 | 9 | 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 |
11 | 11 | from ..models import get_application_model, Grant, AccessToken
|
12 | 12 | from ..settings import oauth2_settings
|
13 | 13 | from ..views import ScopedProtectedResourceView, ReadWriteScopedResourceView
|
@@ -64,6 +64,60 @@ def tearDown(self):
|
64 | 64 | self.dev_user.delete()
|
65 | 65 |
|
66 | 66 |
|
| 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 | + |
67 | 121 | class TestScopesSave(BaseTest):
|
68 | 122 | def test_scopes_saved_in_grant(self):
|
69 | 123 | """
|
|
0 commit comments