|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import os |
| 17 | +from unittest import mock |
| 18 | +import pytest |
| 19 | + |
| 20 | +from google.api_core.gapic_v1 import client_helpers |
| 21 | +from google.auth.exceptions import MutualTLSChannelError |
| 22 | + |
| 23 | + |
| 24 | +def test__get_default_mtls_endpoint(): |
| 25 | + # Test valid API endpoints |
| 26 | + assert client_helpers._get_default_mtls_endpoint("foo.googleapis.com") == "foo.mtls.googleapis.com" |
| 27 | + assert client_helpers._get_default_mtls_endpoint("foo.sandbox.googleapis.com") == "foo.mtls.sandbox.googleapis.com" |
| 28 | + |
| 29 | + # Test endpoints that shouldn't be converted |
| 30 | + assert client_helpers._get_default_mtls_endpoint("foo.mtls.googleapis.com") == "foo.mtls.googleapis.com" |
| 31 | + assert client_helpers._get_default_mtls_endpoint("foo.com") == "foo.com" |
| 32 | + |
| 33 | + # Test empty/None endpoints |
| 34 | + assert client_helpers._get_default_mtls_endpoint("") == "" |
| 35 | + assert client_helpers._get_default_mtls_endpoint(None) is None |
| 36 | + |
| 37 | + |
| 38 | +@mock.patch("google.auth.transport.mtls.should_use_client_cert", autospec=True) |
| 39 | +def test__use_client_cert_effective_with_google_auth(mock_should_use_client_cert): |
| 40 | + # Test when google-auth supports the method |
| 41 | + mock_should_use_client_cert.return_value = True |
| 42 | + assert client_helpers._use_client_cert_effective() is True |
| 43 | + |
| 44 | + mock_should_use_client_cert.return_value = False |
| 45 | + assert client_helpers._use_client_cert_effective() is False |
| 46 | + |
| 47 | + |
| 48 | +@mock.patch.dict(os.environ, {}, clear=True) |
| 49 | +def test__use_client_cert_effective_fallback(): |
| 50 | + # We must patch hasattr to simulate google-auth lacking the method |
| 51 | + with mock.patch("google.api_core.gapic_v1.client_helpers.hasattr", return_value=False): |
| 52 | + # Default is false |
| 53 | + assert client_helpers._use_client_cert_effective() is False |
| 54 | + |
| 55 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): |
| 56 | + assert client_helpers._use_client_cert_effective() is True |
| 57 | + |
| 58 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): |
| 59 | + assert client_helpers._use_client_cert_effective() is False |
| 60 | + |
| 61 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): |
| 62 | + with pytest.raises(ValueError, match="must be either `true` or `false`"): |
| 63 | + client_helpers._use_client_cert_effective() |
| 64 | + |
| 65 | + |
| 66 | +def test__get_api_endpoint_override(): |
| 67 | + # If api_override is provided, it should be returned regardless of other args |
| 68 | + endpoint = client_helpers._get_api_endpoint( |
| 69 | + api_override="custom.endpoint.com", |
| 70 | + client_cert_source=None, |
| 71 | + universe_domain="googleapis.com", |
| 72 | + use_mtls_endpoint="auto", |
| 73 | + default_universe="googleapis.com", |
| 74 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 75 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 76 | + ) |
| 77 | + assert endpoint == "custom.endpoint.com" |
| 78 | + |
| 79 | + |
| 80 | +def test__get_api_endpoint_mtls_always(): |
| 81 | + # use_mtls_endpoint == "always" should use the default mtls endpoint |
| 82 | + endpoint = client_helpers._get_api_endpoint( |
| 83 | + api_override=None, |
| 84 | + client_cert_source=None, |
| 85 | + universe_domain="googleapis.com", |
| 86 | + use_mtls_endpoint="always", |
| 87 | + default_universe="googleapis.com", |
| 88 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 89 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 90 | + ) |
| 91 | + assert endpoint == "foo.mtls.googleapis.com" |
| 92 | + |
| 93 | + |
| 94 | +def test__get_api_endpoint_mtls_auto_with_cert(): |
| 95 | + # "auto" with client_cert_source should use mtls |
| 96 | + endpoint = client_helpers._get_api_endpoint( |
| 97 | + api_override=None, |
| 98 | + client_cert_source=mock.Mock(), |
| 99 | + universe_domain="googleapis.com", |
| 100 | + use_mtls_endpoint="auto", |
| 101 | + default_universe="googleapis.com", |
| 102 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 103 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 104 | + ) |
| 105 | + assert endpoint == "foo.mtls.googleapis.com" |
| 106 | + |
| 107 | + |
| 108 | +def test__get_api_endpoint_mtls_auto_no_cert(): |
| 109 | + # "auto" without client_cert_source should use the default template |
| 110 | + endpoint = client_helpers._get_api_endpoint( |
| 111 | + api_override=None, |
| 112 | + client_cert_source=None, |
| 113 | + universe_domain="googleapis.com", |
| 114 | + use_mtls_endpoint="auto", |
| 115 | + default_universe="googleapis.com", |
| 116 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 117 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 118 | + ) |
| 119 | + assert endpoint == "foo.googleapis.com" |
| 120 | + |
| 121 | + |
| 122 | +def test__get_api_endpoint_mtls_universe_mismatch(): |
| 123 | + # mTLS is only supported in the default universe |
| 124 | + with pytest.raises(MutualTLSChannelError, match="mTLS is not supported"): |
| 125 | + client_helpers._get_api_endpoint( |
| 126 | + api_override=None, |
| 127 | + client_cert_source=mock.Mock(), |
| 128 | + universe_domain="custom-universe.com", |
| 129 | + use_mtls_endpoint="auto", |
| 130 | + default_universe="googleapis.com", |
| 131 | + default_mtls_endpoint="foo.mtls.googleapis.com", |
| 132 | + default_endpoint_template="foo.{UNIVERSE_DOMAIN}", |
| 133 | + ) |
0 commit comments