<s> from django . test import TestCase , Client <EOL> from django . http import HttpResponse <EOL> from django . core . urlresolvers import reverse <EOL> from django . conf . urls import patterns , url , include <EOL> from django . contrib . auth import get_user_model <EOL> from rest_framework import permissions <EOL> from rest_framework . views import APIView <EOL> from oauth2_provider . ext . rest_framework import OAuth2Authentication <EOL> UserModel = get_user_model ( ) <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> class OAuth2View ( MockView ) : <EOL> authentication_classes = [ OAuth2Authentication ] <EOL> urlpatterns = patterns ( <EOL> '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , include ( '<STR_LIT>' ) ) , <EOL> url ( r'<STR_LIT>' , OAuth2View . as_view ( ) ) , <EOL> url ( r'<STR_LIT>' , include ( '<STR_LIT>' ) ) , <EOL> ) <EOL> class TestPersonalAccessTokens ( TestCase ) : <EOL> urls = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> self . user_password = "<STR_LIT>" <EOL> self . user = UserModel . objects . create_user ( "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> self . user_password ) <EOL> self . client = Client ( ) <EOL> def tearDown ( self ) : <EOL> self . user . delete ( ) <EOL> def _create_authorization_header ( self , token ) : <EOL> return "<STR_LIT>" . format ( token ) <EOL> def test_authentication_empty ( self ) : <EOL> response = self . client . get ( "<STR_LIT>" ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_authentication_denied ( self ) : <EOL> auth = self . _create_authorization_header ( "<STR_LIT>" ) <EOL> response = self . client . get ( "<STR_LIT>" , HTTP_AUTHORIZATION = auth ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> def test_authentication_allow ( self ) : <EOL> self . client . login ( username = self . user , password = self . user_password ) <EOL> response = self . client . post ( reverse ( '<STR_LIT>' ) ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> access_token = self . user . accesstoken_set . first ( ) <EOL> auth = self . _create_authorization_header ( access_token ) <EOL> response = self . client . get ( "<STR_LIT>" , HTTP_AUTHORIZATION = auth ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) </s>
