diff options
author | Danny Hermes <daniel.j.hermes@gmail.com> | 2016-02-21 10:07:08 -0800 |
---|---|---|
committer | Danny Hermes <daniel.j.hermes@gmail.com> | 2016-02-21 10:07:08 -0800 |
commit | cd0cbe311c43772a36ebe46004f696e544f1145f (patch) | |
tree | 34f16caf1b39acd13aa1919a8d1a9c4788838b17 /tests | |
parent | c66e4f201f5ed72514d18a1227be5d34efa2421a (diff) | |
download | oauth2client-cd0cbe311c43772a36ebe46004f696e544f1145f.tar.gz |
Getting to 100% line coverage in tests.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/contrib/test_multistore_file.py | 36 | ||||
-rw-r--r-- | tests/test_client.py | 49 | ||||
-rw-r--r-- | tests/test_file.py | 22 |
3 files changed, 48 insertions, 59 deletions
diff --git a/tests/contrib/test_multistore_file.py b/tests/contrib/test_multistore_file.py index 5e1fe77..5e1d29b 100644 --- a/tests/contrib/test_multistore_file.py +++ b/tests/contrib/test_multistore_file.py @@ -19,7 +19,7 @@ import errno import os import stat import tempfile -import unittest +import unittest2 from oauth2client import util from oauth2client.client import OAuth2Credentials @@ -48,7 +48,7 @@ class _MockLockedFile(object): return self.filename_str -class MultistoreFileTests(unittest.TestCase): +class MultistoreFileTests(unittest2.TestCase): def tearDown(self): try: @@ -105,25 +105,25 @@ class MultistoreFileTests(unittest.TestCase): ['some-scope', 'some-other-scope']) store.put(credentials) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER self.assertTrue(store._multistore._read_only) os.chmod(FILENAME, 0o600) + @unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available') def test_multistore_no_symbolic_link_files(self): - if hasattr(os, 'symlink'): - SYMFILENAME = FILENAME + 'sym' - os.symlink(FILENAME, SYMFILENAME) - store = multistore_file.get_credential_storage( - SYMFILENAME, - 'some_client_id', - 'user-agent/1.0', - ['some-scope', 'some-other-scope']) - try: - self.assertRaises( - locked_file.CredentialsFileSymbolicLinkError, - store.get) - finally: - os.unlink(SYMFILENAME) + SYMFILENAME = FILENAME + 'sym' + os.symlink(FILENAME, SYMFILENAME) + store = multistore_file.get_credential_storage( + SYMFILENAME, + 'some_client_id', + 'user-agent/1.0', + ['some-scope', 'some-other-scope']) + try: + self.assertRaises( + locked_file.CredentialsFileSymbolicLinkError, + store.get) + finally: + os.unlink(SYMFILENAME) def test_multistore_non_existent_file(self): store = multistore_file.get_credential_storage( @@ -155,7 +155,7 @@ class MultistoreFileTests(unittest.TestCase): self.assertEquals(None, credentials) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER self.assertEquals( 0o600, stat.S_IMODE(os.stat(FILENAME).st_mode)) diff --git a/tests/test_client.py b/tests/test_client.py index 9b1c819..018727c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -305,33 +305,23 @@ class GoogleCredentialsTests(unittest2.TestCase): expected_err_msg): _get_environment_variable_file() + @mock.patch('os.name', new='nt') + @mock.patch.dict(os.environ, {'APPDATA': DATA_DIR}, clear=True) def test_get_well_known_file_on_windows(self): - ORIGINAL_ISDIR = os.path.isdir - try: - os.path.isdir = lambda path: True - well_known_file = datafile( - os.path.join(client._CLOUDSDK_CONFIG_DIRECTORY, - _WELL_KNOWN_CREDENTIALS_FILE)) - os.name = 'nt' - os.environ['APPDATA'] = DATA_DIR - self.assertEqual(well_known_file, _get_well_known_file()) - finally: - os.path.isdir = ORIGINAL_ISDIR - + well_known_file = datafile( + os.path.join(client._CLOUDSDK_CONFIG_DIRECTORY, + _WELL_KNOWN_CREDENTIALS_FILE)) + self.assertEqual(well_known_file, _get_well_known_file()) + + @mock.patch.dict(os.environ, + {client._CLOUDSDK_CONFIG_ENV_VAR: 'CUSTOM_DIR'}, + clear=True) def test_get_well_known_file_with_custom_config_dir(self): - ORIGINAL_ENVIRON = os.environ - ORIGINAL_ISDIR = os.path.isdir - CUSTOM_DIR = 'CUSTOM_DIR' + CUSTOM_DIR = os.environ[client._CLOUDSDK_CONFIG_ENV_VAR] EXPECTED_FILE = os.path.join(CUSTOM_DIR, _WELL_KNOWN_CREDENTIALS_FILE) - try: - os.environ = {client._CLOUDSDK_CONFIG_ENV_VAR: CUSTOM_DIR} - os.path.isdir = lambda path: True - well_known_file = _get_well_known_file() - self.assertEqual(well_known_file, EXPECTED_FILE) - finally: - os.environ = ORIGINAL_ENVIRON - os.path.isdir = ORIGINAL_ISDIR + well_known_file = _get_well_known_file() + self.assertEqual(well_known_file, EXPECTED_FILE) def test_get_adc_from_file_service_account(self): credentials_file = datafile( @@ -357,17 +347,16 @@ class GoogleCredentialsTests(unittest2.TestCase): self.assertEqual('ABCDEF', d['private_key_id']) os.remove(temp_credential_file) - def test_save_well_known_file_with_non_existent_config_dir(self): + @mock.patch('os.path.isdir', return_value=False) + def test_save_well_known_file_with_non_existent_config_dir(self, + isdir_mock): credential_file = datafile( os.path.join('gcloud', _WELL_KNOWN_CREDENTIALS_FILE)) credentials = _get_application_default_credential_from_file( credential_file) - ORIGINAL_ISDIR = os.path.isdir - try: - os.path.isdir = lambda path: False - self.assertRaises(OSError, save_to_well_known_file, credentials) - finally: - os.path.isdir = ORIGINAL_ISDIR + self.assertRaises(OSError, save_to_well_known_file, credentials) + config_dir = os.path.join(os.path.expanduser('~'), '.config', 'gcloud') + isdir_mock.assert_called_once_with(config_dir) def test_get_adc_from_file_authorized_user(self): credentials_file = datafile(os.path.join( diff --git a/tests/test_file.py b/tests/test_file.py index 7ecc620..4bf56bf 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -79,16 +79,16 @@ class OAuth2ClientFileTests(unittest2.TestCase): credentials = s.get() self.assertEquals(None, credentials) + @unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available') def test_no_sym_link_credentials(self): - if hasattr(os, 'symlink'): - SYMFILENAME = FILENAME + '.sym' - os.symlink(FILENAME, SYMFILENAME) - s = file.Storage(SYMFILENAME) - try: - with self.assertRaises(file.CredentialsFileSymbolicLinkError): - s.get() - finally: - os.unlink(SYMFILENAME) + SYMFILENAME = FILENAME + '.sym' + os.symlink(FILENAME, SYMFILENAME) + s = file.Storage(SYMFILENAME) + try: + with self.assertRaises(file.CredentialsFileSymbolicLinkError): + s.get() + finally: + os.unlink(SYMFILENAME) def test_pickle_and_json_interop(self): # Write a file with a pickled OAuth2Credentials. @@ -177,7 +177,7 @@ class OAuth2ClientFileTests(unittest2.TestCase): new_cred.access_token = 'bar' s.put(new_cred) - credentials._refresh(lambda x: x) + credentials._refresh(None) self.assertEquals(credentials.access_token, 'bar') def test_token_refresh_stream_body(self): @@ -238,7 +238,7 @@ class OAuth2ClientFileTests(unittest2.TestCase): self.assertTrue(os.path.exists(FILENAME)) - if os.name == 'posix': + if os.name == 'posix': # pragma: NO COVER mode = os.stat(FILENAME).st_mode self.assertEquals('0o600', oct(stat.S_IMODE(mode))) |