<s> import unittest <EOL> from django . http import HttpResponse <EOL> from django . test import TestCase <EOL> from django . conf . urls import patterns <EOL> from rest_framework import permissions , status <EOL> try : <EOL> from rest_framework_oauth . authentication import OAuth2Authentication <EOL> except ImportError : <EOL> try : <EOL> from rest_framework . authentication import OAuth2Authentication <EOL> except ImportError : <EOL> OAuth2Authentication = None <EOL> try : <EOL> try : <EOL> from rest_framework_oauth . compat import oauth2_provider <EOL> from rest_framework_oauth . compat . oauth2_provider import oauth2 <EOL> except ImportError : <EOL> oauth2_provider = None <EOL> except ImportError : <EOL> try : <EOL> from rest_framework . compat import oauth2_provider <EOL> from rest_framework . compat . oauth2_provider import oauth2 <EOL> except ImportError : <EOL> oauth2_provider = None <EOL> from rest_framework . test import APIRequestFactory , APIClient <EOL> from rest_framework . views import APIView <EOL> from rest_framework_jwt import utils <EOL> from rest_framework_jwt . compat import get_user_model <EOL> from rest_framework_jwt . settings import api_settings , DEFAULTS <EOL> from rest_framework_jwt . authentication import JSONWebTokenAuthentication <EOL> User = get_user_model ( ) <EOL> DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED = '<STR_LIT>' <EOL> factory = APIRequestFactory ( ) <EOL> class MockView ( APIView ) : <EOL> permission_classes = ( permissions . IsAuthenticated , ) <EOL> def get ( self , request ) : <EOL> return HttpResponse ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> , '<STR_LIT:c>' : <NUM_LIT:3> } ) <EOL> def post ( self , request ) : <EOL> return HttpResponse ( { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> , '<STR_LIT:c>' : <NUM_LIT:3> } ) <EOL> urlpatterns = patterns ( <EOL> '<STR_LIT>' , <EOL> ( r'<STR_LIT>' , MockView . as_view ( <EOL> authentication_classes = [ JSONWebTokenAuthentication ] ) ) , <EOL> ( r'<STR_LIT>' , MockView . as_view ( <EOL> authentication_classes = [ <EOL> JSONWebTokenAuthentication , OAuth2Authentication ] ) ) , <EOL> ( r'<STR_LIT>' , MockView . as_view ( <EOL> authentication_classes = [ <EOL> OAuth2Authentication , JSONWebTokenAuthentication ] ) ) , <EOL> ) <EOL> class JSONWebTokenAuthenticationTests ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . csrf_client = APIClient ( enforce_csrf_checks = True ) <EOL> self . username = '<STR_LIT>' <EOL> self . email = '<STR_LIT>' <EOL> self . user = User . objects . create_user ( self . username , self . email ) <EOL> def test_post_form_passing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = utils . jwt_payload_handler ( self . user ) <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , HTTP_AUTHORIZATION = auth ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_post_json_passing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = utils . jwt_payload_handler ( self . user ) <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> def test_post_form_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . csrf_client . post ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_json_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> response = self . csrf_client . post ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_no_jwt_header_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> auth = '<STR_LIT>' <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , msg ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_invalid_jwt_header_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> auth = '<STR_LIT>' <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , msg ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_expired_token_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = utils . jwt_payload_handler ( self . user ) <EOL> payload [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , msg ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> def test_post_invalid_token_failing_jwt_auth ( self ) : <EOL> """<STR_LIT>""" <EOL> auth = '<STR_LIT>' <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , msg ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> @ unittest . skipUnless ( oauth2_provider , DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED ) <EOL> def test_post_passing_jwt_auth_with_oauth2_priority ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = utils . jwt_payload_handler ( self . user ) <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK , response ) <EOL> @ unittest . skipUnless ( oauth2_provider , DJANGO_OAUTH2_PROVIDER_NOT_INSTALLED ) <EOL> def test_post_passing_oauth2_with_jwt_auth_priority ( self ) : <EOL> """<STR_LIT>""" <EOL> Client = oauth2_provider . oauth2 . models . Client <EOL> AccessToken = oauth2_provider . oauth2 . models . AccessToken <EOL> oauth2_client = Client . objects . create ( <EOL> user = self . user , <EOL> client_type = <NUM_LIT:0> , <EOL> ) <EOL> access_token = AccessToken . objects . create ( <EOL> user = self . user , <EOL> client = oauth2_client , <EOL> ) <EOL> auth = '<STR_LIT>' . format ( access_token . token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK , response ) <EOL> def test_post_form_passing_jwt_invalid_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = dict ( email = None ) <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , HTTP_AUTHORIZATION = auth ) <EOL> msg = '<STR_LIT>' <EOL> self . assertEqual ( response . data [ '<STR_LIT>' ] , msg ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> def test_different_auth_header_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> api_settings . JWT_AUTH_HEADER_PREFIX = '<STR_LIT>' <EOL> payload = utils . jwt_payload_handler ( self . user ) <EOL> token = utils . jwt_encode_handler ( payload ) <EOL> auth = '<STR_LIT>' . format ( token ) <EOL> response = self . csrf_client . post ( <EOL> '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } , <EOL> HTTP_AUTHORIZATION = auth , format = '<STR_LIT>' ) <EOL> self . assertEqual ( response . status_code , status . HTTP_200_OK ) <EOL> api_settings . JWT_AUTH_HEADER_PREFIX = DEFAULTS [ '<STR_LIT>' ] <EOL> def test_post_form_failing_jwt_auth_different_auth_header_prefix ( self ) : <EOL> """<STR_LIT>""" <EOL> api_settings . JWT_AUTH_HEADER_PREFIX = '<STR_LIT>' <EOL> response = self . csrf_client . post ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( response . status_code , status . HTTP_401_UNAUTHORIZED ) <EOL> self . assertEqual ( response [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> api_settings . JWT_AUTH_HEADER_PREFIX = DEFAULTS [ '<STR_LIT>' ] </s>
