Skip to content
This repository was archived by the owner on May 26, 2020. It is now read-only.

Commit fd0dbea

Browse files
author
marccerrato
committed
Added tests to validate JWT Auth compatibility with OAuth2
1 parent 9590190 commit fd0dbea

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

rest_framework_jwt/runtests/settings.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@
2323
'django.contrib.staticfiles',
2424
)
2525

26+
# OAuth2 is optional and won't work if there is no provider & oauth2
27+
try:
28+
import provider
29+
except ImportError:
30+
pass
31+
else:
32+
INSTALLED_APPS += (
33+
'provider',
34+
'provider.oauth2',
35+
)
36+
2637
MIDDLEWARE_CLASSES = (
2738
'django.contrib.sessions.middleware.SessionMiddleware',
2839
'django.middleware.common.CommonMiddleware',

rest_framework_jwt/tests/test_authentication.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from django.test import TestCase
2-
from django.http import HttpResponse
31
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+
46
from rest_framework import permissions, status
7+
from rest_framework.authentication import OAuth2Authentication
8+
from rest_framework.compat import oauth2_provider, oauth2_provider_models
59
from rest_framework.compat import patterns
610
from rest_framework.test import APIRequestFactory, APIClient
711
from rest_framework.views import APIView
812

9-
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
1013
from rest_framework_jwt import utils
14+
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
1115

1216

1317
factory = APIRequestFactory()
@@ -27,6 +31,10 @@ def post(self, request):
2731
'',
2832
(r'^jwt/$', MockView.as_view(
2933
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])),
3038
)
3139

3240

@@ -149,3 +157,43 @@ def test_post_invalid_token_failing_jwt_auth(self):
149157
self.assertEqual(response.data['detail'], msg)
150158
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
151159
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)

tox.ini

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[tox]
2+
envlist = py27
3+
4+
[testenv]
5+
deps = -rrequirements.txt
6+
django-oauth2-provider
7+
commands = {envpython} rest_framework_jwt/runtests/runtests.py
8+

0 commit comments

Comments
 (0)