|
1 | 1 | from __future__ import unicode_literals
|
2 | 2 |
|
3 | 3 | import json
|
| 4 | +import base64 |
| 5 | +try: |
| 6 | + import urllib.parse as urllib |
| 7 | +except ImportError: |
| 8 | + import urllib |
4 | 9 |
|
5 | 10 | from django.core.urlresolvers import reverse
|
6 | 11 | from django.test import TestCase, RequestFactory
|
@@ -58,7 +63,6 @@ def test_client_credential_access_allowed(self):
|
58 | 63 | 'grant_type': 'client_credentials',
|
59 | 64 | }
|
60 | 65 | auth_headers = self.get_basic_auth_header(self.application.client_id, self.application.client_secret)
|
61 |
| - |
62 | 66 | response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers)
|
63 | 67 | self.assertEqual(response.status_code, 200)
|
64 | 68 |
|
@@ -116,3 +120,44 @@ def get_scopes(self):
|
116 | 120 | self.assertEqual(r.user, self.dev_user)
|
117 | 121 | self.assertEqual(r.client, self.application)
|
118 | 122 | self.assertEqual(r.scopes, ['read', 'write'])
|
| 123 | + |
| 124 | + |
| 125 | +class TestClientResourcePasswordBased(BaseTest): |
| 126 | + def test_client_resource_password_based(self): |
| 127 | + """ |
| 128 | + Request an access token using Resource Owner Password Based flow |
| 129 | + """ |
| 130 | + |
| 131 | + self.application.delete() |
| 132 | + self.application = Application( |
| 133 | + name="test_client_credentials_app", |
| 134 | + user=self.dev_user, |
| 135 | + client_type=Application.CLIENT_CONFIDENTIAL, |
| 136 | + authorization_grant_type=Application.GRANT_PASSWORD, |
| 137 | + ) |
| 138 | + self.application.save() |
| 139 | + |
| 140 | + token_request_data = { |
| 141 | + 'grant_type': 'password', |
| 142 | + 'username': 'test_user', |
| 143 | + 'password': '123456' |
| 144 | + } |
| 145 | + auth_headers = self.get_basic_auth_header(urllib.quote_plus(self.application.client_id), urllib.quote_plus(self.application.client_secret)) |
| 146 | + response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers) |
| 147 | + self.assertEqual(response.status_code, 200) |
| 148 | + |
| 149 | + content = json.loads(response.content.decode("utf-8")) |
| 150 | + access_token = content['access_token'] |
| 151 | + |
| 152 | + # use token to access the resource |
| 153 | + auth_headers = { |
| 154 | + 'HTTP_AUTHORIZATION': 'Bearer ' + access_token, |
| 155 | + } |
| 156 | + request = self.factory.get("/fake-resource", **auth_headers) |
| 157 | + request.user = self.test_user |
| 158 | + |
| 159 | + view = ResourceView.as_view() |
| 160 | + response = view(request) |
| 161 | + self.assertEqual(response, "This is a protected resource") |
| 162 | + |
| 163 | + |
0 commit comments