<s> """<STR_LIT>""" <EOL> import datetime <EOL> import unittest <EOL> import httplib2 <EOL> import mock <EOL> import socks <EOL> import googleads . errors <EOL> import googleads . oauth2 <EOL> import fake_filesystem <EOL> import fake_tempfile <EOL> from oauth2client . client import AccessTokenRefreshError <EOL> from oauth2client . client import OAuth2Credentials <EOL> from oauth2client . client import SignedJwtAssertionCredentials <EOL> class GetAPIScopeTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . api_name_adwords = '<STR_LIT>' <EOL> self . api_name_dfp = '<STR_LIT>' <EOL> self . scope_adwords = '<STR_LIT>' <EOL> self . scope_dfp = '<STR_LIT>' <EOL> def testGetAPIScope_adwords ( self ) : <EOL> self . assertEquals ( googleads . oauth2 . GetAPIScope ( self . api_name_adwords ) , <EOL> self . scope_adwords ) <EOL> def testGetAPIScope_badKey ( self ) : <EOL> self . assertRaises ( googleads . errors . GoogleAdsValueError , <EOL> googleads . oauth2 . GetAPIScope , '<STR_LIT>' ) <EOL> def testGetAPIScope_dfp ( self ) : <EOL> self . assertEquals ( googleads . oauth2 . GetAPIScope ( self . api_name_dfp ) , <EOL> self . scope_dfp ) <EOL> class GoogleOAuth2ClientTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def testCreateHttpHeader ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> NotImplementedError , <EOL> googleads . oauth2 . GoogleOAuth2Client ( ) . CreateHttpHeader ) <EOL> def testRefresh ( self ) : <EOL> """<STR_LIT>""" <EOL> self . assertRaises ( <EOL> NotImplementedError , <EOL> googleads . oauth2 . GoogleOAuth2Client ( ) . Refresh ) <EOL> class GoogleRefreshTokenClientTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . client_id = '<STR_LIT>' <EOL> self . client_secret = '<STR_LIT>' <EOL> self . refresh_token = '<STR_LIT>' <EOL> self . proxy_info = httplib2 . ProxyInfo ( socks . PROXY_TYPE_HTTP , '<STR_LIT>' , <EOL> <NUM_LIT> ) <EOL> self . https_proxy = '<STR_LIT>' % ( self . proxy_info . proxy_host , <EOL> self . proxy_info . proxy_port ) <EOL> self . access_token_unrefreshed = '<STR_LIT:a>' <EOL> self . access_token_refreshed = '<STR_LIT:b>' <EOL> self . http = mock . Mock ( spec = httplib2 . Http ) <EOL> self . opener = self . http . return_value = mock . Mock ( ) <EOL> self . opener . proxy_info = self . proxy_info <EOL> self . opener . ca_certs = None <EOL> self . opener . disable_ssl_certificate_valiation = True <EOL> self . oauth2_credentials = mock . Mock ( spec = OAuth2Credentials ) <EOL> self . mock_oauth2_credentials = self . oauth2_credentials . return_value = ( <EOL> mock . Mock ( ) ) <EOL> self . mock_oauth2_credentials . access_token = self . access_token_unrefreshed <EOL> self . mock_oauth2_credentials . token_expiry = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:12> ) <EOL> def apply ( headers ) : <EOL> headers [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> % self . mock_oauth2_credentials . access_token ) <EOL> def refresh ( mock_http ) : <EOL> self . mock_oauth2_credentials . access_token = self . access_token_refreshed <EOL> self . mock_oauth2_credentials . token_expiry = datetime . datetime . utcnow ( ) <EOL> self . mock_oauth2_credentials . apply = mock . Mock ( side_effect = apply ) <EOL> self . mock_oauth2_credentials . refresh = mock . Mock ( side_effect = refresh ) <EOL> with mock . patch ( '<STR_LIT>' , <EOL> self . oauth2_credentials ) : <EOL> self . googleads_client = googleads . oauth2 . GoogleRefreshTokenClient ( <EOL> self . client_id , self . client_secret , self . refresh_token , <EOL> self . proxy_info ) <EOL> def testCreateHttpHeader_noRefresh ( self ) : <EOL> header = { '<STR_LIT>' : '<STR_LIT>' % self . access_token_unrefreshed } <EOL> self . mock_oauth2_credentials . token_expiry = None <EOL> self . assertEqual ( header , self . googleads_client . CreateHttpHeader ( ) ) <EOL> def testCreateHttpHeader_refresh ( self ) : <EOL> header = { u'<STR_LIT>' : '<STR_LIT>' % self . access_token_refreshed } <EOL> with mock . patch ( '<STR_LIT>' , self . http ) : <EOL> self . assertEqual ( header , self . googleads_client . CreateHttpHeader ( ) ) <EOL> self . http . assert_called_once_with ( <EOL> ca_certs = None , proxy_info = self . proxy_info , <EOL> disable_ssl_certificate_validation = False ) <EOL> self . mock_oauth2_credentials . refresh . assert_called_once_with ( <EOL> self . opener ) <EOL> def testCreateHttpHeader_refreshFails ( self ) : <EOL> self . mock_oauth2_credentials . refresh . side_effect = AccessTokenRefreshError ( <EOL> '<STR_LIT>' ) <EOL> with mock . patch ( '<STR_LIT>' , self . http ) : <EOL> self . assertRaises ( AccessTokenRefreshError , <EOL> self . googleads_client . CreateHttpHeader ) <EOL> self . assertFalse ( self . mock_oauth2_credentials . apply . called ) <EOL> class GoogleServiceAccountTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . scope = '<STR_LIT>' <EOL> self . service_account_email = '<STR_LIT>' <EOL> self . private_key_password = '<STR_LIT>' <EOL> self . proxy_info = httplib2 . ProxyInfo ( socks . PROXY_TYPE_HTTP , '<STR_LIT>' , <EOL> <NUM_LIT> ) <EOL> self . https_proxy = '<STR_LIT>' % ( self . proxy_info . proxy_host , <EOL> self . proxy_info . proxy_port ) <EOL> self . access_token_unrefreshed = '<STR_LIT:a>' <EOL> self . access_token_refreshed = '<STR_LIT:b>' <EOL> filesystem = fake_filesystem . FakeFilesystem ( ) <EOL> tempfile = fake_tempfile . FakeTempfileModule ( filesystem ) <EOL> fake_open = fake_filesystem . FakeFileOpen ( filesystem ) <EOL> key_file_path = tempfile . NamedTemporaryFile ( delete = False ) . name <EOL> with fake_open ( key_file_path , '<STR_LIT:w>' ) as file_handle : <EOL> file_handle . write ( '<STR_LIT>' ) <EOL> self . http = mock . Mock ( spec = httplib2 . Http ) <EOL> self . opener = self . http . return_value = mock . Mock ( ) <EOL> self . opener . proxy_info = self . proxy_info <EOL> self . opener . ca_certs = None <EOL> self . opener . disable_ssl_certificate_valiation = True <EOL> self . oauth2_credentials = mock . Mock ( spec = SignedJwtAssertionCredentials ) <EOL> self . mock_oauth2_credentials = self . oauth2_credentials . return_value = ( <EOL> mock . Mock ( ) ) <EOL> self . mock_oauth2_credentials . access_token = '<STR_LIT:x>' <EOL> self . mock_oauth2_credentials . token_expiry = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , <EOL> <NUM_LIT:12> ) <EOL> def apply ( headers ) : <EOL> headers [ '<STR_LIT>' ] = ( '<STR_LIT>' <EOL> % self . mock_oauth2_credentials . access_token ) <EOL> def refresh ( mock_http ) : <EOL> self . mock_oauth2_credentials . access_token = ( <EOL> self . access_token_unrefreshed if <EOL> self . mock_oauth2_credentials . access_token is '<STR_LIT:x>' <EOL> else self . access_token_refreshed ) <EOL> self . mock_oauth2_credentials . token_expiry = datetime . datetime . utcnow ( ) <EOL> self . mock_oauth2_credentials . apply = mock . Mock ( side_effect = apply ) <EOL> self . mock_oauth2_credentials . refresh = mock . Mock ( side_effect = refresh ) <EOL> with mock . patch ( '<STR_LIT>' , fake_open ) : <EOL> with mock . patch ( '<STR_LIT>' , <EOL> self . oauth2_credentials ) : <EOL> self . googleads_client = googleads . oauth2 . GoogleServiceAccountClient ( <EOL> self . scope , self . service_account_email , key_file_path , <EOL> self . private_key_password , proxy_info = self . proxy_info ) <EOL> self . mock_oauth2_credentials . refresh . reset_mock ( ) <EOL> def testCreateHttpHeader_noRefresh ( self ) : <EOL> header = { '<STR_LIT>' : '<STR_LIT>' % self . access_token_unrefreshed } <EOL> self . mock_oauth2_credentials . token_expiry = None <EOL> self . assertEqual ( header , self . googleads_client . CreateHttpHeader ( ) ) <EOL> def testCreateHttpHeader_refresh ( self ) : <EOL> header = { u'<STR_LIT>' : '<STR_LIT>' % self . access_token_refreshed } <EOL> with mock . patch ( '<STR_LIT>' , self . http ) : <EOL> self . assertEqual ( header , self . googleads_client . CreateHttpHeader ( ) ) <EOL> self . http . assert_called_once_with ( <EOL> ca_certs = None , proxy_info = self . proxy_info , <EOL> disable_ssl_certificate_validation = False ) <EOL> self . mock_oauth2_credentials . refresh . assert_called_once_with ( <EOL> self . opener ) <EOL> def testCreateHttpHeader_refreshFails ( self ) : <EOL> self . mock_oauth2_credentials . refresh . side_effect = AccessTokenRefreshError ( <EOL> '<STR_LIT>' ) <EOL> with mock . patch ( '<STR_LIT>' , self . http ) : <EOL> self . assertRaises ( AccessTokenRefreshError , <EOL> self . googleads_client . CreateHttpHeader ) <EOL> self . assertFalse ( self . mock_oauth2_credentials . apply . called ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
