|
| 1 | +from oauth2_provider.compat import parse_qs, urlparse |
| 2 | +from django.core.urlresolvers import reverse |
| 3 | + |
| 4 | +from apps.test import BaseApiTest |
| 5 | +from ..models import Application |
| 6 | + |
| 7 | + |
| 8 | +class TestAuthorizeWithCustomScheme(BaseApiTest): |
| 9 | + def test_post_with_valid_non_standard_scheme(self): |
| 10 | + redirect_uri = 'com.custom.bluebutton://example.it' |
| 11 | + # create a user |
| 12 | + self._create_user('anna', '123456') |
| 13 | + capability_a = self._create_capability('Capability A', []) |
| 14 | + capability_b = self._create_capability('Capability B', []) |
| 15 | + # create an application and add capabilities |
| 16 | + application = self._create_application( |
| 17 | + 'an app', |
| 18 | + grant_type=Application.GRANT_AUTHORIZATION_CODE, |
| 19 | + redirect_uris=redirect_uri) |
| 20 | + application.scope.add(capability_a, capability_b) |
| 21 | + # user logs in |
| 22 | + self.client.login(username='anna', password='123456') |
| 23 | + # post the authorization form with only one scope selected |
| 24 | + payload = { |
| 25 | + 'client_id': application.client_id, |
| 26 | + 'response_type': 'code', |
| 27 | + 'redirect_uri': redirect_uri, |
| 28 | + 'scope': ['capability-a'], |
| 29 | + 'expires_in': 86400, |
| 30 | + 'allow': True, |
| 31 | + } |
| 32 | + response = self.client.post(reverse('oauth2_provider:authorize'), data=payload) |
| 33 | + |
| 34 | + self.assertEqual(response.status_code, 302) |
| 35 | + # now extract the authorization code and use it to request an access_token |
| 36 | + query_dict = parse_qs(urlparse(response['Location']).query) |
| 37 | + authorization_code = query_dict.pop('code') |
| 38 | + token_request_data = { |
| 39 | + 'grant_type': 'authorization_code', |
| 40 | + 'code': authorization_code, |
| 41 | + 'redirect_uri': redirect_uri, |
| 42 | + 'client_id': application.client_id, |
| 43 | + } |
| 44 | + response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data) |
| 45 | + self.assertEqual(response.status_code, 200) |
| 46 | + |
| 47 | + def test_post_with_invalid_non_standard_scheme(self): |
| 48 | + redirect_uri = 'com.custom.bluebutton://example.it' |
| 49 | + bad_redirect_uri = 'com.custom.bad://example.it' |
| 50 | + # create a user |
| 51 | + self._create_user('anna', '123456') |
| 52 | + capability_a = self._create_capability('Capability A', []) |
| 53 | + capability_b = self._create_capability('Capability B', []) |
| 54 | + # create an application and add capabilities |
| 55 | + application = self._create_application( |
| 56 | + 'an app', |
| 57 | + grant_type=Application.GRANT_AUTHORIZATION_CODE, |
| 58 | + redirect_uris=redirect_uri) |
| 59 | + application.scope.add(capability_a, capability_b) |
| 60 | + # user logs in |
| 61 | + self.client.login(username='anna', password='123456') |
| 62 | + # post the authorization form with only one scope selected |
| 63 | + payload = { |
| 64 | + 'client_id': application.client_id, |
| 65 | + 'response_type': 'code', |
| 66 | + 'redirect_uri': bad_redirect_uri, |
| 67 | + 'scope': ['capability-a'], |
| 68 | + 'expires_in': 86400, |
| 69 | + 'allow': True, |
| 70 | + } |
| 71 | + response = self.client.post(reverse('oauth2_provider:authorize'), data=payload) |
| 72 | + |
| 73 | + self.assertEqual(response.status_code, 400) |
0 commit comments