@@ -342,6 +342,40 @@ def test_create_scoped(self):
342
342
self .assertEqual (credentials ,
343
343
credentials .create_scoped (['dummy_scope' ]))
344
344
345
+ @mock .patch .object (GoogleCredentials ,
346
+ '_implicit_credentials_from_files' )
347
+ @mock .patch .object (GoogleCredentials ,
348
+ '_implicit_credentials_from_gce' )
349
+ @mock .patch .object (client , '_in_gae_environment' ,
350
+ return_value = True )
351
+ @mock .patch .object (client , '_get_application_default_credential_GAE' ,
352
+ return_value = object ())
353
+ def test_get_application_default_in_gae (self , gae_adc , in_gae ,
354
+ from_gce , from_files ):
355
+ credentials = GoogleCredentials .get_application_default ()
356
+ self .assertEqual (credentials , gae_adc .return_value )
357
+ in_gae .assert_called_once_with ()
358
+ from_files .assert_not_called ()
359
+ from_gce .assert_not_called ()
360
+
361
+ @mock .patch .object (GoogleCredentials ,
362
+ '_implicit_credentials_from_gae' ,
363
+ return_value = None )
364
+ @mock .patch .object (GoogleCredentials ,
365
+ '_implicit_credentials_from_files' ,
366
+ return_value = None )
367
+ @mock .patch .object (client , '_in_gce_environment' ,
368
+ return_value = True )
369
+ @mock .patch .object (client , '_get_application_default_credential_GCE' ,
370
+ return_value = object ())
371
+ def test_get_application_default_in_gce (self , gce_adc , in_gce ,
372
+ from_files , from_gae ):
373
+ credentials = GoogleCredentials .get_application_default ()
374
+ self .assertEqual (credentials , gce_adc .return_value )
375
+ in_gce .assert_called_once_with ()
376
+ from_gae .assert_called_once_with ()
377
+ from_files .assert_called_once_with ()
378
+
345
379
def test_environment_check_gae_production (self ):
346
380
with mock_module_import ('google.appengine' ):
347
381
self ._environment_check_gce_helper (
@@ -461,6 +495,10 @@ def test_get_environment_variable_file_error(self):
461
495
expected_err_msg ):
462
496
_get_environment_variable_file ()
463
497
498
+ @mock .patch .dict (os .environ , {}, clear = True )
499
+ def test_get_environment_variable_file_without_env_var (self ):
500
+ self .assertIsNone (_get_environment_variable_file ())
501
+
464
502
@mock .patch ('os.name' , new = 'nt' )
465
503
@mock .patch .dict (os .environ , {'APPDATA' : DATA_DIR }, clear = True )
466
504
def test_get_well_known_file_on_windows (self ):
@@ -469,6 +507,14 @@ def test_get_well_known_file_on_windows(self):
469
507
_WELL_KNOWN_CREDENTIALS_FILE ))
470
508
self .assertEqual (well_known_file , _get_well_known_file ())
471
509
510
+ @mock .patch ('os.name' , new = 'nt' )
511
+ @mock .patch .dict (os .environ , {'SystemDrive' : 'G:' }, clear = True )
512
+ def test_get_well_known_file_on_windows_without_appdata (self ):
513
+ well_known_file = os .path .join ('G:' , '\\ ' ,
514
+ client ._CLOUDSDK_CONFIG_DIRECTORY ,
515
+ client ._WELL_KNOWN_CREDENTIALS_FILE )
516
+ self .assertEqual (well_known_file , _get_well_known_file ())
517
+
472
518
@mock .patch .dict (os .environ ,
473
519
{client ._CLOUDSDK_CONFIG_ENV_VAR : 'CUSTOM_DIR' },
474
520
clear = True )
@@ -593,7 +639,7 @@ def test_raise_exception_for_reading_json(self):
593
639
@mock .patch ('oauth2client.client._in_gae_environment' , return_value = False )
594
640
@mock .patch ('oauth2client.client._get_environment_variable_file' )
595
641
@mock .patch ('oauth2client.client._get_well_known_file' )
596
- def test_get_adc_from_environment_variable_service_account (self , * stubs ):
642
+ def test_get_adc_from_env_var_service_account (self , * stubs ):
597
643
# Set up stubs.
598
644
get_well_known , get_env_file , in_gae , in_gce = stubs
599
645
get_env_file .return_value = datafile (
@@ -609,14 +655,14 @@ def test_get_adc_from_environment_variable_service_account(self, *stubs):
609
655
610
656
def test_env_name (self ):
611
657
self .assertEqual (None , client .SETTINGS .env_name )
612
- self .test_get_adc_from_environment_variable_service_account ()
658
+ self .test_get_adc_from_env_var_service_account ()
613
659
self .assertEqual (DEFAULT_ENV_NAME , client .SETTINGS .env_name )
614
660
615
661
@mock .patch ('oauth2client.client._in_gce_environment' )
616
662
@mock .patch ('oauth2client.client._in_gae_environment' , return_value = False )
617
663
@mock .patch ('oauth2client.client._get_environment_variable_file' )
618
664
@mock .patch ('oauth2client.client._get_well_known_file' )
619
- def test_get_adc_from_environment_variable_authorized_user (self , * stubs ):
665
+ def test_get_adc_from_env_var_authorized_user (self , * stubs ):
620
666
# Set up stubs.
621
667
get_well_known , get_env_file , in_gae , in_gce = stubs
622
668
get_env_file .return_value = datafile (os .path .join (
@@ -635,7 +681,7 @@ def test_get_adc_from_environment_variable_authorized_user(self, *stubs):
635
681
@mock .patch ('oauth2client.client._in_gae_environment' , return_value = False )
636
682
@mock .patch ('oauth2client.client._get_environment_variable_file' )
637
683
@mock .patch ('oauth2client.client._get_well_known_file' )
638
- def test_get_adc_from_environment_variable_malformed_file (self , * stubs ):
684
+ def test_get_adc_from_env_var_malformed_file (self , * stubs ):
639
685
# Set up stubs.
640
686
get_well_known , get_env_file , in_gae , in_gce = stubs
641
687
get_env_file .return_value = datafile (
@@ -662,7 +708,7 @@ def test_get_adc_from_environment_variable_malformed_file(self, *stubs):
662
708
return_value = None )
663
709
@mock .patch ('oauth2client.client._get_well_known_file' ,
664
710
return_value = 'BOGUS_FILE' )
665
- def test_get_application_default_environment_not_set_up (self , * stubs ):
711
+ def test_get_adc_env_not_set_up (self , * stubs ):
666
712
# Unpack stubs.
667
713
get_well_known , get_env_file , in_gae , in_gce = stubs
668
714
# Make sure the well-known file actually doesn't exist.
@@ -678,6 +724,33 @@ def test_get_application_default_environment_not_set_up(self, *stubs):
678
724
in_gae .assert_called_once_with ()
679
725
in_gce .assert_called_once_with ()
680
726
727
+ @mock .patch ('oauth2client.client._in_gce_environment' , return_value = False )
728
+ @mock .patch ('oauth2client.client._in_gae_environment' , return_value = False )
729
+ @mock .patch ('oauth2client.client._get_environment_variable_file' ,
730
+ return_value = None )
731
+ @mock .patch ('oauth2client.client._get_well_known_file' )
732
+ def test_get_adc_env_from_well_known (self , * stubs ):
733
+ # Unpack stubs.
734
+ get_well_known , get_env_file , in_gae , in_gce = stubs
735
+ # Make sure the well-known file is an actual file.
736
+ get_well_known .return_value = __file__
737
+ # Make sure the well-known file actually doesn't exist.
738
+ self .assertTrue (os .path .exists (get_well_known .return_value ))
739
+
740
+ method_name = ('oauth2client.client.'
741
+ '_get_application_default_credential_from_file' )
742
+ result_creds = object ()
743
+ with mock .patch (method_name ,
744
+ return_value = result_creds ) as get_from_file :
745
+ result = GoogleCredentials .get_application_default ()
746
+ self .assertEqual (result , result_creds )
747
+ get_from_file .assert_called_once_with (__file__ )
748
+
749
+ get_well_known .assert_called_once_with ()
750
+ get_env_file .assert_called_once_with ()
751
+ in_gae .assert_called_once_with ()
752
+ in_gce .assert_not_called ()
753
+
681
754
def test_from_stream_service_account (self ):
682
755
credentials_file = datafile (
683
756
os .path .join ('gcloud' , _WELL_KNOWN_CREDENTIALS_FILE ))
@@ -693,6 +766,15 @@ def test_from_stream_authorized_user(self):
693
766
credentials_file )
694
767
self .validate_google_credentials (credentials )
695
768
769
+ def test_from_stream_missing_file (self ):
770
+ credentials_filename = None
771
+ expected_err_msg = (r'The parameter passed to the from_stream\(\) '
772
+ r'method should point to a file.' )
773
+ with self .assertRaisesRegexp (ApplicationDefaultCredentialsError ,
774
+ expected_err_msg ):
775
+ self .get_a_google_credentials_object ().from_stream (
776
+ credentials_filename )
777
+
696
778
def test_from_stream_malformed_file_1 (self ):
697
779
credentials_file = datafile (
698
780
os .path .join ('gcloud' ,
@@ -1867,5 +1949,43 @@ def test_existing(self):
1867
1949
self ._save_helper (filename )
1868
1950
1869
1951
1952
+ class Test__get_application_default_credential_GAE (unittest2 .TestCase ):
1953
+
1954
+ @mock .patch .dict ('sys.modules' , {
1955
+ 'oauth2client.contrib.appengine' : mock .Mock ()})
1956
+ def test_it (self ):
1957
+ gae_mod = sys .modules ['oauth2client.contrib.appengine' ]
1958
+ gae_mod .AppAssertionCredentials = creds_kls = mock .Mock ()
1959
+ creds_kls .return_value = object ()
1960
+ credentials = client ._get_application_default_credential_GAE ()
1961
+ self .assertEqual (credentials , creds_kls .return_value )
1962
+ creds_kls .assert_called_once_with ([])
1963
+
1964
+
1965
+ class Test__get_application_default_credential_GCE (unittest2 .TestCase ):
1966
+
1967
+ @mock .patch .dict ('sys.modules' , {
1968
+ 'oauth2client.contrib.gce' : mock .Mock ()})
1969
+ def test_it (self ):
1970
+ gce_mod = sys .modules ['oauth2client.contrib.gce' ]
1971
+ gce_mod .AppAssertionCredentials = creds_kls = mock .Mock ()
1972
+ creds_kls .return_value = object ()
1973
+ credentials = client ._get_application_default_credential_GCE ()
1974
+ self .assertEqual (credentials , creds_kls .return_value )
1975
+ creds_kls .assert_called_once_with ()
1976
+
1977
+
1978
+ class Test__require_crypto_or_die (unittest2 .TestCase ):
1979
+
1980
+ @mock .patch .object (client , 'HAS_CRYPTO' , new = True )
1981
+ def test_with_crypto (self ):
1982
+ self .assertIsNone (client ._require_crypto_or_die ())
1983
+
1984
+ @mock .patch .object (client , 'HAS_CRYPTO' , new = False )
1985
+ def test_without_crypto (self ):
1986
+ with self .assertRaises (client .CryptoUnavailableError ):
1987
+ client ._require_crypto_or_die ()
1988
+
1989
+
1870
1990
if __name__ == '__main__' : # pragma: NO COVER
1871
1991
unittest2 .main ()
0 commit comments