aboutsummaryrefslogtreecommitdiff
path: root/tests/contrib/test_multiprocess_file_storage.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/contrib/test_multiprocess_file_storage.py')
-rw-r--r--tests/contrib/test_multiprocess_file_storage.py71
1 files changed, 25 insertions, 46 deletions
diff --git a/tests/contrib/test_multiprocess_file_storage.py b/tests/contrib/test_multiprocess_file_storage.py
index d8b91a9..bf30c14 100644
--- a/tests/contrib/test_multiprocess_file_storage.py
+++ b/tests/contrib/test_multiprocess_file_storage.py
@@ -20,16 +20,16 @@ import json
import multiprocessing
import os
import tempfile
-import unittest
import fasteners
import mock
-import six
-from six.moves import urllib_parse
+from six import StringIO
+import unittest2
from oauth2client import client
from oauth2client.contrib import multiprocess_file_storage
-from tests import http_mock
+
+from ..http_mock import HttpMockSequence
@contextlib.contextmanager
@@ -68,10 +68,14 @@ def _generate_token_response_http(new_token='new_token'):
'access_token': new_token,
'expires_in': '3600',
})
- return http_mock.HttpMock(data=token_response)
+ http = HttpMockSequence([
+ ({'status': '200'}, token_response),
+ ])
+
+ return http
-class MultiprocessStorageBehaviorTests(unittest.TestCase):
+class MultiprocessStorageBehaviorTests(unittest2.TestCase):
def setUp(self):
filehandle, self.filename = tempfile.mkstemp(
@@ -111,23 +115,6 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
self.assertIsNone(credentials)
- def _verify_refresh_payload(self, http, credentials):
- self.assertEqual(http.requests, 1)
- self.assertEqual(http.uri, credentials.token_uri)
- self.assertEqual(http.method, 'POST')
- expected_body = {
- 'grant_type': ['refresh_token'],
- 'client_id': [credentials.client_id],
- 'client_secret': [credentials.client_secret],
- 'refresh_token': [credentials.refresh_token],
- }
- self.assertEqual(urllib_parse.parse_qs(http.body), expected_body)
- expected_headers = {
- 'content-type': 'application/x-www-form-urlencoded',
- 'user-agent': credentials.user_agent,
- }
- self.assertEqual(http.headers, expected_headers)
-
def test_single_process_refresh(self):
store = multiprocess_file_storage.MultiprocessFileStorage(
self.filename, 'single-process')
@@ -141,9 +128,6 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
retrieved = store.get()
self.assertEqual(retrieved.access_token, 'new_token')
- # Verify mocks.
- self._verify_refresh_payload(http, credentials)
-
def test_multi_process_refresh(self):
# This will test that two processes attempting to refresh credentials
# will only refresh once.
@@ -152,7 +136,6 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
credentials = _create_test_credentials()
credentials.set_store(store)
store.put(credentials)
- actual_token = 'b'
def child_process_func(
die_event, ready_event, check_event): # pragma: NO COVER
@@ -173,12 +156,10 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
credentials.store.acquire_lock = replacement_acquire_lock
- http = _generate_token_response_http(actual_token)
+ http = _generate_token_response_http('b')
credentials.refresh(http)
- self.assertEqual(credentials.access_token, actual_token)
- # Verify mock http.
- self._verify_refresh_payload(http, credentials)
+ self.assertEqual(credentials.access_token, 'b')
check_event = multiprocessing.Event()
with scoped_child_process(child_process_func, check_event=check_event):
@@ -187,17 +168,15 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
store._backend._process_lock.acquire(blocking=False))
check_event.set()
- http = _generate_token_response_http('not ' + actual_token)
- credentials.refresh(http=http)
# The child process will refresh first, so we should end up
- # with `actual_token`' as the token.
- self.assertEqual(credentials.access_token, actual_token)
-
- # Make sure the refresh did not make a request.
- self.assertEqual(http.requests, 0)
+ # with 'b' as the token.
+ http = mock.Mock()
+ credentials.refresh(http=http)
+ self.assertEqual(credentials.access_token, 'b')
+ self.assertFalse(http.request.called)
retrieved = store.get()
- self.assertEqual(retrieved.access_token, actual_token)
+ self.assertEqual(retrieved.access_token, 'b')
def test_read_only_file_fail_lock(self):
credentials = _create_test_credentials()
@@ -221,7 +200,7 @@ class MultiprocessStorageBehaviorTests(unittest.TestCase):
self.assertIsNotNone(store.get())
-class MultiprocessStorageUnitTests(unittest.TestCase):
+class MultiprocessStorageUnitTests(unittest2.TestCase):
def setUp(self):
filehandle, self.filename = tempfile.mkstemp(
@@ -254,7 +233,7 @@ class MultiprocessStorageUnitTests(unittest.TestCase):
def test__read_write_credentials_file(self):
credentials = _create_test_credentials()
- contents = six.StringIO()
+ contents = StringIO()
multiprocess_file_storage._write_credentials_file(
contents, {'key': credentials})
@@ -274,23 +253,23 @@ class MultiprocessStorageUnitTests(unittest.TestCase):
# the invalid one but still load the valid one.
data['credentials']['invalid'] = '123'
results = multiprocess_file_storage._load_credentials_file(
- six.StringIO(json.dumps(data)))
+ StringIO(json.dumps(data)))
self.assertNotIn('invalid', results)
self.assertEqual(
results['key'].access_token, credentials.access_token)
def test__load_credentials_file_invalid_json(self):
- contents = six.StringIO('{[')
+ contents = StringIO('{[')
self.assertEqual(
multiprocess_file_storage._load_credentials_file(contents), {})
def test__load_credentials_file_no_file_version(self):
- contents = six.StringIO('{}')
+ contents = StringIO('{}')
self.assertEqual(
multiprocess_file_storage._load_credentials_file(contents), {})
def test__load_credentials_file_bad_file_version(self):
- contents = six.StringIO(json.dumps({'file_version': 1}))
+ contents = StringIO(json.dumps({'file_version': 1}))
self.assertEqual(
multiprocess_file_storage._load_credentials_file(contents), {})
@@ -331,4 +310,4 @@ class MultiprocessStorageUnitTests(unittest.TestCase):
if __name__ == '__main__': # pragma: NO COVER
- unittest.main()
+ unittest2.main()