Use autospec where appropriate
diff --git a/tests/compute_engine/test_credentials.py b/tests/compute_engine/test_credentials.py
index 87f7472..568aec7 100644
--- a/tests/compute_engine/test_credentials.py
+++ b/tests/compute_engine/test_credentials.py
@@ -68,7 +68,7 @@
# expired)
assert self.credentials.valid
- @mock.patch('google.auth.compute_engine._metadata.get')
+ @mock.patch('google.auth.compute_engine._metadata.get', autospec=True)
def test_refresh_error(self, get_mock):
get_mock.side_effect = exceptions.TransportError('http error')
@@ -77,7 +77,7 @@
assert excinfo.match(r'http error')
- @mock.patch('google.auth.compute_engine._metadata.get')
+ @mock.patch('google.auth.compute_engine._metadata.get', autospec=True)
def test_before_request_refreshes(self, get_mock):
get_mock.side_effect = [{
# First request is for sevice account info.
diff --git a/tests/oauth2/test_credentials.py b/tests/oauth2/test_credentials.py
index 5c76cb2..b53b188 100644
--- a/tests/oauth2/test_credentials.py
+++ b/tests/oauth2/test_credentials.py
@@ -46,7 +46,7 @@
with pytest.raises(NotImplementedError):
self.credentials.with_scopes(['email'])
- @mock.patch('google.oauth2._client.refresh_grant')
+ @mock.patch('google.oauth2._client.refresh_grant', autospec=True)
@mock.patch(
'google.auth._helpers.utcnow', return_value=datetime.datetime.min)
def test_refresh_success(self, now_mock, refresh_grant_mock):
diff --git a/tests/oauth2/test_service_account.py b/tests/oauth2/test_service_account.py
index f07f79d..678e6a3 100644
--- a/tests/oauth2/test_service_account.py
+++ b/tests/oauth2/test_service_account.py
@@ -159,7 +159,7 @@
payload = jwt.decode(token, PUBLIC_CERT_BYTES)
assert payload['sub'] == subject
- @mock.patch('google.oauth2._client.jwt_grant')
+ @mock.patch('google.oauth2._client.jwt_grant', autospec=True)
def test_refresh_success(self, jwt_grant_mock):
token = 'token'
jwt_grant_mock.return_value = (
@@ -185,7 +185,7 @@
# expired)
assert self.credentials.valid
- @mock.patch('google.oauth2._client.jwt_grant')
+ @mock.patch('google.oauth2._client.jwt_grant', autospec=True)
def test_before_request_refreshes(self, jwt_grant_mock):
token = 'token'
jwt_grant_mock.return_value = (
diff --git a/tests/test__cloud_sdk.py b/tests/test__cloud_sdk.py
index 86f69a1..ba72072 100644
--- a/tests/test__cloud_sdk.py
+++ b/tests/test__cloud_sdk.py
@@ -38,7 +38,8 @@
with open(os.path.join(DATA_DIR, 'cloud_sdk.cfg')) as fh:
CLOUD_SDK_CONFIG_DATA = fh.read()
-CONFIG_PATH_PATCH = mock.patch('google.auth._cloud_sdk.get_config_path')
+CONFIG_PATH_PATCH = mock.patch(
+ 'google.auth._cloud_sdk.get_config_path', autospec=True)
@pytest.fixture
diff --git a/tests/test__default.py b/tests/test__default.py
index bfb0c39..a317e0a 100644
--- a/tests/test__default.py
+++ b/tests/test__default.py
@@ -43,7 +43,7 @@
LOAD_FILE_PATCH = mock.patch(
'google.auth._default._load_credentials_from_file', return_value=(
- mock.sentinel.credentials, mock.sentinel.project_id))
+ mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
def test__load_credentials_from_file_invalid_json(tmpdir):
@@ -131,7 +131,9 @@
@LOAD_FILE_PATCH
-@mock.patch('google.auth._cloud_sdk.get_application_default_credentials_path')
+@mock.patch(
+ 'google.auth._cloud_sdk.get_application_default_credentials_path',
+ autospec=True)
def test__get_gcloud_sdk_credentials(
mock_get_adc_path, mock_load):
mock_get_adc_path.return_value = SERVICE_ACCOUNT_FILE
@@ -143,7 +145,9 @@
mock_load.assert_called_with(SERVICE_ACCOUNT_FILE)
-@mock.patch('google.auth._cloud_sdk.get_application_default_credentials_path')
+@mock.patch(
+ 'google.auth._cloud_sdk.get_application_default_credentials_path',
+ autospec=True)
def test__get_gcloud_sdk_credentials_non_existent(mock_get_adc_path, tmpdir):
non_existent = tmpdir.join('non-existent')
mock_get_adc_path.return_value = str(non_existent)
@@ -156,7 +160,7 @@
@mock.patch(
'google.auth._cloud_sdk.get_project_id',
- return_value=mock.sentinel.project_id)
+ return_value=mock.sentinel.project_id, autospec=True)
@mock.patch('os.path.isfile', return_value=True)
@LOAD_FILE_PATCH
def test__get_gcloud_sdk_credentials_project_id(
@@ -174,7 +178,7 @@
@mock.patch(
'google.auth._cloud_sdk.get_project_id',
- return_value=None)
+ return_value=None, autospec=True)
@mock.patch('os.path.isfile', return_value=True)
@LOAD_FILE_PATCH
def test__get_gcloud_sdk_credentials_no_project_id(
@@ -212,10 +216,11 @@
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=True)
+ 'google.auth.compute_engine._metadata.ping', return_value=True,
+ autospec=True)
@mock.patch(
'google.auth.compute_engine._metadata.get_project_id',
- return_value='example-project')
+ return_value='example-project', autospec=True)
def test__get_gce_credentials(get_mock, ping_mock):
credentials, project_id = _default._get_gce_credentials()
@@ -223,7 +228,9 @@
assert project_id == 'example-project'
-@mock.patch('google.auth.compute_engine._metadata.ping', return_value=False)
+@mock.patch(
+ 'google.auth.compute_engine._metadata.ping', return_value=False,
+ autospec=True)
def test__get_gce_credentials_no_ping(ping_mock):
credentials, project_id = _default._get_gce_credentials()
@@ -232,10 +239,11 @@
@mock.patch(
- 'google.auth.compute_engine._metadata.ping', return_value=True)
+ 'google.auth.compute_engine._metadata.ping', return_value=True,
+ autospec=True)
@mock.patch(
'google.auth.compute_engine._metadata.get_project_id',
- side_effect=exceptions.TransportError())
+ side_effect=exceptions.TransportError(), autospec=True)
def test__get_gce_credentials_no_project_id(get_mock, ping_mock):
credentials, project_id = _default._get_gce_credentials()
@@ -243,7 +251,9 @@
assert project_id is None
-@mock.patch('google.auth.compute_engine._metadata.ping', return_value=False)
+@mock.patch(
+ 'google.auth.compute_engine._metadata.ping', return_value=False,
+ autospec=True)
def test__get_gce_credentials_explicit_request(ping_mock):
_default._get_gce_credentials(mock.sentinel.request)
ping_mock.assert_called_with(request=mock.sentinel.request)
@@ -251,7 +261,8 @@
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
- return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
+ return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
+ autospec=True)
def test_default_early_out(get_mock):
assert _default.default() == (
mock.sentinel.credentials, mock.sentinel.project_id)
@@ -259,7 +270,8 @@
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
- return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
+ return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
+ autospec=True)
def test_default_explict_project_id(get_mock, monkeypatch):
monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
assert _default.default() == (
@@ -268,7 +280,8 @@
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
- return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
+ return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
+ autospec=True)
def test_default_explict_legacy_project_id(get_mock, monkeypatch):
monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
assert _default.default() == (
@@ -277,16 +290,16 @@
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
- return_value=(None, None))
+ return_value=(None, None), autospec=True)
@mock.patch(
'google.auth._default._get_gcloud_sdk_credentials',
- return_value=(None, None))
+ return_value=(None, None), autospec=True)
@mock.patch(
'google.auth._default._get_gae_credentials',
- return_value=(None, None))
+ return_value=(None, None), autospec=True)
@mock.patch(
'google.auth._default._get_gce_credentials',
- return_value=(None, None))
+ return_value=(None, None), autospec=True)
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
with pytest.raises(exceptions.DefaultCredentialsError):
assert _default.default()
@@ -294,9 +307,10 @@
@mock.patch(
'google.auth._default._get_explicit_environ_credentials',
- return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
+ return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
+ autospec=True)
@mock.patch(
- 'google.auth.credentials.with_scopes_if_required')
+ 'google.auth.credentials.with_scopes_if_required', autospec=True)
def test_default_scoped(with_scopes_mock, get_mock):
scopes = ['one', 'two']
diff --git a/tests/test_crypt.py b/tests/test_crypt.py
index 9069216..33105e4 100644
--- a/tests/test_crypt.py
+++ b/tests/test_crypt.py
@@ -131,8 +131,11 @@
def test_from_string_pub_cert_failure(self):
cert_bytes = PUBLIC_CERT_BYTES
true_der = rsa.pem.load_pem(cert_bytes, 'CERTIFICATE')
- with mock.patch('rsa.pem.load_pem',
- return_value=true_der + b'extra') as load_pem:
+ load_pem_patch = mock.patch(
+ 'rsa.pem.load_pem', return_value=true_der + b'extra',
+ autospec=True)
+
+ with load_pem_patch as load_pem:
with pytest.raises(ValueError):
crypt.Verifier.from_string(cert_bytes)
load_pem.assert_called_once_with(cert_bytes, 'CERTIFICATE')
@@ -161,13 +164,17 @@
six.StringIO(_helpers.from_bytes(key_bytes)),
crypt._PKCS8_MARKER)
- with mock.patch('pyasn1.codec.der.decoder.decode') as mock_decode:
- key_info, remaining = None, 'extra'
- mock_decode.return_value = (key_info, remaining)
+ key_info, remaining = None, 'extra'
+ decode_patch = mock.patch(
+ 'pyasn1.codec.der.decoder.decode',
+ return_value=(key_info, remaining),
+ autospec=True)
+
+ with decode_patch as decode:
with pytest.raises(ValueError):
crypt.Signer.from_string(key_bytes)
# Verify mock was called.
- mock_decode.assert_called_once_with(
+ decode.assert_called_once_with(
pem_bytes, asn1Spec=crypt._PKCS8_SPEC)
def test_from_string_pkcs8_unicode(self):
diff --git a/tests/transport/test_grpc.py b/tests/transport/test_grpc.py
index 5faa807..84e4b56 100644
--- a/tests/transport/test_grpc.py
+++ b/tests/transport/test_grpc.py
@@ -70,10 +70,10 @@
[('authorization', 'Bearer {}'.format(credentials.token))], None)
-@mock.patch('grpc.composite_channel_credentials')
-@mock.patch('grpc.metadata_call_credentials')
-@mock.patch('grpc.ssl_channel_credentials')
-@mock.patch('grpc.secure_channel')
+@mock.patch('grpc.composite_channel_credentials', autospec=True)
+@mock.patch('grpc.metadata_call_credentials', autospec=True)
+@mock.patch('grpc.ssl_channel_credentials', autospec=True)
+@mock.patch('grpc.secure_channel', autospec=True)
def test_secure_authorized_channel(
secure_channel, ssl_channel_credentials, metadata_call_credentials,
composite_channel_credentials):
@@ -105,10 +105,10 @@
assert channel == secure_channel.return_value
-@mock.patch('grpc.composite_channel_credentials')
-@mock.patch('grpc.metadata_call_credentials')
-@mock.patch('grpc.ssl_channel_credentials')
-@mock.patch('grpc.secure_channel')
+@mock.patch('grpc.composite_channel_credentials', autospec=True)
+@mock.patch('grpc.metadata_call_credentials', autospec=True)
+@mock.patch('grpc.ssl_channel_credentials', autospec=True)
+@mock.patch('grpc.secure_channel', autospec=True)
def test_secure_authorized_channel_explicit_ssl(
secure_channel, ssl_channel_credentials, metadata_call_credentials,
composite_channel_credentials):