@@ -90,47 +90,31 @@ def _callFUT(self, client_email, private_key_path, scope=None):
9090 def test_it (self ):
9191 from gcloud import credentials as MUT
9292 from gcloud ._testing import _Monkey
93- from gcloud ._testing import _NamedTemporaryFile
9493
9594 CLIENT_EMAIL = 'phred@example.com'
96- PRIVATE_KEY = b'SEEkR1t'
97- client = _Client ()
98- with _Monkey (MUT , client = client ):
99- with _NamedTemporaryFile () as temp :
100- with open (temp .name , 'wb' ) as file_obj :
101- file_obj .write (PRIVATE_KEY )
102- found = self ._callFUT (CLIENT_EMAIL , temp .name )
95+ MOCK_FILENAME = 'foo.path'
96+ MOCK_CRED_CLASS = _MockServiceAccountCredentials ()
97+ with _Monkey (MUT , ServiceAccountCredentials = MOCK_CRED_CLASS ):
98+ found = self ._callFUT (CLIENT_EMAIL , MOCK_FILENAME )
10399
104- self .assertTrue (found is client ._signed )
105- expected_called_with = {
106- 'service_account_name' : CLIENT_EMAIL ,
107- 'private_key' : PRIVATE_KEY ,
108- 'scope' : None ,
109- }
110- self .assertEqual (client ._called_with , expected_called_with )
100+ self .assertTrue (found is MOCK_CRED_CLASS ._result )
101+ self .assertEqual (MOCK_CRED_CLASS .p12_called ,
102+ [(CLIENT_EMAIL , MOCK_FILENAME , None )])
111103
112104 def test_it_with_scope (self ):
113105 from gcloud import credentials as MUT
114106 from gcloud ._testing import _Monkey
115- from gcloud ._testing import _NamedTemporaryFile
116107
117108 CLIENT_EMAIL = 'phred@example.com'
118- PRIVATE_KEY = b'SEEkR1t'
119109 SCOPE = 'SCOPE'
120- client = _Client ()
121- with _Monkey (MUT , client = client ):
122- with _NamedTemporaryFile () as temp :
123- with open (temp .name , 'wb' ) as file_obj :
124- file_obj .write (PRIVATE_KEY )
125- found = self ._callFUT (CLIENT_EMAIL , temp .name , SCOPE )
110+ MOCK_FILENAME = 'foo.path'
111+ MOCK_CRED_CLASS = _MockServiceAccountCredentials ()
112+ with _Monkey (MUT , ServiceAccountCredentials = MOCK_CRED_CLASS ):
113+ found = self ._callFUT (CLIENT_EMAIL , MOCK_FILENAME , SCOPE )
126114
127- self .assertTrue (found is client ._signed )
128- expected_called_with = {
129- 'service_account_name' : CLIENT_EMAIL ,
130- 'private_key' : PRIVATE_KEY ,
131- 'scope' : SCOPE ,
132- }
133- self .assertEqual (client ._called_with , expected_called_with )
115+ self .assertTrue (found is MOCK_CRED_CLASS ._result )
116+ self .assertEqual (MOCK_CRED_CLASS .p12_called ,
117+ [(CLIENT_EMAIL , MOCK_FILENAME , SCOPE )])
134118
135119
136120class Test_get_for_service_account_json (unittest2 .TestCase ):
@@ -279,8 +263,7 @@ def _run_with_fake_crypto(self, credentials, private_key_text,
279263 result = self ._callFUT (credentials , string_to_sign )
280264
281265 if crypt ._pkcs12_key_as_pem_called :
282- self .assertEqual (crypt ._private_key_text ,
283- base64 .b64encode (private_key_text ))
266+ self .assertEqual (crypt ._private_key_text , private_key_text )
284267 self .assertEqual (crypt ._private_key_password , 'notasecret' )
285268 self .assertEqual (openssl_crypto ._loaded ,
286269 [(openssl_crypto .FILETYPE_PEM , _Crypt ._KEY )])
@@ -296,22 +279,28 @@ def _run_with_fake_crypto(self, credentials, private_key_text,
296279 self .assertEqual (result , sign_result )
297280
298281 def test_p12_type (self ):
299- from oauth2client .client import SignedJwtAssertionCredentials
282+ from oauth2client .service_account import ServiceAccountCredentials
300283 ACCOUNT_NAME = 'dummy_service_account_name'
301284 PRIVATE_KEY_TEXT = b'dummy_private_key_text'
302285 STRING_TO_SIGN = b'dummy_signature'
303- CREDENTIALS = SignedJwtAssertionCredentials (
304- ACCOUNT_NAME , PRIVATE_KEY_TEXT , [])
286+ SIGNER = object ()
287+ CREDENTIALS = ServiceAccountCredentials (
288+ ACCOUNT_NAME , SIGNER )
289+ CREDENTIALS ._private_key_pkcs12 = PRIVATE_KEY_TEXT
290+ CREDENTIALS ._private_key_password = 'notasecret'
305291 self ._run_with_fake_crypto (CREDENTIALS , PRIVATE_KEY_TEXT ,
306292 STRING_TO_SIGN )
307293
308294 def test_p12_type_non_bytes_to_sign (self ):
309- from oauth2client .client import SignedJwtAssertionCredentials
295+ from oauth2client .service_account import ServiceAccountCredentials
310296 ACCOUNT_NAME = 'dummy_service_account_name'
311297 PRIVATE_KEY_TEXT = b'dummy_private_key_text'
312298 STRING_TO_SIGN = u'dummy_signature'
313- CREDENTIALS = SignedJwtAssertionCredentials (
314- ACCOUNT_NAME , PRIVATE_KEY_TEXT , [])
299+ SIGNER = object ()
300+ CREDENTIALS = ServiceAccountCredentials (
301+ ACCOUNT_NAME , SIGNER )
302+ CREDENTIALS ._private_key_pkcs12 = PRIVATE_KEY_TEXT
303+ CREDENTIALS ._private_key_password = 'notasecret'
315304 self ._run_with_fake_crypto (CREDENTIALS , PRIVATE_KEY_TEXT ,
316305 STRING_TO_SIGN )
317306
@@ -321,21 +310,16 @@ def test_json_type(self):
321310
322311 PRIVATE_KEY_TEXT = 'dummy_private_key_pkcs8_text'
323312 STRING_TO_SIGN = b'dummy_signature'
324-
325- def _get_private_key (private_key_pkcs8_text ):
326- return private_key_pkcs8_text
327-
328- with _Monkey (service_account , _get_private_key = _get_private_key ):
329- CREDENTIALS = service_account ._ServiceAccountCredentials (
330- 'dummy_service_account_id' , 'dummy_service_account_email' ,
331- 'dummy_private_key_id' , PRIVATE_KEY_TEXT , [])
332-
313+ SIGNER = object ()
314+ CREDENTIALS = service_account .ServiceAccountCredentials (
315+ 'dummy_service_account_email' , SIGNER )
316+ CREDENTIALS ._private_key_pkcs8_pem = PRIVATE_KEY_TEXT
333317 self ._run_with_fake_crypto (CREDENTIALS , PRIVATE_KEY_TEXT ,
334318 STRING_TO_SIGN )
335319
336320 def test_gae_type (self ):
337321 # Relies on setUp fixing up App Engine imports.
338- from oauth2client .appengine import AppAssertionCredentials
322+ from oauth2client .contrib . appengine import AppAssertionCredentials
339323 from gcloud ._testing import _Monkey
340324 from gcloud import credentials
341325
@@ -387,33 +371,20 @@ def test_bad_type(self):
387371 None , None , None )
388372 self .assertRaises (ValueError , self ._callFUT , CREDENTIALS )
389373
390- def test_p12_type (self ):
391- from oauth2client .client import SignedJwtAssertionCredentials
392- SERVICE_ACCOUNT_NAME = 'SERVICE_ACCOUNT_NAME'
393- CREDENTIALS = SignedJwtAssertionCredentials (SERVICE_ACCOUNT_NAME ,
394- b'bogus_key' , [])
395- found = self ._callFUT (CREDENTIALS )
396- self .assertEqual (found , SERVICE_ACCOUNT_NAME )
397-
398- def test_json_type (self ):
374+ def test_service_account_type (self ):
399375 from oauth2client import service_account
400- from gcloud ._testing import _Monkey
401-
402- def _get_private_key (private_key_pkcs8_text ):
403- return private_key_pkcs8_text
404376
405377 SERVICE_ACCOUNT_NAME = 'SERVICE_ACCOUNT_NAME'
406- with _Monkey (service_account , _get_private_key = _get_private_key ):
407- CREDENTIALS = service_account ._ServiceAccountCredentials (
408- 'bogus_id' , SERVICE_ACCOUNT_NAME , 'bogus_id' ,
409- 'bogus_key_text' , [])
378+ SIGNER = object ()
379+ CREDENTIALS = service_account .ServiceAccountCredentials (
380+ SERVICE_ACCOUNT_NAME , SIGNER )
410381
411382 found = self ._callFUT (CREDENTIALS )
412383 self .assertEqual (found , SERVICE_ACCOUNT_NAME )
413384
414385 def test_gae_type (self ):
415386 # Relies on setUp fixing up App Engine imports.
416- from oauth2client .appengine import AppAssertionCredentials
387+ from oauth2client .contrib . appengine import AppAssertionCredentials
417388 from gcloud ._testing import _Monkey
418389 from gcloud import credentials
419390
@@ -483,25 +454,26 @@ def test_bad_argument(self):
483454 self .assertRaises (TypeError , self ._callFUT , None )
484455
485456 def test_signed_jwt_for_p12 (self ):
486- import base64
487- from oauth2client import client
457+ from oauth2client import service_account
488458 from gcloud ._testing import _Monkey
489459 from gcloud import credentials as MUT
490460
491- scopes = []
492461 PRIVATE_KEY = b'dummy_private_key_text'
493- credentials = client .SignedJwtAssertionCredentials (
494- 'dummy_service_account_name' , PRIVATE_KEY , scopes )
462+ SIGNER = object ()
463+ credentials = service_account .ServiceAccountCredentials (
464+ 'dummy_service_account_email' , SIGNER )
465+ credentials ._private_key_pkcs12 = PRIVATE_KEY
466+ credentials ._private_key_password = password = 'password-nope'
467+
495468 crypt = _Crypt ()
496469 load_result = object ()
497470 openssl_crypto = _OpenSSLCrypto (load_result , None )
498471
499472 with _Monkey (MUT , crypt = crypt , crypto = openssl_crypto ):
500473 result = self ._callFUT (credentials )
501474
502- self .assertEqual (crypt ._private_key_text ,
503- base64 .b64encode (PRIVATE_KEY ))
504- self .assertEqual (crypt ._private_key_password , 'notasecret' )
475+ self .assertEqual (crypt ._private_key_text , PRIVATE_KEY )
476+ self .assertEqual (crypt ._private_key_password , password )
505477 self .assertEqual (result , load_result )
506478 self .assertEqual (openssl_crypto ._loaded ,
507479 [(openssl_crypto .FILETYPE_PEM , _Crypt ._KEY )])
@@ -515,14 +487,10 @@ def test_service_account_via_json_key(self):
515487 scopes = []
516488
517489 PRIVATE_TEXT = 'dummy_private_key_pkcs8_text'
518-
519- def _get_private_key (private_key_pkcs8_text ):
520- return private_key_pkcs8_text
521-
522- with _Monkey (service_account , _get_private_key = _get_private_key ):
523- credentials = service_account ._ServiceAccountCredentials (
524- 'dummy_service_account_id' , 'dummy_service_account_email' ,
525- 'dummy_private_key_id' , PRIVATE_TEXT , scopes )
490+ SIGNER = object ()
491+ credentials = service_account .ServiceAccountCredentials (
492+ 'dummy_service_account_email' , SIGNER , scopes = scopes )
493+ credentials ._private_key_pkcs8_pem = PRIVATE_TEXT
526494
527495 load_result = object ()
528496 openssl_crypto = _OpenSSLCrypto (load_result , None )
@@ -541,14 +509,11 @@ def test_without_pyopenssl(self):
541509 from gcloud import credentials as credentials_mod
542510
543511 PRIVATE_TEXT = 'dummy_private_key_pkcs8_text'
512+ SIGNER = object ()
544513
545- def _get_private_key (private_key_pkcs8_text ):
546- return private_key_pkcs8_text
547-
548- with _Monkey (service_account , _get_private_key = _get_private_key ):
549- credentials = service_account ._ServiceAccountCredentials (
550- 'dummy_service_account_id' , 'dummy_service_account_email' ,
551- 'dummy_private_key_id' , PRIVATE_TEXT , '' )
514+ credentials = service_account .ServiceAccountCredentials (
515+ 'dummy_service_account_email' , SIGNER )
516+ credentials ._private_key_pkcs8_pem = PRIVATE_TEXT
552517
553518 with _Monkey (credentials_mod , crypto = None ):
554519 with self .assertRaises (EnvironmentError ):
@@ -648,6 +613,7 @@ def create_scoped(self, scopes):
648613
649614
650615class _Client (object ):
616+
651617 def __init__ (self ):
652618 self ._signed = _Credentials ()
653619
@@ -659,10 +625,6 @@ def get_application_default():
659625
660626 self .GoogleCredentials = GoogleCredentials
661627
662- def SignedJwtAssertionCredentials (self , ** kw ):
663- self ._called_with = kw
664- return self ._signed
665-
666628
667629class _Crypt (object ):
668630
@@ -724,3 +686,14 @@ def non_transactional(*args, **kwargs):
724686 def do_nothing_wrapper (func ):
725687 return func
726688 return do_nothing_wrapper
689+
690+
691+ class _MockServiceAccountCredentials (object ):
692+
693+ def __init__ (self ):
694+ self .p12_called = []
695+ self ._result = _Credentials ()
696+
697+ def from_p12_keyfile (self , email , path , scopes = None ):
698+ self .p12_called .append ((email , path , scopes ))
699+ return self ._result
0 commit comments