<s> from django . test import TestCase <EOL> from django . test . client import RequestFactory <EOL> from django . contrib . auth . models import User <EOL> from doac . handlers . bearer import BearerHandler <EOL> from doac . models import AuthorizationToken , Client , Scope <EOL> from doac . utils import request_error_header <EOL> class TestBearerHandler ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . oclient = Client ( name = "<STR_LIT>" , access_host = "<STR_LIT>" ) <EOL> self . oclient . save ( ) <EOL> self . scope = Scope ( short_name = "<STR_LIT:test>" , full_name = "<STR_LIT>" , description = "<STR_LIT>" ) <EOL> self . scope . save ( ) <EOL> self . user = User ( username = "<STR_LIT>" , password = "<STR_LIT:test>" , email = "<STR_LIT>" ) <EOL> self . user . save ( ) <EOL> self . at = AuthorizationToken ( client = self . oclient , user = self . user ) <EOL> self . at . save ( ) <EOL> self . at . scope = [ self . scope ] <EOL> self . at . save ( ) <EOL> self . rt = self . at . generate_refresh_token ( ) <EOL> self . token = self . rt . generate_access_token ( ) <EOL> self . handler = BearerHandler ( ) <EOL> self . factory = RequestFactory ( ) <EOL> def test_access_token ( self ) : <EOL> request = self . factory . get ( "<STR_LIT:/>" ) <EOL> token = self . handler . access_token ( self . token . token , request ) <EOL> self . assertEqual ( token , self . token ) <EOL> token = self . handler . access_token ( "<STR_LIT>" , request ) <EOL> self . assertEqual ( token , None ) <EOL> def test_authenticate ( self ) : <EOL> request = self . factory . get ( "<STR_LIT:/>" ) <EOL> user = self . handler . authenticate ( self . token . token , request ) <EOL> self . assertEqual ( user , self . user ) <EOL> user = self . handler . authenticate ( "<STR_LIT>" , request ) <EOL> self . assertEqual ( user , None ) <EOL> def test_validate ( self ) : <EOL> from doac . exceptions . base import InvalidToken <EOL> from doac . exceptions . invalid_request import CredentialsNotProvided <EOL> request = self . factory . get ( "<STR_LIT:/>" ) <EOL> result = self . handler . validate ( self . token . token , request ) <EOL> self . assertEqual ( result , None ) <EOL> response = self . handler . validate ( "<STR_LIT>" , request ) <EOL> self . assertNotEqual ( response , None ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] , request_error_header ( InvalidToken ) ) <EOL> response = self . handler . validate ( "<STR_LIT>" , request ) <EOL> self . assertNotEqual ( response , None ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT> ) <EOL> self . assertEqual ( response [ "<STR_LIT>" ] , request_error_header ( CredentialsNotProvided ) ) </s>
