Skip to content

Commit e16d70b

Browse files
authored
test: Add tests to cover deprecation and suppression (#1817)
* test: Add tests to covere deprecation and suppression * lint * blacken
1 parent ebf3363 commit e16d70b

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

tests/test__default.py

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import json
1616
import os
17+
import warnings
1718

1819
import mock
1920
import pytest # type: ignore
@@ -398,7 +399,6 @@ def test_load_credentials_from_file_impersonated_passing_scopes():
398399

399400

400401
def test_load_credentials_from_file_impersonated_wrong_target_principal(tmpdir):
401-
402402
with open(IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE) as fh:
403403
impersonated_credentials_info = json.load(fh)
404404
impersonated_credentials_info[
@@ -414,7 +414,6 @@ def test_load_credentials_from_file_impersonated_wrong_target_principal(tmpdir):
414414

415415

416416
def test_load_credentials_from_file_impersonated_wrong_source_type(tmpdir):
417-
418417
with open(IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE) as fh:
419418
impersonated_credentials_info = json.load(fh)
420419
impersonated_credentials_info["source_credentials"]["type"] = "external_account"
@@ -1325,7 +1324,7 @@ def test_default_impersonated_service_account_set_default_scopes(get_adc_path):
13251324
"google.auth._cloud_sdk.get_application_default_credentials_path", autospec=True
13261325
)
13271326
def test_default_impersonated_service_account_set_both_scopes_and_default_scopes(
1328-
get_adc_path
1327+
get_adc_path,
13291328
):
13301329
get_adc_path.return_value = IMPERSONATED_SERVICE_ACCOUNT_AUTHORIZED_USER_SOURCE_FILE
13311330
scopes = ["scope1", "scope2"]
@@ -1410,3 +1409,54 @@ def test_quota_gce_credentials(unused_get, unused_ping):
14101409
quota_project_id=explicit_quota
14111410
)
14121411
assert credentials.quota_project_id == explicit_quota
1412+
1413+
1414+
def test_load_credentials_from_file_deprecation_warning():
1415+
with pytest.warns(
1416+
DeprecationWarning, match="The load_credentials_from_file method is deprecated"
1417+
):
1418+
_default.load_credentials_from_file(SERVICE_ACCOUNT_FILE)
1419+
1420+
1421+
def test_load_credentials_from_dict_deprecation_warning():
1422+
with pytest.warns(
1423+
DeprecationWarning, match="The load_credentials_from_dict method is deprecated"
1424+
):
1425+
_default.load_credentials_from_dict(SERVICE_ACCOUNT_FILE_DATA)
1426+
1427+
1428+
@mock.patch("google.auth._cloud_sdk.get_project_id", return_value=None, autospec=True)
1429+
@mock.patch("os.path.isfile", return_value=True, autospec=True)
1430+
@mock.patch(
1431+
"google.auth._cloud_sdk.get_application_default_credentials_path",
1432+
return_value=SERVICE_ACCOUNT_FILE,
1433+
autospec=True,
1434+
)
1435+
def test_get_gcloud_sdk_credentials_suppresses_deprecation_warning(
1436+
get_adc_path, isfile, get_project_id
1437+
):
1438+
with warnings.catch_warnings(record=True) as caught_warnings:
1439+
warnings.simplefilter("always")
1440+
1441+
_default._get_gcloud_sdk_credentials()
1442+
1443+
assert not any(
1444+
isinstance(w.message, DeprecationWarning)
1445+
and "load_credentials_from_file" in str(w.message)
1446+
for w in caught_warnings
1447+
)
1448+
1449+
1450+
def test_get_explicit_environ_credentials_suppresses_deprecation_warning(monkeypatch):
1451+
monkeypatch.setenv(environment_vars.CREDENTIALS, SERVICE_ACCOUNT_FILE)
1452+
1453+
with warnings.catch_warnings(record=True) as caught_warnings:
1454+
warnings.simplefilter("always")
1455+
1456+
_default._get_explicit_environ_credentials()
1457+
1458+
assert not any(
1459+
isinstance(w.message, DeprecationWarning)
1460+
and "load_credentials_from_file" in str(w.message)
1461+
for w in caught_warnings
1462+
)

0 commit comments

Comments
 (0)