<s> '''<STR_LIT>''' <EOL> from base import RauthTestCase <EOL> from test_service import HttpMixin , RequestMixin , ServiceMixin <EOL> from rauth . service import OAuth2Service <EOL> from rauth . session import OAUTH2_DEFAULT_TIMEOUT , OAuth2Session <EOL> from rauth . compat import parse_qsl , is_basestring <EOL> from copy import deepcopy <EOL> from mock import patch <EOL> import requests <EOL> import json <EOL> import pickle <EOL> class OAuth2ServiceTestCase ( RauthTestCase , RequestMixin , ServiceMixin , <EOL> HttpMixin ) : <EOL> client_id = '<STR_LIT>' <EOL> client_secret = '<STR_LIT>' <EOL> access_token = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> RauthTestCase . setUp ( self ) <EOL> self . access_token_url = '<STR_LIT>' <EOL> self . authorize_url = '<STR_LIT>' <EOL> self . base_url = '<STR_LIT>' <EOL> self . service = OAuth2Service ( self . client_id , <EOL> self . client_secret , <EOL> access_token_url = self . access_token_url , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> self . session = self . service . get_session ( self . access_token ) <EOL> self . session . request = self . fake_request <EOL> self . service . get_session = self . fake_get_session <EOL> @ patch . object ( requests . Session , '<STR_LIT>' ) <EOL> def fake_request ( self , <EOL> method , <EOL> url , <EOL> mock_request , <EOL> bearer_auth = False , <EOL> ** kwargs ) : <EOL> mock_request . return_value = self . response <EOL> url = self . session . _set_url ( url ) <EOL> service = OAuth2Service ( self . client_id , <EOL> self . client_secret , <EOL> access_token_url = self . access_token_url , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> session = service . get_session ( self . access_token ) <EOL> r = session . request ( method , <EOL> url , <EOL> bearer_auth = bearer_auth , <EOL> ** deepcopy ( kwargs ) ) <EOL> kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> if is_basestring ( kwargs . get ( '<STR_LIT>' , { } ) ) : <EOL> kwargs [ '<STR_LIT>' ] = dict ( parse_qsl ( kwargs [ '<STR_LIT>' ] ) ) <EOL> if bearer_auth and self . access_token is not None : <EOL> auth = mock_request . call_args [ <NUM_LIT:1> ] [ '<STR_LIT>' ] <EOL> self . assertEqual ( auth . access_token , self . access_token ) <EOL> kwargs [ '<STR_LIT>' ] = auth <EOL> else : <EOL> kwargs [ '<STR_LIT>' ] . update ( { '<STR_LIT>' : <EOL> self . access_token } ) <EOL> mock_request . assert_called_with ( method , <EOL> url , <EOL> timeout = OAUTH2_DEFAULT_TIMEOUT , <EOL> ** kwargs ) <EOL> return r <EOL> def fake_get_session ( self , token = None ) : <EOL> return self . session <EOL> def test_get_session ( self ) : <EOL> s = self . service . get_session ( ) <EOL> self . assertIsInstance ( s , OAuth2Session ) <EOL> def test_get_authorize_url ( self ) : <EOL> url = self . service . get_authorize_url ( ) <EOL> expected_fmt = '<STR_LIT>' <EOL> self . assertEqual ( url , expected_fmt . format ( self . service . client_id ) ) <EOL> def test_get_raw_access_token ( self ) : <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> r = self . service . get_raw_access_token ( ) <EOL> self . assertEqual ( r . content , resp ) <EOL> def test_get_raw_access_token_with_params ( self ) : <EOL> resp = '<STR_LIT>' <EOL> self . response . content = resp <EOL> r = self . service . get_raw_access_token ( params = { '<STR_LIT:a>' : '<STR_LIT:b>' } ) <EOL> self . assertEqual ( r . content , resp ) <EOL> def test_get_access_token ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> access_token = self . service . get_access_token ( ) <EOL> self . assertEqual ( access_token , '<STR_LIT>' ) <EOL> def test_get_access_token_with_json_decoder ( self ) : <EOL> self . response . content = json . dumps ( { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> access_token = self . service . get_access_token ( decoder = json . loads ) <EOL> self . assertEqual ( access_token , '<STR_LIT>' ) <EOL> def test_request_with_bearer_auth ( self ) : <EOL> r = self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> bearer_auth = True ) <EOL> self . assert_ok ( r ) <EOL> def test_get_auth_session ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> s = self . service . get_auth_session ( ) <EOL> self . assertIsInstance ( s , OAuth2Session ) <EOL> def test_get_auth_session_with_access_token_response ( self ) : <EOL> self . response . content = '<STR_LIT>' <EOL> s = self . service . get_auth_session ( ) <EOL> self . assertIsNotNone ( s . access_token_response ) <EOL> def test_pickle_session ( self ) : <EOL> session = pickle . loads ( pickle . dumps ( self . session ) ) <EOL> session . request = self . fake_request <EOL> r = session . request ( '<STR_LIT:GET>' , '<STR_LIT>' , bearer_auth = True ) <EOL> self . assert_ok ( r ) </s>
