Skip to content

Commit bd510e5

Browse files
committed
Merge branch 'feat/gapic-centralization-api-core-mtls' into feat/gapic-generator-centralization-mtls
2 parents bbcc340 + d90149a commit bd510e5

2 files changed

Lines changed: 110 additions & 76 deletions

File tree

packages/google-api-core/tests/unit/gapic/test_client_cert.py

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,62 +18,90 @@
1818

1919
import pytest
2020

21-
21+
from google.auth.transport import mtls
2222
from google.api_core.gapic_v1.client_cert import (
2323
get_client_cert_source,
2424
use_client_cert_effective,
2525
)
2626

2727

28-
@mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True)
29-
def test_use_client_cert_effective_with_google_auth(mock_method):
30-
# Test when google-auth supports the method
31-
mock_method.return_value = True
32-
assert use_client_cert_effective() is True
33-
34-
mock_method.return_value = False
35-
assert use_client_cert_effective() is False
36-
37-
38-
@mock.patch.dict(os.environ, {}, clear=True)
39-
def test_use_client_cert_effective_fallback():
40-
# We must patch hasattr to simulate google-auth lacking the method
41-
with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", return_value=False):
42-
# Default is false
43-
assert use_client_cert_effective() is False
44-
45-
env_true = {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}
46-
with mock.patch.dict(os.environ, env_true):
47-
assert use_client_cert_effective() is True
48-
49-
with mock.patch.dict(
50-
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}
51-
):
52-
assert use_client_cert_effective() is False
53-
54-
with mock.patch.dict(
55-
os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}
56-
):
57-
match_str = "must be either `true` or `false`"
58-
with pytest.raises(ValueError, match=match_str):
59-
use_client_cert_effective()
60-
61-
62-
@mock.patch(
63-
"google.auth.transport.mtls.has_default_client_cert_source", create=True
64-
) # noqa: E501
65-
@mock.patch(
66-
"google.auth.transport.mtls.default_client_cert_source", create=True
67-
) # noqa: E501
68-
def test_get_client_cert_source(mock_default, mock_has_default):
69-
mock_default.return_value = b"default_cert"
70-
mock_has_default.return_value = True
71-
72-
# When use_cert_flag is False, return None
73-
assert get_client_cert_source(b"provided", False) is None
74-
75-
# When provided_cert_source is given, return provided
76-
assert get_client_cert_source(b"provided", True) == b"provided" # noqa: E501
77-
78-
# When no provided cert but default is available
79-
assert get_client_cert_source(None, True) == b"default_cert"
28+
@pytest.mark.parametrize(
29+
"has_method, method_val, env_val, expected",
30+
[
31+
# should_use_client_cert is available cases
32+
(True, True, None, "true"),
33+
(True, False, None, "false"),
34+
(True, False, "unsupported", "false"),
35+
# should_use_client_cert is unavailable cases
36+
(False, None, "true", "true"),
37+
(False, None, "false", "false"),
38+
(False, None, "True", "true"),
39+
(False, None, "False", "false"),
40+
(False, None, "TRUE", "true"),
41+
(False, None, "FALSE", "false"),
42+
(False, None, None, "false"),
43+
(False, None, "unsupported", "value_error"),
44+
],
45+
ids=[
46+
"google_auth_true",
47+
"google_auth_false",
48+
"google_auth_false_env_unsupported",
49+
"fallback_env_true_lowercase",
50+
"fallback_env_false_lowercase",
51+
"fallback_env_true_titlecase",
52+
"fallback_env_false_titlecase",
53+
"fallback_env_true_uppercase",
54+
"fallback_env_false_uppercase",
55+
"fallback_env_unset",
56+
"fallback_env_unsupported",
57+
]
58+
)
59+
def test_use_client_cert_effective(has_method, method_val, env_val, expected):
60+
# Mock hasattr to control whether should_use_client_cert exists
61+
original_hasattr = hasattr
62+
def custom_hasattr(obj, name):
63+
if obj is mtls and name == "should_use_client_cert":
64+
return has_method
65+
return original_hasattr(obj, name)
66+
67+
with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", custom_hasattr):
68+
with mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True, return_value=method_val):
69+
env = {}
70+
if env_val is not None:
71+
env["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = env_val
72+
with mock.patch.dict(os.environ, env, clear=True):
73+
if expected == "value_error":
74+
with pytest.raises(ValueError, match="must be either `true` or `false`"):
75+
use_client_cert_effective()
76+
else:
77+
assert use_client_cert_effective() is (expected == "true")
78+
79+
80+
@pytest.mark.parametrize(
81+
"provided, use_cert, has_default_avail, default_val, expected",
82+
[
83+
(None, False, True, b"default", None),
84+
(b"provided", False, True, b"default", None),
85+
(b"provided", True, True, b"default", b"provided"),
86+
(None, True, True, b"default", b"default"),
87+
(None, True, False, b"default", None),
88+
],
89+
ids=[
90+
"use_cert_false_no_provided",
91+
"use_cert_false_with_provided",
92+
"use_cert_true_with_provided",
93+
"use_cert_true_no_provided_default_avail",
94+
"use_cert_true_no_provided_default_unavail",
95+
]
96+
)
97+
def test_get_client_cert_source(provided, use_cert, has_default_avail, default_val, expected):
98+
original_hasattr = hasattr
99+
def custom_hasattr(obj, name):
100+
if obj is mtls and name == "has_default_client_cert_source":
101+
return has_default_avail
102+
return original_hasattr(obj, name)
103+
104+
with mock.patch("google.api_core.gapic_v1.client_cert.hasattr", custom_hasattr):
105+
with mock.patch("google.auth.transport.mtls.has_default_client_cert_source", create=True, return_value=has_default_avail):
106+
with mock.patch("google.auth.transport.mtls.default_client_cert_source", create=True, return_value=default_val):
107+
assert get_client_cert_source(provided, use_cert) == expected

packages/google-api-core/tests/unit/gapic/test_config_helpers.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,35 @@
1919
import pytest
2020

2121
from google.auth.exceptions import MutualTLSChannelError
22-
2322
from google.api_core.gapic_v1.config_helpers import read_environment_variables
2423

2524

26-
@mock.patch(
27-
"google.api_core.gapic_v1.config_helpers.use_client_cert_effective"
28-
) # noqa: E501
29-
@mock.patch.dict(os.environ, clear=True)
30-
def test_read_environment_variables(mock_effective):
31-
mock_effective.return_value = True
32-
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "always"
33-
os.environ["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] = "custom.com"
34-
35-
cert, mtls, domain = read_environment_variables()
36-
assert cert is True
37-
assert mtls == "always"
38-
assert domain == "custom.com"
39-
40-
41-
@mock.patch.dict(os.environ, clear=True)
42-
def test_read_environment_variables_invalid_mtls():
43-
os.environ["GOOGLE_API_USE_MTLS_ENDPOINT"] = "invalid"
44-
with pytest.raises(
45-
MutualTLSChannelError, match="must be `never`, `auto` or `always`"
46-
):
47-
read_environment_variables()
25+
@pytest.mark.parametrize(
26+
"env, mock_cert_val, expected",
27+
[
28+
({}, False, (False, "auto", None)),
29+
({}, True, (True, "auto", None)),
30+
({"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}, False, (False, "never", None)),
31+
({"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}, False, (False, "always", None)),
32+
({"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}, False, (False, "auto", None)),
33+
({"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}, False, "mutual_tls_error"),
34+
({"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}, False, (False, "auto", "foo.com")),
35+
],
36+
ids=[
37+
"default_env",
38+
"client_cert_true",
39+
"mtls_never",
40+
"mtls_always",
41+
"mtls_auto",
42+
"mtls_invalid",
43+
"universe_domain",
44+
]
45+
)
46+
def test_read_environment_variables(env, mock_cert_val, expected):
47+
with mock.patch("google.api_core.gapic_v1.config_helpers.use_client_cert_effective", return_value=mock_cert_val):
48+
with mock.patch.dict(os.environ, env, clear=True):
49+
if expected == "mutual_tls_error":
50+
with pytest.raises(MutualTLSChannelError, match="must be `never`, `auto` or `always`"):
51+
read_environment_variables()
52+
else:
53+
assert read_environment_variables() == expected

0 commit comments

Comments
 (0)