@@ -101,6 +101,15 @@ from google.iam.v1 import policy_pb2 # type: ignore
101101{{ shared_macros.add_google_api_core_version_header_import(service.version) }}
102102
103103
104+ @pytest.fixture(autouse=True)
105+ def mock_should_use_client_cert():
106+ 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):
108+ yield
109+ else:
110+ yield
111+
112+
104113CRED_INFO_JSON = {
105114 "credential_source": "/path/to/file",
106115 "credential_type": "service account credentials",
@@ -683,6 +692,105 @@ def test_{{ service.client_name|snake_case }}_mtls_env_auto(client_class, transp
683692 )
684693
685694
695+ def test_use_client_cert_effective():
696+ # Test case 1: Test when `should_use_client_cert` returns True.
697+ # We mock the `should_use_client_cert` function to simulate a scenario where
698+ # the google-auth library supports automatic mTLS and determines that a
699+ # client certificate should be used.
700+ if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
701+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=True):
702+ assert {{ service.client_name }}._use_client_cert_effective() is True
703+
704+ # Test case 2: Test when `should_use_client_cert` returns False.
705+ # We mock the `should_use_client_cert` function to simulate a scenario where
706+ # the google-auth library supports automatic mTLS and determines that a
707+ # client certificate should NOT be used.
708+ if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
709+ with mock.patch("google.auth.transport.mtls.should_use_client_cert", return_value=False):
710+ assert {{ service.client_name }}._use_client_cert_effective() is False
711+
712+ # Test case 3: Test when `should_use_client_cert` is unavailable and the
713+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "true".
714+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
715+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}):
716+ assert {{ service.client_name }}._use_client_cert_effective() is True
717+
718+ # Test case 4: Test when `should_use_client_cert` is unavailable and the
719+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "false".
720+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
721+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}):
722+ assert {{ service.client_name }}._use_client_cert_effective() is False
723+
724+ # Test case 5: Test when `should_use_client_cert` is unavailable and the
725+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "True".
726+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
727+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "True"}):
728+ assert {{ service.client_name }}._use_client_cert_effective() is True
729+
730+ # Test case 6: Test when `should_use_client_cert` is unavailable and the
731+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "False".
732+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
733+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "False"}):
734+ assert {{ service.client_name }}._use_client_cert_effective() is False
735+
736+ # Test case 7: Test when `should_use_client_cert` is unavailable and the
737+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "TRUE".
738+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
739+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "TRUE"}):
740+ assert {{ service.client_name }}._use_client_cert_effective() is True
741+
742+ # Test case 8: Test when `should_use_client_cert` is unavailable and the
743+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to "FALSE".
744+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
745+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "FALSE"}):
746+ assert {{ service.client_name }}._use_client_cert_effective() is False
747+
748+ # Test case 9: Test when `should_use_client_cert` is unavailable and the
749+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is not set.
750+ # In this case, the method should return False, which is the default value.
751+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
752+ with mock.patch.dict(os.environ, clear=True):
753+ assert {{ service.client_name }}._use_client_cert_effective() is False
754+
755+ # Test case 10: Test when `should_use_client_cert` is unavailable and the
756+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value.
757+ # The method should raise a ValueError as the environment variable must be either
758+ # "true" or "false".
759+ if not hasattr(google.auth.transport.mtls, "should_use_client_cert"):
760+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}):
761+ with pytest.raises(ValueError):
762+ {{ service.client_name }}._use_client_cert_effective()
763+
764+ # Test case 11: Test when `should_use_client_cert` is available and the
765+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is set to an invalid value.
766+ # The method should return False as the environment variable is set to an invalid value.
767+ if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
768+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "unsupported"}):
769+ assert {{ service.client_name }}._use_client_cert_effective() is False
770+
771+ # Test case 12: Test when `should_use_client_cert` is available and the
772+ # `GOOGLE_API_USE_CLIENT_CERTIFICATE` environment variable is unset. Also,
773+ # the GOOGLE_API_CONFIG environment variable is unset.
774+ if hasattr(google.auth.transport.mtls, "should_use_client_cert"):
775+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": ""}):
776+ with mock.patch.dict(os.environ, {"GOOGLE_API_CERTIFICATE_CONFIG": ""}):
777+ assert {{ service.client_name }}._use_client_cert_effective() is False
778+
779+
780+ def test__get_client_cert_source():
781+ mock_provided_cert_source = mock.Mock()
782+ mock_default_cert_source = mock.Mock()
783+
784+ assert {{ service.client_name }}._get_client_cert_source(None, False) is None
785+ assert {{ service.client_name }}._get_client_cert_source(mock_provided_cert_source, False) is None
786+ assert {{ service.client_name }}._get_client_cert_source(mock_provided_cert_source, True) == mock_provided_cert_source
787+
788+ with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
789+ with mock.patch('google.auth.transport.mtls.default_client_cert_source', return_value=mock_default_cert_source):
790+ assert {{ service.client_name }}._get_client_cert_source(None, True) is mock_default_cert_source
791+ assert {{ service.client_name }}._get_client_cert_source(mock_provided_cert_source, "true") is mock_provided_cert_source
792+
793+
686794@pytest.mark.parametrize("client_class", [
687795 {% if 'grpc' in opts .transport %}
688796 {{ service.client_name }}, {{ service.async_client_name }}
0 commit comments