@@ -39,8 +39,8 @@ def setUp(self):
3939 self .scope_ad_manager = 'https://www.googleapis.com/auth/dfp'
4040
4141 def testGetAPIScope_adwords (self ):
42- self .assertEquals (googleads .oauth2 .GetAPIScope (self .api_name_adwords ),
43- self .scope_adwords )
42+ self .assertEqual (googleads .oauth2 .GetAPIScope (self .api_name_adwords ),
43+ self .scope_adwords )
4444
4545 def testGetAPIScope_badKey (self ):
4646 self .assertRaises (googleads .errors .GoogleAdsValueError ,
@@ -153,7 +153,7 @@ def testCreateHttpHeader_refresh(self):
153153 header = self .refresh_client .CreateHttpHeader ()
154154 self .assertEqual (mock_session_instance .proxies , {})
155155 self .assertEqual (mock_session_instance .verify , True )
156- self .assertEqual (mock_session_instance .cert , None )
156+ self .assertIsNone (mock_session_instance .cert )
157157 self .mock_req .assert_called_once_with (session = mock_session_instance )
158158 self .assertEqual (expected_header , header )
159159 self .mock_credentials_instance .refresh .assert_called_once_with (
@@ -195,6 +195,49 @@ def testCreateHttpHeader_refreshFails(self):
195195 self .assertFalse (self .mock_credentials_instance .apply .called )
196196
197197
198+ class GoogleCredentialsClientTest (unittest .TestCase ):
199+ """Tests for the googleads.oauth2.GoogleCredentialsClient class."""
200+
201+ def setUp (self ):
202+ self .access_token = 'a'
203+
204+ # Mock out google.auth.transport.Request for testing.
205+ self .mock_req = mock .Mock (spec = Request )
206+ self .mock_req .return_value = mock .Mock ()
207+ self .mock_req_instance = self .mock_req .return_value
208+
209+ # Mock out google.oauth2.credentials.Credentials for testing
210+ self .mock_credentials = mock .Mock (spec = Credentials )
211+ self .mock_credentials .expiry = None
212+
213+ def apply (headers , token = None ):
214+ headers ['authorization' ] = ('Bearer %s' % self .access_token )
215+
216+ self .mock_credentials .apply = mock .Mock (side_effect = apply )
217+
218+ self .client = googleads .oauth2 .GoogleCredentialsClient (
219+ self .mock_credentials )
220+
221+ def testRefresh (self ):
222+ with mock .patch ('google.auth.transport.requests.Request' , self .mock_req ):
223+ self .client .Refresh ()
224+ self .mock_req .assert_called_once ()
225+
226+ def testCreateHttpHeader_credentialsExpired (self ):
227+ self .mock_credentials .expiry = object ()
228+ self .mock_credentials .expired = True
229+ with mock .patch ('google.auth.transport.requests.Request' , self .mock_req ):
230+ self .client .CreateHttpHeader ()
231+ self .mock_req .assert_called_once ()
232+
233+ def testCreateHttpHeader_applyCredentialsToken (self ):
234+ expected_header = {u'authorization' : 'Bearer %s' % self .access_token }
235+
236+ with mock .patch ('google.auth.transport.requests.Request' , self .mock_req ):
237+ header = self .client .CreateHttpHeader ()
238+ self .assertEqual (header , expected_header )
239+
240+
198241class GoogleServiceAccountTest (unittest .TestCase ):
199242 """Tests for the googleads.oauth2.GoogleServiceAccountClient class."""
200243
@@ -240,7 +283,7 @@ def apply(headers, token=None):
240283 def refresh (request ):
241284 self .mock_credentials_instance .token = (
242285 self .access_token_unrefreshed if
243- self .mock_credentials_instance .token is 'x'
286+ self .mock_credentials_instance .token == 'x'
244287 else self .access_token_refreshed )
245288 self .mock_credentials_instance .token_expiry = datetime .datetime .utcnow ()
246289
@@ -283,7 +326,7 @@ def testCreateHttpHeader_refresh(self):
283326 header = self .sa_client .CreateHttpHeader ()
284327 self .assertEqual (mock_session_instance .proxies , {})
285328 self .assertEqual (mock_session_instance .verify , True )
286- self .assertEqual (mock_session_instance .cert , None )
329+ self .assertIsNone (mock_session_instance .cert )
287330 self .mock_req .assert_called_once_with (session = mock_session_instance )
288331 self .assertEqual (expected_header , header )
289332 self .mock_credentials_instance .refresh .assert_called_once_with (
0 commit comments