<s> '''<STR_LIT>''' <EOL> from base import RauthTestCase <EOL> from test_service import ( FakeHexdigest , HttpMixin , MutableDatetime , <EOL> RequestMixin , ServiceMixin ) <EOL> from rauth . compat import parse_qsl , urlsplit , is_basestring <EOL> from rauth . service import OflyService <EOL> from rauth . session import OFLY_DEFAULT_TIMEOUT , OflySession <EOL> from copy import deepcopy <EOL> from datetime import datetime <EOL> from functools import wraps <EOL> from mock import patch <EOL> import requests <EOL> import pickle <EOL> class OflyServiceTestCase ( RauthTestCase , RequestMixin , ServiceMixin , <EOL> HttpMixin ) : <EOL> app_id = '<STR_LIT>' <EOL> app_secret = '<STR_LIT>' <EOL> user_id = '<STR_LIT>' <EOL> def setUp ( self ) : <EOL> RauthTestCase . setUp ( self ) <EOL> self . authorize_url = '<STR_LIT>' <EOL> self . base_url = '<STR_LIT>' <EOL> self . service = OflyService ( self . app_id , <EOL> self . app_secret , <EOL> name = '<STR_LIT>' , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> self . session = self . service . get_session ( self . user_id ) <EOL> self . session . request = self . fake_request <EOL> self . service . get_session = self . fake_get_session <EOL> def fake_get_sorted_params ( self , params ) : <EOL> def sorting_gen ( ) : <EOL> for k in sorted ( params . keys ( ) ) : <EOL> yield '<STR_LIT:=>' . join ( ( k , params [ k ] ) ) <EOL> return '<STR_LIT:&>' . join ( sorting_gen ( ) ) <EOL> def fake_sign ( app_id , user_id ) : <EOL> def wrap ( func ) : <EOL> @ wraps ( func ) <EOL> @ patch ( '<STR_LIT>' , MutableDatetime ) <EOL> @ patch ( '<STR_LIT>' , FakeHexdigest ) <EOL> @ patch ( '<STR_LIT>' , FakeHexdigest ) <EOL> def decorated ( * args , ** kwargs ) : <EOL> hash_meth = kwargs . get ( '<STR_LIT>' , '<STR_LIT>' ) . upper ( ) <EOL> ofly_params = { '<STR_LIT>' : app_id , <EOL> '<STR_LIT>' : hash_meth , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:foo>' , <EOL> '<STR_LIT>' : user_id } <EOL> MutableDatetime . utcnow = classmethod ( lambda cls : datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> return func ( ofly_params = ofly_params , * args , ** kwargs ) <EOL> return decorated <EOL> return wrap <EOL> @ patch . object ( requests . Session , '<STR_LIT>' ) <EOL> @ fake_sign ( app_id , user_id ) <EOL> def fake_request ( self , <EOL> method , <EOL> url , <EOL> mock_request , <EOL> ofly_params , <EOL> user_id = None , <EOL> hash_meth = '<STR_LIT>' , <EOL> ** kwargs ) : <EOL> mock_request . return_value = self . response <EOL> user_id = user_id or self . service . user_id <EOL> service = OflyService ( self . app_id , <EOL> self . app_secret , <EOL> name = '<STR_LIT>' , <EOL> authorize_url = self . authorize_url , <EOL> base_url = self . base_url ) <EOL> session = service . get_session ( self . user_id ) <EOL> r = session . request ( method , <EOL> url , <EOL> user_id = user_id , <EOL> hash_meth = hash_meth , <EOL> ** deepcopy ( kwargs ) ) <EOL> url = self . session . _set_url ( url ) <EOL> kwargs . setdefault ( '<STR_LIT>' , { } ) <EOL> if is_basestring ( kwargs [ '<STR_LIT>' ] ) : <EOL> kwargs [ '<STR_LIT>' ] = dict ( parse_qsl ( kwargs [ '<STR_LIT>' ] ) ) <EOL> url_path = urlsplit ( url ) . path <EOL> signature_base_string = self . service . app_secret + url_path + '<STR_LIT:?>' <EOL> if len ( kwargs [ '<STR_LIT>' ] ) : <EOL> signature_base_string += self . fake_get_sorted_params ( kwargs [ '<STR_LIT>' ] ) + '<STR_LIT:&>' <EOL> signature_base_string += self . fake_get_sorted_params ( ofly_params ) <EOL> all_params = dict ( tuple ( ofly_params . items ( ) ) + <EOL> tuple ( kwargs [ '<STR_LIT>' ] . items ( ) ) ) <EOL> kwargs [ '<STR_LIT>' ] = self . fake_get_sorted_params ( all_params ) <EOL> if not isinstance ( kwargs [ '<STR_LIT>' ] , bytes ) : <EOL> kwargs [ '<STR_LIT>' ] = kwargs [ '<STR_LIT>' ] . encode ( '<STR_LIT:utf-8>' ) <EOL> mock_request . assert_called_with ( method , <EOL> url , <EOL> timeout = OFLY_DEFAULT_TIMEOUT , <EOL> ** kwargs ) <EOL> return r <EOL> def fake_get_session ( self , token ) : <EOL> return self . session <EOL> def test_get_session ( self ) : <EOL> s = self . service . get_session ( '<STR_LIT:foo>' ) <EOL> self . assertIsInstance ( s , OflySession ) <EOL> @ fake_sign ( app_id , user_id ) <EOL> def test_get_authorize_url ( self , ofly_params ) : <EOL> expected_url = '<STR_LIT>' <EOL> ofly_params . pop ( '<STR_LIT>' ) <EOL> params = self . fake_get_sorted_params ( ofly_params ) <EOL> url = self . service . get_authorize_url ( ) <EOL> self . assertEqual ( url , expected_url + params ) <EOL> def test_request_with_md5 ( self ) : <EOL> r = self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> user_id = self . user_id , <EOL> hash_meth = '<STR_LIT>' ) <EOL> self . assert_ok ( r ) <EOL> def test_request_with_bad_hash_meth ( self ) : <EOL> with self . assertRaises ( TypeError ) as e : <EOL> self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> user_id = self . user_id , <EOL> hash_meth = '<STR_LIT:foo>' ) <EOL> self . assertEqual ( str ( e . exception ) , <EOL> '<STR_LIT>' ) <EOL> def test_get_auth_session ( self ) : <EOL> s = self . service . get_auth_session ( '<STR_LIT:foo>' ) <EOL> self . assertIsInstance ( s , OflySession ) <EOL> def test_pickle_session ( self ) : <EOL> session = pickle . loads ( pickle . dumps ( self . session ) ) <EOL> session . request = self . fake_request <EOL> r = self . session . request ( '<STR_LIT:GET>' , <EOL> '<STR_LIT>' , <EOL> user_id = self . user_id , <EOL> hash_meth = '<STR_LIT>' ) <EOL> self . assert_ok ( r ) </s>
