@@ -102,14 +102,20 @@ from google.iam.v1 import policy_pb2 # type: ignore
102102
103103
104104@pytest.fixture(autouse=True)
105- def mock_should_use_client_cert():
105+ def mock_should_use_client_cert(request ):
106106 if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
107- with mock.patch("google.auth.transport. mtls.should_use_client_cert", return_value=False) :
107+ if " mtls" in request.node.name or "client_cert" in request.node.name :
108108 yield
109+ else:
110+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False):
111+ yield
109112 else:
110113 yield
111114
112115
116+
117+
118+
113119CRED_INFO_JSON = {
114120 "credential_source": "/path/to/file",
115121 "credential_type": "service account credentials",
@@ -678,18 +684,22 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp
678684 with mock.patch.object(transport_class, '__init__') as patched:
679685 with mock.patch("google.auth.transport.mtls.has_default_client_cert_source", return_value=False):
680686 patched.return_value = None
681- client = client_class(transport=transport_name)
682- patched.assert_called_once_with(
683- credentials=None,
684- credentials_file=None,
685- host=client._DEFAULT_ENDPOINT_TEMPLATE.format(UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE),
686- scopes=None,
687- client_cert_source_for_mtls=None,
688- quota_project_id=None,
689- client_info=transports.base.DEFAULT_CLIENT_INFO,
690- always_use_jwt_access=True,
691- api_audience=None,
692- )
687+ if use_client_cert_env == "true":
688+ with pytest.raises(ValueError):
689+ client = client_class(transport=transport_name)
690+ else:
691+ client = client_class(transport=transport_name)
692+ patched.assert_called_once_with(
693+ credentials=None,
694+ credentials_file=None,
695+ host=client._DEFAULT_ENDPOINT_TEMPLATE.format(UNIVERSE_DOMAIN=client._DEFAULT_UNIVERSE),
696+ scopes=None,
697+ client_cert_source_for_mtls=None,
698+ quota_project_id=None,
699+ client_info=transports.base.DEFAULT_CLIENT_INFO,
700+ always_use_jwt_access=True,
701+ api_audience=None,
702+ )
693703
694704
695705def test_use_client_cert_effective():
@@ -766,15 +776,17 @@ def test_use_client_cert_effective():
766776 # The method should return False as the environment variable is set to an invalid value.
767777 if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
768778 with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}):
769- assert {{ service.client_name }}._use_client_cert_effective() is False
779+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False):
780+ assert {{ service.client_name }}._use_client_cert_effective() is False
770781
771782 # Test case 12: Test when `should_use_client_cert` is available and the
772783 # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also,
773784 # the GOOGLE_API_CONFIG environment variable is unset.
774785 if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
775786 with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}):
776787 with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}):
777- assert {{ service.client_name }}._use_client_cert_effective() is False
788+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False):
789+ assert {{ service.client_name }}._use_client_cert_effective() is False
778790
779791
780792def test__get_client_cert_source():
@@ -807,23 +819,25 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
807819
808820 # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
809821 with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
810- mock_api_endpoint = "foo"
811- options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
812- api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
813- assert api_endpoint == mock_api_endpoint
814- assert cert_source == mock_client_cert_source
822+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True, return_value=True):
823+ mock_api_endpoint = "foo"
824+ options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
825+ api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
826+ assert api_endpoint == mock_api_endpoint
827+ assert cert_source == mock_client_cert_source
815828
816829 # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
817- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
818- mock_client_cert_source = mock.Mock()
819- mock_api_endpoint = "foo"
820- options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
821- api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
822- assert api_endpoint == mock_api_endpoint
823- assert cert_source is None
830+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false"}):
831+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", create=True, return_value=False):
832+ mock_client_cert_source = mock.Mock()
833+ mock_api_endpoint = "foo"
834+ options = client_options.ClientOptions(client_cert_source=mock_client_cert_source, api_endpoint=mock_api_endpoint)
835+ api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source(options)
836+ assert api_endpoint == mock_api_endpoint
837+ assert cert_source is None
824838
825839 # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "Unsupported".
826- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported"}):
840+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "Unsupported", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false" }):
827841 if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
828842 mock_client_cert_source = mock.Mock()
829843 mock_api_endpoint = "foo"
@@ -864,6 +878,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
864878 for config_data, expected_cert_source in test_cases:
865879 env = os.environ.copy()
866880 env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", None)
881+ env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", None)
867882 with mock.patch.dict(os.environ, env, clear=True):
868883 config_filename = "mock_certificate_config.json"
869884 config_file_content = json.dumps(config_data)
@@ -911,6 +926,7 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
911926 for config_data, expected_cert_source in test_cases:
912927 env = os.environ.copy()
913928 env.pop("GOOGLE_API_USE_CLIENT_CERTIFICATE", "")
929+ env.pop("CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE", "")
914930 with mock.patch.dict(os.environ, env, clear=True):
915931 config_filename = "mock_certificate_config.json"
916932 config_file_content = json.dumps(config_data)
@@ -931,26 +947,26 @@ def test_{{ service.client_name|snake_case }}_get_mtls_endpoint_and_cert_source(
931947 assert cert_source is expected_cert_source
932948
933949 # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
934- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}):
950+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "never", "GOOGLE_API_USE_CLIENT_CERTIFICATE": "false", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false" }):
935951 api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
936952 assert api_endpoint == client_class.DEFAULT_ENDPOINT
937953 assert cert_source is None
938954
939955 # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
940- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}):
956+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_API_USE_CLIENT_CERTIFICATE": "false", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false" }):
941957 api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
942958 assert api_endpoint == client_class.DEFAULT_MTLS_ENDPOINT
943959 assert cert_source is None
944960
945961 # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
946- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
962+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false" }):
947963 with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
948964 api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
949965 assert api_endpoint == client_class.DEFAULT_ENDPOINT
950966 assert cert_source is None
951967
952968 # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
953- with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
969+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true", "CLOUDSDK_CONTEXT_AWARE_USE_CLIENT_CERTIFICATE": "false" }):
954970 with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
955971 with mock.patch('google.auth.transport.mtls.default_client_cert_source', return_value=mock_client_cert_source):
956972 api_endpoint, cert_source = client_class.get_mtls_endpoint_and_cert_source()
0 commit comments