Skip to content

Commit 8784b23

Browse files
author
Jon Wayne Parrott
committed
Use autospec where appropriate
1 parent 8c3a10b commit 8784b23

File tree

7 files changed

+62
-40
lines changed

7 files changed

+62
-40
lines changed

tests/compute_engine/test_credentials.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_refresh_success(self, get_mock, now_mock):
6868
# expired)
6969
assert self.credentials.valid
7070

71-
@mock.patch('google.auth.compute_engine._metadata.get')
71+
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True)
7272
def test_refresh_error(self, get_mock):
7373
get_mock.side_effect = exceptions.TransportError('http error')
7474

@@ -77,7 +77,7 @@ def test_refresh_error(self, get_mock):
7777

7878
assert excinfo.match(r'http error')
7979

80-
@mock.patch('google.auth.compute_engine._metadata.get')
80+
@mock.patch('google.auth.compute_engine._metadata.get', autospec=True)
8181
def test_before_request_refreshes(self, get_mock):
8282
get_mock.side_effect = [{
8383
# First request is for sevice account info.

tests/oauth2/test_credentials.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_create_scoped(self):
4646
with pytest.raises(NotImplementedError):
4747
self.credentials.with_scopes(['email'])
4848

49-
@mock.patch('google.oauth2._client.refresh_grant')
49+
@mock.patch('google.oauth2._client.refresh_grant', autospec=True)
5050
@mock.patch(
5151
'google.auth._helpers.utcnow', return_value=datetime.datetime.min)
5252
def test_refresh_success(self, now_mock, refresh_grant_mock):

tests/oauth2/test_service_account.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test__make_authorization_grant_assertion_subject(self):
159159
payload = jwt.decode(token, PUBLIC_CERT_BYTES)
160160
assert payload['sub'] == subject
161161

162-
@mock.patch('google.oauth2._client.jwt_grant')
162+
@mock.patch('google.oauth2._client.jwt_grant', autospec=True)
163163
def test_refresh_success(self, jwt_grant_mock):
164164
token = 'token'
165165
jwt_grant_mock.return_value = (
@@ -185,7 +185,7 @@ def test_refresh_success(self, jwt_grant_mock):
185185
# expired)
186186
assert self.credentials.valid
187187

188-
@mock.patch('google.oauth2._client.jwt_grant')
188+
@mock.patch('google.oauth2._client.jwt_grant', autospec=True)
189189
def test_before_request_refreshes(self, jwt_grant_mock):
190190
token = 'token'
191191
jwt_grant_mock.return_value = (

tests/test__cloud_sdk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
with open(os.path.join(DATA_DIR, 'cloud_sdk.cfg')) as fh:
3939
CLOUD_SDK_CONFIG_DATA = fh.read()
4040

41-
CONFIG_PATH_PATCH = mock.patch('google.auth._cloud_sdk.get_config_path')
41+
CONFIG_PATH_PATCH = mock.patch(
42+
'google.auth._cloud_sdk.get_config_path', autospec=True)
4243

4344

4445
@pytest.fixture

tests/test__default.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
LOAD_FILE_PATCH = mock.patch(
4545
'google.auth._default._load_credentials_from_file', return_value=(
46-
mock.sentinel.credentials, mock.sentinel.project_id))
46+
mock.sentinel.credentials, mock.sentinel.project_id), autospec=True)
4747

4848

4949
def test__load_credentials_from_file_invalid_json(tmpdir):
@@ -131,7 +131,9 @@ def test__get_explicit_environ_credentials_no_project_id(
131131

132132

133133
@LOAD_FILE_PATCH
134-
@mock.patch('google.auth._cloud_sdk.get_application_default_credentials_path')
134+
@mock.patch(
135+
'google.auth._cloud_sdk.get_application_default_credentials_path',
136+
autospec=True)
135137
def test__get_gcloud_sdk_credentials(
136138
mock_get_adc_path, mock_load):
137139
mock_get_adc_path.return_value = SERVICE_ACCOUNT_FILE
@@ -143,7 +145,9 @@ def test__get_gcloud_sdk_credentials(
143145
mock_load.assert_called_with(SERVICE_ACCOUNT_FILE)
144146

145147

146-
@mock.patch('google.auth._cloud_sdk.get_application_default_credentials_path')
148+
@mock.patch(
149+
'google.auth._cloud_sdk.get_application_default_credentials_path',
150+
autospec=True)
147151
def test__get_gcloud_sdk_credentials_non_existent(mock_get_adc_path, tmpdir):
148152
non_existent = tmpdir.join('non-existent')
149153
mock_get_adc_path.return_value = str(non_existent)
@@ -156,7 +160,7 @@ def test__get_gcloud_sdk_credentials_non_existent(mock_get_adc_path, tmpdir):
156160

157161
@mock.patch(
158162
'google.auth._cloud_sdk.get_project_id',
159-
return_value=mock.sentinel.project_id)
163+
return_value=mock.sentinel.project_id, autospec=True)
160164
@mock.patch('os.path.isfile', return_value=True)
161165
@LOAD_FILE_PATCH
162166
def test__get_gcloud_sdk_credentials_project_id(
@@ -174,7 +178,7 @@ def test__get_gcloud_sdk_credentials_project_id(
174178

175179
@mock.patch(
176180
'google.auth._cloud_sdk.get_project_id',
177-
return_value=None)
181+
return_value=None, autospec=True)
178182
@mock.patch('os.path.isfile', return_value=True)
179183
@LOAD_FILE_PATCH
180184
def test__get_gcloud_sdk_credentials_no_project_id(
@@ -212,18 +216,21 @@ def test__get_gae_credentials_no_apis():
212216

213217

214218
@mock.patch(
215-
'google.auth.compute_engine._metadata.ping', return_value=True)
219+
'google.auth.compute_engine._metadata.ping', return_value=True,
220+
autospec=True)
216221
@mock.patch(
217222
'google.auth.compute_engine._metadata.get_project_id',
218-
return_value='example-project')
223+
return_value='example-project', autospec=True)
219224
def test__get_gce_credentials(get_mock, ping_mock):
220225
credentials, project_id = _default._get_gce_credentials()
221226

222227
assert isinstance(credentials, compute_engine.Credentials)
223228
assert project_id == 'example-project'
224229

225230

226-
@mock.patch('google.auth.compute_engine._metadata.ping', return_value=False)
231+
@mock.patch(
232+
'google.auth.compute_engine._metadata.ping', return_value=False,
233+
autospec=True)
227234
def test__get_gce_credentials_no_ping(ping_mock):
228235
credentials, project_id = _default._get_gce_credentials()
229236

@@ -232,34 +239,39 @@ def test__get_gce_credentials_no_ping(ping_mock):
232239

233240

234241
@mock.patch(
235-
'google.auth.compute_engine._metadata.ping', return_value=True)
242+
'google.auth.compute_engine._metadata.ping', return_value=True,
243+
autospec=True)
236244
@mock.patch(
237245
'google.auth.compute_engine._metadata.get_project_id',
238-
side_effect=exceptions.TransportError())
246+
side_effect=exceptions.TransportError(), autospec=True)
239247
def test__get_gce_credentials_no_project_id(get_mock, ping_mock):
240248
credentials, project_id = _default._get_gce_credentials()
241249

242250
assert isinstance(credentials, compute_engine.Credentials)
243251
assert project_id is None
244252

245253

246-
@mock.patch('google.auth.compute_engine._metadata.ping', return_value=False)
254+
@mock.patch(
255+
'google.auth.compute_engine._metadata.ping', return_value=False,
256+
autospec=True)
247257
def test__get_gce_credentials_explicit_request(ping_mock):
248258
_default._get_gce_credentials(mock.sentinel.request)
249259
ping_mock.assert_called_with(request=mock.sentinel.request)
250260

251261

252262
@mock.patch(
253263
'google.auth._default._get_explicit_environ_credentials',
254-
return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
264+
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
265+
autospec=True)
255266
def test_default_early_out(get_mock):
256267
assert _default.default() == (
257268
mock.sentinel.credentials, mock.sentinel.project_id)
258269

259270

260271
@mock.patch(
261272
'google.auth._default._get_explicit_environ_credentials',
262-
return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
273+
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
274+
autospec=True)
263275
def test_default_explict_project_id(get_mock, monkeypatch):
264276
monkeypatch.setenv(environment_vars.PROJECT, 'explicit-env')
265277
assert _default.default() == (
@@ -268,7 +280,8 @@ def test_default_explict_project_id(get_mock, monkeypatch):
268280

269281
@mock.patch(
270282
'google.auth._default._get_explicit_environ_credentials',
271-
return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
283+
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
284+
autospec=True)
272285
def test_default_explict_legacy_project_id(get_mock, monkeypatch):
273286
monkeypatch.setenv(environment_vars.LEGACY_PROJECT, 'explicit-env')
274287
assert _default.default() == (
@@ -277,26 +290,27 @@ def test_default_explict_legacy_project_id(get_mock, monkeypatch):
277290

278291
@mock.patch(
279292
'google.auth._default._get_explicit_environ_credentials',
280-
return_value=(None, None))
293+
return_value=(None, None), autospec=True)
281294
@mock.patch(
282295
'google.auth._default._get_gcloud_sdk_credentials',
283-
return_value=(None, None))
296+
return_value=(None, None), autospec=True)
284297
@mock.patch(
285298
'google.auth._default._get_gae_credentials',
286-
return_value=(None, None))
299+
return_value=(None, None), autospec=True)
287300
@mock.patch(
288301
'google.auth._default._get_gce_credentials',
289-
return_value=(None, None))
302+
return_value=(None, None), autospec=True)
290303
def test_default_fail(unused_gce, unused_gae, unused_sdk, unused_explicit):
291304
with pytest.raises(exceptions.DefaultCredentialsError):
292305
assert _default.default()
293306

294307

295308
@mock.patch(
296309
'google.auth._default._get_explicit_environ_credentials',
297-
return_value=(mock.sentinel.credentials, mock.sentinel.project_id))
310+
return_value=(mock.sentinel.credentials, mock.sentinel.project_id),
311+
autospec=True)
298312
@mock.patch(
299-
'google.auth.credentials.with_scopes_if_required')
313+
'google.auth.credentials.with_scopes_if_required', autospec=True)
300314
def test_default_scoped(with_scopes_mock, get_mock):
301315
scopes = ['one', 'two']
302316

tests/test_crypt.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ def test_from_string_pub_cert_unicode(self):
131131
def test_from_string_pub_cert_failure(self):
132132
cert_bytes = PUBLIC_CERT_BYTES
133133
true_der = rsa.pem.load_pem(cert_bytes, 'CERTIFICATE')
134-
with mock.patch('rsa.pem.load_pem',
135-
return_value=true_der + b'extra') as load_pem:
134+
load_pem_patch = mock.patch(
135+
'rsa.pem.load_pem', return_value=true_der + b'extra',
136+
autospec=True)
137+
138+
with load_pem_patch as load_pem:
136139
with pytest.raises(ValueError):
137140
crypt.Verifier.from_string(cert_bytes)
138141
load_pem.assert_called_once_with(cert_bytes, 'CERTIFICATE')
@@ -161,13 +164,17 @@ def test_from_string_pkcs8_extra_bytes(self):
161164
six.StringIO(_helpers.from_bytes(key_bytes)),
162165
crypt._PKCS8_MARKER)
163166

164-
with mock.patch('pyasn1.codec.der.decoder.decode') as mock_decode:
165-
key_info, remaining = None, 'extra'
166-
mock_decode.return_value = (key_info, remaining)
167+
key_info, remaining = None, 'extra'
168+
decode_patch = mock.patch(
169+
'pyasn1.codec.der.decoder.decode',
170+
return_value=(key_info, remaining),
171+
autospec=True)
172+
173+
with decode_patch as decode:
167174
with pytest.raises(ValueError):
168175
crypt.Signer.from_string(key_bytes)
169176
# Verify mock was called.
170-
mock_decode.assert_called_once_with(
177+
decode.assert_called_once_with(
171178
pem_bytes, asn1Spec=crypt._PKCS8_SPEC)
172179

173180
def test_from_string_pkcs8_unicode(self):

tests/transport/test_grpc.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def test_call_refresh(self):
7070
[('authorization', 'Bearer {}'.format(credentials.token))], None)
7171

7272

73-
@mock.patch('grpc.composite_channel_credentials')
74-
@mock.patch('grpc.metadata_call_credentials')
75-
@mock.patch('grpc.ssl_channel_credentials')
76-
@mock.patch('grpc.secure_channel')
73+
@mock.patch('grpc.composite_channel_credentials', autospec=True)
74+
@mock.patch('grpc.metadata_call_credentials', autospec=True)
75+
@mock.patch('grpc.ssl_channel_credentials', autospec=True)
76+
@mock.patch('grpc.secure_channel', autospec=True)
7777
def test_secure_authorized_channel(
7878
secure_channel, ssl_channel_credentials, metadata_call_credentials,
7979
composite_channel_credentials):
@@ -105,10 +105,10 @@ def test_secure_authorized_channel(
105105
assert channel == secure_channel.return_value
106106

107107

108-
@mock.patch('grpc.composite_channel_credentials')
109-
@mock.patch('grpc.metadata_call_credentials')
110-
@mock.patch('grpc.ssl_channel_credentials')
111-
@mock.patch('grpc.secure_channel')
108+
@mock.patch('grpc.composite_channel_credentials', autospec=True)
109+
@mock.patch('grpc.metadata_call_credentials', autospec=True)
110+
@mock.patch('grpc.ssl_channel_credentials', autospec=True)
111+
@mock.patch('grpc.secure_channel', autospec=True)
112112
def test_secure_authorized_channel_explicit_ssl(
113113
secure_channel, ssl_channel_credentials, metadata_call_credentials,
114114
composite_channel_credentials):

0 commit comments

Comments
 (0)