Merge pull request #305 from dhermes/remove-test-yagni
Removed unused test code.
This commit is contained in:
@@ -24,21 +24,15 @@ import httplib2
|
||||
class HttpMock(object):
|
||||
"""Mock of httplib2.Http"""
|
||||
|
||||
def __init__(self, filename=None, headers=None):
|
||||
def __init__(self, headers=None):
|
||||
"""HttpMock constructor.
|
||||
|
||||
Args:
|
||||
filename: string, absolute filename to read response from
|
||||
headers: dict, header to return with response
|
||||
"""
|
||||
if headers is None:
|
||||
headers = {'status': '200 OK'}
|
||||
if filename:
|
||||
f = file(filename, 'r')
|
||||
self.data = f.read()
|
||||
f.close()
|
||||
else:
|
||||
self.data = None
|
||||
headers = {'status': '200'}
|
||||
self.data = None
|
||||
self.response_headers = headers
|
||||
self.headers = None
|
||||
self.uri = None
|
||||
@@ -78,10 +72,7 @@ class HttpMockSequence(object):
|
||||
|
||||
* 'echo_request_headers' means return the request headers in the response
|
||||
body
|
||||
* 'echo_request_headers_as_json' means return the request headers in
|
||||
the response body
|
||||
* 'echo_request_body' means return the request body in the response body
|
||||
* 'echo_request_uri' means return the request uri in the response body
|
||||
"""
|
||||
|
||||
def __init__(self, iterable):
|
||||
@@ -107,13 +98,17 @@ class HttpMockSequence(object):
|
||||
if getattr(body, 'read', None) else None)
|
||||
if content == 'echo_request_headers':
|
||||
content = headers
|
||||
elif content == 'echo_request_headers_as_json':
|
||||
content = json.dumps(headers)
|
||||
elif content == 'echo_request_body':
|
||||
content = (body
|
||||
if body_stream_content is None else body_stream_content)
|
||||
elif content == 'echo_request_uri':
|
||||
content = uri
|
||||
elif not isinstance(content, bytes):
|
||||
raise TypeError('http content should be bytes: %r' % (content,))
|
||||
return httplib2.Response(resp), content
|
||||
|
||||
|
||||
class CacheMock(object):
|
||||
|
||||
def __init__(self):
|
||||
self.cache = {}
|
||||
|
||||
def get(self, key, namespace=''):
|
||||
# ignoring namespace for easier testing
|
||||
return self.cache.get(key, None)
|
||||
|
||||
@@ -34,6 +34,7 @@ dev_appserver.fix_sys_path()
|
||||
import mock
|
||||
import webapp2
|
||||
|
||||
from .http_mock import CacheMock
|
||||
from google.appengine.api import apiproxy_stub
|
||||
from google.appengine.api import apiproxy_stub_map
|
||||
from google.appengine.api import app_identity
|
||||
@@ -80,19 +81,6 @@ def load_and_cache(existing_file, fakename, cache_mock):
|
||||
cache_mock.cache[fakename] = {client_type: client_info}
|
||||
|
||||
|
||||
class CacheMock(object):
|
||||
def __init__(self):
|
||||
self.cache = {}
|
||||
|
||||
def get(self, key, namespace=''):
|
||||
# ignoring namespace for easier testing
|
||||
return self.cache.get(key, None)
|
||||
|
||||
def set(self, key, value, namespace=''):
|
||||
# ignoring namespace for easier testing
|
||||
self.cache[key] = value
|
||||
|
||||
|
||||
class UserMock(object):
|
||||
"""Mock the app engine user service"""
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ import mock
|
||||
import six
|
||||
from six.moves import urllib
|
||||
|
||||
from .http_mock import CacheMock
|
||||
from .http_mock import HttpMock
|
||||
from .http_mock import HttpMockSequence
|
||||
from oauth2client import GOOGLE_REVOKE_URI
|
||||
@@ -112,19 +113,6 @@ def load_and_cache(existing_file, fakename, cache_mock):
|
||||
cache_mock.cache[fakename] = {client_type: client_info}
|
||||
|
||||
|
||||
class CacheMock(object):
|
||||
def __init__(self):
|
||||
self.cache = {}
|
||||
|
||||
def get(self, key, namespace=''):
|
||||
# ignoring namespace for easier testing
|
||||
return self.cache.get(key, None)
|
||||
|
||||
def set(self, key, value, namespace=''):
|
||||
# ignoring namespace for easier testing
|
||||
self.cache[key] = value
|
||||
|
||||
|
||||
class CredentialsTests(unittest.TestCase):
|
||||
|
||||
def test_to_from_json(self):
|
||||
@@ -133,23 +121,6 @@ class CredentialsTests(unittest.TestCase):
|
||||
restored = Credentials.new_from_json(json)
|
||||
|
||||
|
||||
class MockResponse(object):
|
||||
"""Mock the response of urllib2.urlopen() call."""
|
||||
|
||||
def __init__(self, headers):
|
||||
self._headers = headers
|
||||
|
||||
def info(self):
|
||||
class Info:
|
||||
def __init__(self, headers):
|
||||
self.headers = headers
|
||||
|
||||
def get(self, key, default=None):
|
||||
return self.headers.get(key, default)
|
||||
|
||||
return Info(self._headers)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def mock_module_import(module):
|
||||
"""Place a dummy objects in sys.modules to mock an import test."""
|
||||
@@ -169,27 +140,19 @@ def mock_module_import(module):
|
||||
class GoogleCredentialsTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.env_server_software = os.environ.get('SERVER_SOFTWARE', None)
|
||||
self.env_google_application_credentials = (
|
||||
os.environ.get(GOOGLE_APPLICATION_CREDENTIALS, None))
|
||||
self.env_appdata = os.environ.get('APPDATA', None)
|
||||
self.os_name = os.name
|
||||
from oauth2client import client
|
||||
client.SETTINGS.env_name = None
|
||||
|
||||
def tearDown(self):
|
||||
self.reset_env('SERVER_SOFTWARE', self.env_server_software)
|
||||
self.reset_env(GOOGLE_APPLICATION_CREDENTIALS,
|
||||
self.env_google_application_credentials)
|
||||
self.reset_env('APPDATA', self.env_appdata)
|
||||
self.reset_env('SERVER_SOFTWARE')
|
||||
self.reset_env(GOOGLE_APPLICATION_CREDENTIALS)
|
||||
self.reset_env('APPDATA')
|
||||
os.name = self.os_name
|
||||
|
||||
def reset_env(self, env, value):
|
||||
def reset_env(self, env):
|
||||
"""Set the environment variable 'env' to 'value'."""
|
||||
if value is not None:
|
||||
os.environ[env] = value
|
||||
else:
|
||||
os.environ.pop(env, '')
|
||||
os.environ.pop(env, None)
|
||||
|
||||
def validate_service_account_credentials(self, credentials):
|
||||
self.assertTrue(isinstance(credentials, _ServiceAccountCredentials))
|
||||
@@ -796,7 +759,7 @@ class BasicCredentialsTests(unittest.TestCase):
|
||||
# First, test that we correctly encode basic objects, making sure
|
||||
# to include a bytes object. Note that oauth2client will normalize
|
||||
# everything to bytes, no matter what python version we're in.
|
||||
http = credentials.authorize(HttpMock(headers={'status': '200'}))
|
||||
http = credentials.authorize(HttpMock())
|
||||
headers = {u'foo': 3, b'bar': True, 'baz': b'abc'}
|
||||
cleaned_headers = {b'foo': b'3', b'bar': b'True', b'baz': b'abc'}
|
||||
http.request(u'http://example.com', method=u'GET', headers=headers)
|
||||
@@ -825,7 +788,7 @@ class BasicCredentialsTests(unittest.TestCase):
|
||||
refresh_token, token_expiry, token_uri,
|
||||
user_agent, revoke_uri=revoke_uri)
|
||||
|
||||
http = HttpMock(headers={'status': '200'})
|
||||
http = HttpMock()
|
||||
http = credentials.authorize(http)
|
||||
http.request(u'http://example.com', method=u'GET',
|
||||
headers={u'foo': u'bar'})
|
||||
@@ -941,8 +904,6 @@ class AccessTokenCredentialsTests(unittest.TestCase):
|
||||
self.fail('should throw exception if token expires')
|
||||
except AccessTokenCredentialsError:
|
||||
pass
|
||||
except Exception:
|
||||
self.fail('should only throw AccessTokenCredentialsError')
|
||||
|
||||
def test_token_revoke_success(self):
|
||||
_token_revoke_test_helper(
|
||||
|
||||
@@ -21,6 +21,7 @@ import unittest
|
||||
|
||||
import httplib2
|
||||
|
||||
from oauth2client._helpers import _from_bytes
|
||||
from oauth2client import GOOGLE_AUTH_URI
|
||||
from oauth2client import GOOGLE_REVOKE_URI
|
||||
from oauth2client import GOOGLE_TOKEN_URI
|
||||
@@ -207,10 +208,7 @@ class OAuth2CredentialsTests(unittest.TestCase):
|
||||
]
|
||||
for src, match in ERRORS:
|
||||
# Ensure that it is unicode
|
||||
try:
|
||||
src = src.decode('utf-8')
|
||||
except AttributeError:
|
||||
pass
|
||||
src = _from_bytes(src)
|
||||
# Test load(s)
|
||||
try:
|
||||
clientsecrets.loads(src)
|
||||
|
||||
@@ -22,6 +22,7 @@ import unittest
|
||||
|
||||
import mock
|
||||
|
||||
from oauth2client._helpers import _from_bytes
|
||||
from oauth2client._helpers import _to_bytes
|
||||
from oauth2client.client import save_to_well_known_file
|
||||
from oauth2client.devshell import _SendRecv
|
||||
@@ -107,9 +108,10 @@ class _AuthReferenceServer(threading.Thread):
|
||||
super(_AuthReferenceServer, self).__init__(None)
|
||||
self.response = (response or
|
||||
'["joe@example.com", "fooproj", "sometoken"]')
|
||||
self.bad_request = False
|
||||
|
||||
def __enter__(self):
|
||||
self.start_server()
|
||||
return self.start_server()
|
||||
|
||||
def start_server(self):
|
||||
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
@@ -139,21 +141,20 @@ class _AuthReferenceServer(threading.Thread):
|
||||
s, unused_addr = self._socket.accept()
|
||||
resp_buffer = ''
|
||||
resp_1 = s.recv(6).decode()
|
||||
if '\n' not in resp_1:
|
||||
raise Exception('invalid request data')
|
||||
nstr, extra = resp_1.split('\n', 1)
|
||||
resp_buffer = extra
|
||||
n = int(nstr)
|
||||
to_read = n - len(extra)
|
||||
if to_read > 0:
|
||||
resp_buffer += s.recv(to_read, socket.MSG_WAITALL)
|
||||
resp_buffer += _from_bytes(s.recv(to_read, socket.MSG_WAITALL))
|
||||
if resp_buffer != CREDENTIAL_INFO_REQUEST_JSON:
|
||||
raise Exception('bad request')
|
||||
self.bad_request = True
|
||||
l = len(self.response)
|
||||
s.sendall(('%d\n%s' % (l, self.response)).encode())
|
||||
finally:
|
||||
if s:
|
||||
s.close()
|
||||
# Will fail if s is None, but these tests never encounter
|
||||
# that scenario.
|
||||
s.close()
|
||||
|
||||
|
||||
class DevshellCredentialsTests(unittest.TestCase):
|
||||
@@ -161,6 +162,27 @@ class DevshellCredentialsTests(unittest.TestCase):
|
||||
def test_signals_no_server(self):
|
||||
self.assertRaises(NoDevshellServer, DevshellCredentials)
|
||||
|
||||
def test_bad_message_to_mock_server(self):
|
||||
request_content = CREDENTIAL_INFO_REQUEST_JSON + 'extrastuff'
|
||||
request_message = _to_bytes(
|
||||
'%d\n%s' % (len(request_content), request_content))
|
||||
response_message = 'foobar'
|
||||
with _AuthReferenceServer(response_message) as auth_server:
|
||||
self.assertFalse(auth_server.bad_request)
|
||||
sock = socket.socket()
|
||||
port = int(os.getenv(DEVSHELL_ENV, 0))
|
||||
sock.connect(('localhost', port))
|
||||
sock.sendall(request_message)
|
||||
|
||||
# Mimic the receive part of _SendRecv
|
||||
header = sock.recv(6).decode()
|
||||
len_str, result = header.split('\n', 1)
|
||||
to_read = int(len_str) - len(result)
|
||||
result += sock.recv(to_read, socket.MSG_WAITALL).decode()
|
||||
|
||||
self.assertTrue(auth_server.bad_request)
|
||||
self.assertEqual(result, response_message)
|
||||
|
||||
def test_request_response(self):
|
||||
with _AuthReferenceServer():
|
||||
response = _SendRecv()
|
||||
|
||||
Reference in New Issue
Block a user