aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJon Wayne Parrott <jon.wayne.parrott@gmail.com>2016-04-08 12:33:13 -0700
committerJon Wayne Parrott <jon.wayne.parrott@gmail.com>2016-04-08 14:19:10 -0700
commit2f724203e41c771b4cc4e91b283833baf66f9c5f (patch)
treee86ecf8b08b118b23967058e46473118cc09a0ab /tests
parentef33f70abb43da50793f169ad7efdd4c72df4966 (diff)
downloadoauth2client-2f724203e41c771b4cc4e91b283833baf66f9c5f.tar.gz
100% coverage for oauth2client.contrib.multistore_file
Diffstat (limited to 'tests')
-rw-r--r--tests/contrib/test_multistore_file.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/contrib/test_multistore_file.py b/tests/contrib/test_multistore_file.py
index 11c423d..53b6542 100644
--- a/tests/contrib/test_multistore_file.py
+++ b/tests/contrib/test_multistore_file.py
@@ -21,6 +21,8 @@ import stat
import tempfile
import unittest2
+import mock
+
from oauth2client import util
from oauth2client.client import OAuth2Credentials
from oauth2client.contrib import locked_file
@@ -116,6 +118,18 @@ class MultistoreFileTests(unittest2.TestCase):
finally:
os.unlink(filename)
+ def test_lock_file_raise_unexpected_error(self):
+ filehandle, filename = tempfile.mkstemp()
+ os.close(filehandle)
+
+ try:
+ multistore = multistore_file._MultiStore(filename)
+ multistore._file = _MockLockedFile(filename, errno.EBUSY)
+ self.assertRaises(IOError, multistore._lock)
+ self.assertTrue(multistore._file.open_and_lock_called)
+ finally:
+ os.unlink(filename)
+
def test_read_only_file_fail_lock(self):
credentials = self._create_test_credentials()
@@ -133,6 +147,32 @@ class MultistoreFileTests(unittest2.TestCase):
self.assertTrue(store._multistore._read_only)
os.chmod(FILENAME, 0o600)
+ def test_read_only_file_fail_lock_no_warning(self):
+ open(FILENAME, 'a+b').close()
+ os.chmod(FILENAME, 0o400)
+
+ multistore = multistore_file._MultiStore(FILENAME)
+
+ with mock.patch.object(multistore_file.logger, 'warn') as mock_warn:
+ multistore._warn_on_readonly = False
+ multistore._lock()
+ self.assertFalse(mock_warn.called)
+
+ def test_lock_skip_refresh(self):
+ with open(FILENAME, 'w') as f:
+ f.write('123')
+ os.chmod(FILENAME, 0o400)
+
+ multistore = multistore_file._MultiStore(FILENAME)
+
+ refresh_patch = mock.patch.object(
+ multistore, '_refresh_data_cache')
+
+ with refresh_patch as refresh_mock:
+ multistore._data = {}
+ multistore._lock()
+ self.assertFalse(refresh_mock.called)
+
@unittest2.skipIf(not hasattr(os, 'symlink'), 'No symlink available')
def test_multistore_no_symbolic_link_files(self):
SYMFILENAME = FILENAME + 'sym'
@@ -168,12 +208,14 @@ class MultistoreFileTests(unittest2.TestCase):
credentials.user_agent,
['some-scope', 'some-other-scope'])
+ # Save credentials
store.put(credentials)
credentials = store.get()
self.assertNotEquals(None, credentials)
self.assertEquals('foo', credentials.access_token)
+ # Delete credentials
store.delete()
credentials = store.get()
@@ -282,6 +324,61 @@ class MultistoreFileTests(unittest2.TestCase):
keys = multistore_file.get_all_credential_keys(FILENAME)
self.assertEquals([], keys)
+ def _refresh_data_cache_helper(self):
+ multistore = multistore_file._MultiStore(FILENAME)
+ json_patch = mock.patch.object(multistore, '_locked_json_read')
+
+ return multistore, json_patch
+
+ def test__refresh_data_cache_bad_json(self):
+ multistore, json_patch = self._refresh_data_cache_helper()
+
+ with json_patch as json_mock:
+ json_mock.side_effect = ValueError('')
+ multistore._refresh_data_cache()
+ self.assertTrue(json_mock.called)
+ self.assertEqual(multistore._data, {})
+
+ def test__refresh_data_cache_bad_version(self):
+ multistore, json_patch = self._refresh_data_cache_helper()
+
+ with json_patch as json_mock:
+ json_mock.return_value = {}
+ multistore._refresh_data_cache()
+ self.assertTrue(json_mock.called)
+ self.assertEqual(multistore._data, {})
+
+ def test__refresh_data_cache_newer_version(self):
+ multistore, json_patch = self._refresh_data_cache_helper()
+
+ with json_patch as json_mock:
+ json_mock.return_value = {'file_version': 5}
+ self.assertRaises(
+ multistore_file.NewerCredentialStoreError,
+ multistore._refresh_data_cache)
+ self.assertTrue(json_mock.called)
+
+ def test__refresh_data_cache_bad_credentials(self):
+ multistore, json_patch = self._refresh_data_cache_helper()
+
+ with json_patch as json_mock:
+ json_mock.return_value = {
+ 'file_version': 1,
+ 'data': [
+ {'lol': 'this is a bad credential object.'}
+ ]}
+ multistore._refresh_data_cache()
+ self.assertTrue(json_mock.called)
+ self.assertEqual(multistore._data, {})
+
+ def test__delete_credential_nonexistent(self):
+ multistore = multistore_file._MultiStore(FILENAME)
+
+ with mock.patch.object(multistore, '_write') as write_mock:
+ multistore._data = {}
+ multistore._delete_credential('nonexistent_key')
+ self.assertTrue(write_mock.called)
+
if __name__ == '__main__': # pragma: NO COVER
unittest2.main()