|
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
|
@@ -116,3 +121,44 @@ def get_scopes(self):
|
116 | 121 | self.assertEqual(r.user, self.dev_user)
|
117 | 122 | self.assertEqual(r.client, self.application)
|
118 | 123 | self.assertEqual(r.scopes, ['read', 'write'])
|
| 124 | + |
| 125 | + |
| 126 | +class TestClientResourcePasswordBased(BaseTest): |
| 127 | + def test_client_resource_password_based(self): |
| 128 | + """ |
| 129 | + Request an access token using Resource Owner Password Based flow |
| 130 | + """ |
| 131 | + |
| 132 | + self.application.delete() |
| 133 | + self.application = Application( |
| 134 | + name="test_client_credentials_app", |
| 135 | + user=self.dev_user, |
| 136 | + client_type=Application.CLIENT_CONFIDENTIAL, |
| 137 | + authorization_grant_type=Application.GRANT_PASSWORD, |
| 138 | + ) |
| 139 | + self.application.save() |
| 140 | + |
| 141 | + token_request_data = { |
| 142 | + 'grant_type': 'password', |
| 143 | + 'username': 'test_user', |
| 144 | + 'password': '123456' |
| 145 | + } |
| 146 | + auth_headers = self.get_basic_auth_header(urllib.quote_plus(self.application.client_id), urllib.quote_plus(self.application.client_secret)) |
| 147 | + response = self.client.post(reverse('oauth2_provider:token'), data=token_request_data, **auth_headers) |
| 148 | + self.assertEqual(response.status_code, 200) |
| 149 | + |
| 150 | + content = json.loads(response.content.decode("utf-8")) |
| 151 | + access_token = content['access_token'] |
| 152 | + |
| 153 | + # use token to access the resource |
| 154 | + auth_headers = { |
| 155 | + 'HTTP_AUTHORIZATION': 'Bearer ' + access_token, |
| 156 | + } |
| 157 | + request = self.factory.get("/fake-resource", **auth_headers) |
| 158 | + request.user = self.test_user |
| 159 | + |
| 160 | + view = ResourceView.as_view() |
| 161 | + response = view(request) |
| 162 | + self.assertEqual(response, "This is a protected resource") |
| 163 | + |
| 164 | + |
0 commit comments