<s> import json <EOL> import mock <EOL> import unittest <EOL> from rauth import OAuth1Session <EOL> from contextio . lib . api import Api <EOL> from contextio . lib . errors import RequestError <EOL> class TestApi ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" ) <EOL> def test_constructor_creates_object_with_default_config ( self ) : <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" ) <EOL> self . assertEqual ( "<STR_LIT:foo>" , self . api . consumer_key ) <EOL> self . assertEqual ( "<STR_LIT:bar>" , self . api . consumer_secret ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . url_base ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . api_version ) <EOL> self . assertEqual ( None , self . api . debug ) <EOL> self . assertIsInstance ( self . api . session , OAuth1Session ) <EOL> def test_constructor_creates_object_with_custom_config ( self ) : <EOL> self . api = Api ( <EOL> consumer_key = "<STR_LIT:foo>" , <EOL> consumer_secret = "<STR_LIT:bar>" , <EOL> url_base = "<STR_LIT>" , <EOL> debug = "<STR_LIT>" , <EOL> api_version = "<STR_LIT>" <EOL> ) <EOL> self . assertEqual ( "<STR_LIT:foo>" , self . api . consumer_key ) <EOL> self . assertEqual ( "<STR_LIT:bar>" , self . api . consumer_secret ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . url_base ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . api_version ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . debug ) <EOL> def test_constructor_maps_True_to_print_for_debug_value ( self ) : <EOL> self . api = Api ( <EOL> consumer_key = "<STR_LIT:foo>" , <EOL> consumer_secret = "<STR_LIT:bar>" , <EOL> debug = True <EOL> ) <EOL> self . assertEqual ( "<STR_LIT:foo>" , self . api . consumer_key ) <EOL> self . assertEqual ( "<STR_LIT:bar>" , self . api . consumer_secret ) <EOL> self . assertEqual ( "<STR_LIT>" , self . api . debug ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_debug_prints_message_when_debug_equals_print ( self , mock_six_print ) : <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" , debug = "<STR_LIT>" ) <EOL> mock_response = mock . Mock ( ) <EOL> mock_response . request . url = "<STR_LIT>" <EOL> mock_response . request . method = "<STR_LIT:GET>" <EOL> mock_response . status_code = <NUM_LIT> <EOL> message = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( <EOL> mock_response . request . url , mock_response . request . method , mock_response . status_code , <EOL> mock_response . request . __dict__ , mock_response . __dict__ ) <EOL> self . api . _debug ( mock_response ) <EOL> mock_six_print . assert_called_with ( message ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_debug_logs_message_when_debug_equals_log ( self , mock_logging_debug ) : <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" , debug = "<STR_LIT>" ) <EOL> mock_response = mock . Mock ( ) <EOL> mock_response . request . url = "<STR_LIT>" <EOL> mock_response . request . method = "<STR_LIT:GET>" <EOL> mock_response . status_code = <NUM_LIT> <EOL> message = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) . format ( <EOL> mock_response . request . url , mock_response . request . method , mock_response . status_code , <EOL> mock_response . request . __dict__ , mock_response . __dict__ ) <EOL> self . api . _debug ( mock_response ) <EOL> mock_logging_debug . assert_called_with ( message ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_defaults_to_GET_method_and_include_user_agent_header ( self , mock_session , mock_pkg_resources ) : <EOL> mock_package = mock . Mock ( ) <EOL> mock_package . version = "<STR_LIT>" <EOL> mock_pkg_resources . require . return_value = [ mock_package ] <EOL> mock_session . return_value . request = mock . Mock ( ) <EOL> mock_request = mock_session . return_value . request <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" ) <EOL> with self . assertRaises ( RequestError ) : <EOL> self . api . _request_uri ( "<STR_LIT>" ) <EOL> mock_request . assert_called_with ( <EOL> "<STR_LIT:GET>" , "<STR_LIT>" , <EOL> data = "<STR_LIT>" , <EOL> header_auth = True , <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } , <EOL> params = { } <EOL> ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_uri_raises_RequestError_if_status_not_between_200_and_300 ( self , mock_request ) : <EOL> mock_request . side_effect = RequestError <EOL> with self . assertRaises ( RequestError ) : <EOL> self . api . _request_uri ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_uri_returns_response_content_if_UnicodeDecodeError_raised ( self , mock_session ) : <EOL> mock_request = mock_session . return_value . request . return_value <EOL> mock_request . json . side_effect = UnicodeDecodeError ( "<STR_LIT>" , "<STR_LIT>" , <NUM_LIT> , <NUM_LIT> , "<STR_LIT>" ) <EOL> mock_request . status_code = <NUM_LIT:200> <EOL> mock_request . content = bytes ( "<STR_LIT>" ) <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" ) <EOL> response = self . api . _request_uri ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , response ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_uri_returns_response_text_if_ValueError_raised ( self , mock_session ) : <EOL> mock_request = mock_session . return_value . request . return_value <EOL> mock_request . status_code = <NUM_LIT:200> <EOL> mock_request . json . side_effect = ValueError ( ) <EOL> mock_request . text = "<STR_LIT>" <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" ) <EOL> response = self . api . _request_uri ( "<STR_LIT>" ) <EOL> self . assertEqual ( "<STR_LIT>" , response ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_uri_returns_json ( self , mock_request ) : <EOL> mock_request . return_value = ( { "<STR_LIT:foo>" : "<STR_LIT:bar>" } ) <EOL> response = self . api . _request_uri ( "<STR_LIT>" ) <EOL> self . assertEqual ( { "<STR_LIT:foo>" : "<STR_LIT:bar>" } , response ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> @ mock . patch ( "<STR_LIT>" ) <EOL> def test_request_includes_body_if_method_is_POST ( self , mock_session , mock_pkg_resources ) : <EOL> mock_package = mock . Mock ( ) <EOL> mock_package . version = "<STR_LIT>" <EOL> mock_pkg_resources . require . return_value = [ mock_package ] <EOL> mock_request = mock_session . return_value . request <EOL> mock_request . return_value . status_code = <NUM_LIT:200> <EOL> self . api = Api ( consumer_key = "<STR_LIT:foo>" , consumer_secret = "<STR_LIT:bar>" , api_version = "<STR_LIT>" ) <EOL> self . api . _request_uri ( "<STR_LIT>" , method = "<STR_LIT:POST>" , body = json . dumps ( { "<STR_LIT:foo>" : "<STR_LIT:bar>" } ) ) <EOL> mock_request . assert_called_with ( <EOL> "<STR_LIT:POST>" , "<STR_LIT>" , <EOL> data = { "<STR_LIT:body>" : '<STR_LIT>' } , <EOL> header_auth = True , <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) </s>
