Skip to content

Commit cbe6330

Browse files
committed
Merge pull request googleapis#482 from dhermes/cover-client-v4
Adding more branch coverage in client.py.
2 parents 3396e52 + 4ada106 commit cbe6330

File tree

2 files changed

+127
-7
lines changed

2 files changed

+127
-7
lines changed

oauth2client/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,7 @@ def sign_blob(self, blob):
16281628
raise NotImplementedError('This method is abstract.')
16291629

16301630

1631-
def _RequireCryptoOrDie():
1631+
def _require_crypto_or_die():
16321632
"""Ensure we have a crypto library, or throw CryptoUnavailableError.
16331633
16341634
The oauth2client.crypt module requires either PyCrypto or PyOpenSSL
@@ -1667,7 +1667,7 @@ def verify_id_token(id_token, audience, http=None,
16671667
oauth2client.crypt.AppIdentityError: if the JWT fails to verify.
16681668
CryptoUnavailableError: if no crypto library is available.
16691669
"""
1670-
_RequireCryptoOrDie()
1670+
_require_crypto_or_die()
16711671
if http is None:
16721672
http = _cached_http
16731673

tests/test_client.py

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,40 @@ def test_create_scoped(self):
342342
self.assertEqual(credentials,
343343
credentials.create_scoped(['dummy_scope']))
344344

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+
345379
def test_environment_check_gae_production(self):
346380
with mock_module_import('google.appengine'):
347381
self._environment_check_gce_helper(
@@ -461,6 +495,10 @@ def test_get_environment_variable_file_error(self):
461495
expected_err_msg):
462496
_get_environment_variable_file()
463497

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+
464502
@mock.patch('os.name', new='nt')
465503
@mock.patch.dict(os.environ, {'APPDATA': DATA_DIR}, clear=True)
466504
def test_get_well_known_file_on_windows(self):
@@ -469,6 +507,14 @@ def test_get_well_known_file_on_windows(self):
469507
_WELL_KNOWN_CREDENTIALS_FILE))
470508
self.assertEqual(well_known_file, _get_well_known_file())
471509

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+
472518
@mock.patch.dict(os.environ,
473519
{client._CLOUDSDK_CONFIG_ENV_VAR: 'CUSTOM_DIR'},
474520
clear=True)
@@ -593,7 +639,7 @@ def test_raise_exception_for_reading_json(self):
593639
@mock.patch('oauth2client.client._in_gae_environment', return_value=False)
594640
@mock.patch('oauth2client.client._get_environment_variable_file')
595641
@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):
597643
# Set up stubs.
598644
get_well_known, get_env_file, in_gae, in_gce = stubs
599645
get_env_file.return_value = datafile(
@@ -609,14 +655,14 @@ def test_get_adc_from_environment_variable_service_account(self, *stubs):
609655

610656
def test_env_name(self):
611657
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()
613659
self.assertEqual(DEFAULT_ENV_NAME, client.SETTINGS.env_name)
614660

615661
@mock.patch('oauth2client.client._in_gce_environment')
616662
@mock.patch('oauth2client.client._in_gae_environment', return_value=False)
617663
@mock.patch('oauth2client.client._get_environment_variable_file')
618664
@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):
620666
# Set up stubs.
621667
get_well_known, get_env_file, in_gae, in_gce = stubs
622668
get_env_file.return_value = datafile(os.path.join(
@@ -635,7 +681,7 @@ def test_get_adc_from_environment_variable_authorized_user(self, *stubs):
635681
@mock.patch('oauth2client.client._in_gae_environment', return_value=False)
636682
@mock.patch('oauth2client.client._get_environment_variable_file')
637683
@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):
639685
# Set up stubs.
640686
get_well_known, get_env_file, in_gae, in_gce = stubs
641687
get_env_file.return_value = datafile(
@@ -662,7 +708,7 @@ def test_get_adc_from_environment_variable_malformed_file(self, *stubs):
662708
return_value=None)
663709
@mock.patch('oauth2client.client._get_well_known_file',
664710
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):
666712
# Unpack stubs.
667713
get_well_known, get_env_file, in_gae, in_gce = stubs
668714
# 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):
678724
in_gae.assert_called_once_with()
679725
in_gce.assert_called_once_with()
680726

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+
681754
def test_from_stream_service_account(self):
682755
credentials_file = datafile(
683756
os.path.join('gcloud', _WELL_KNOWN_CREDENTIALS_FILE))
@@ -693,6 +766,15 @@ def test_from_stream_authorized_user(self):
693766
credentials_file)
694767
self.validate_google_credentials(credentials)
695768

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+
696778
def test_from_stream_malformed_file_1(self):
697779
credentials_file = datafile(
698780
os.path.join('gcloud',
@@ -1867,5 +1949,43 @@ def test_existing(self):
18671949
self._save_helper(filename)
18681950

18691951

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+
18701990
if __name__ == '__main__': # pragma: NO COVER
18711991
unittest2.main()

0 commit comments

Comments
 (0)