1
- from django .test import TestCase
2
- from django .http import HttpResponse
3
1
from django .contrib .auth .models import User
2
+ from django .http import HttpResponse
3
+ from django .test import TestCase
4
+ from django .utils import unittest
5
+
4
6
from rest_framework import permissions , status
7
+ from rest_framework .authentication import OAuth2Authentication
8
+ from rest_framework .compat import oauth2_provider , oauth2_provider_models
5
9
from rest_framework .compat import patterns
6
10
from rest_framework .test import APIRequestFactory , APIClient
7
11
from rest_framework .views import APIView
8
12
9
- from rest_framework_jwt .authentication import JSONWebTokenAuthentication
10
13
from rest_framework_jwt import utils
14
+ from rest_framework_jwt .authentication import JSONWebTokenAuthentication
11
15
12
16
13
17
factory = APIRequestFactory ()
@@ -27,6 +31,10 @@ def post(self, request):
27
31
'' ,
28
32
(r'^jwt/$' , MockView .as_view (
29
33
authentication_classes = [JSONWebTokenAuthentication ])),
34
+ (r'^jwt-oauth2/$' , MockView .as_view (
35
+ authentication_classes = [JSONWebTokenAuthentication , OAuth2Authentication ])),
36
+ (r'^oauth2-jwt/$' , MockView .as_view (
37
+ authentication_classes = [OAuth2Authentication , JSONWebTokenAuthentication ])),
30
38
)
31
39
32
40
@@ -149,3 +157,43 @@ def test_post_invalid_token_failing_jwt_auth(self):
149
157
self .assertEqual (response .data ['detail' ], msg )
150
158
self .assertEqual (response .status_code , status .HTTP_401_UNAUTHORIZED )
151
159
self .assertEqual (response ['WWW-Authenticate' ], 'Bearer realm="api"' )
160
+
161
+ @unittest .skipUnless (oauth2_provider , 'django-oauth2-provider not installed' )
162
+ def test_post_passing_jwt_auth_with_oauth2_priority (self ):
163
+ """
164
+ Ensure POSTing over JWT auth with correct credentials
165
+ passes and does not require CSRF when OAuth2Authentication
166
+ has priority on authentication_classes
167
+ """
168
+ payload = utils .jwt_payload_handler (self .user )
169
+ token = utils .jwt_encode_handler (payload )
170
+
171
+ auth = 'Bearer {0}' .format (token )
172
+ response = self .csrf_client .post (
173
+ '/oauth2-jwt/' , {'example' : 'example' },
174
+ HTTP_AUTHORIZATION = auth , format = 'json' )
175
+
176
+ self .assertEqual (response .status_code , status .HTTP_200_OK , response )
177
+
178
+ @unittest .skipUnless (oauth2_provider , 'django-oauth2-provider not installed' )
179
+ def test_post_passing_oauth2_with_jwt_auth_priority (self ):
180
+ """
181
+ Ensure POSTing over OAuth2 with correct credentials
182
+ passes and does not require CSRF when JSONWebTokenAuthentication
183
+ has priority on authentication_classes
184
+ """
185
+ oauth2_client = oauth2_provider_models .Client .objects .create (
186
+ user = self .user ,
187
+ client_type = 0 ,
188
+ )
189
+ access_token = oauth2_provider_models .AccessToken .objects .create (
190
+ user = self .user ,
191
+ client = oauth2_client ,
192
+ )
193
+
194
+ auth = 'Bearer {0}' .format (access_token .token )
195
+ response = self .csrf_client .post (
196
+ '/jwt-oauth2/' , {'example' : 'example' },
197
+ HTTP_AUTHORIZATION = auth , format = 'json' )
198
+
199
+ self .assertEqual (response .status_code , status .HTTP_200_OK , response )
0 commit comments