|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import os |
| 16 | +from unittest import mock |
| 17 | + |
15 | 18 | import pytest |
| 19 | +from google.auth.exceptions import MutualTLSChannelError |
16 | 20 |
|
17 | 21 | try: |
18 | 22 | import grpc # noqa: F401 |
@@ -91,3 +95,92 @@ def test_create_method_configs(): |
91 | 95 | retry, timeout = method_configs["Plain"] |
92 | 96 | assert retry is None |
93 | 97 | assert timeout._timeout == 30.0 |
| 98 | + |
| 99 | + |
| 100 | +def test_use_client_cert_effective_true(): |
| 101 | + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) |
| 102 | + mock_mtls.should_use_client_cert.return_value = True |
| 103 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 104 | + assert config.use_client_cert_effective() is True |
| 105 | + |
| 106 | + |
| 107 | +def test_use_client_cert_effective_false(): |
| 108 | + mock_mtls = mock.Mock(spec=["should_use_client_cert"]) |
| 109 | + mock_mtls.should_use_client_cert.return_value = False |
| 110 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 111 | + assert config.use_client_cert_effective() is False |
| 112 | + |
| 113 | + |
| 114 | +def test_use_client_cert_effective_fallback_env_true(): |
| 115 | + mock_mtls = mock.Mock(spec=[]) |
| 116 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 117 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "true"}): |
| 118 | + assert config.use_client_cert_effective() is True |
| 119 | + |
| 120 | + |
| 121 | +def test_use_client_cert_effective_fallback_env_false(): |
| 122 | + mock_mtls = mock.Mock(spec=[]) |
| 123 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 124 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "false"}): |
| 125 | + assert config.use_client_cert_effective() is False |
| 126 | + |
| 127 | + |
| 128 | +def test_use_client_cert_effective_fallback_env_invalid(): |
| 129 | + mock_mtls = mock.Mock(spec=[]) |
| 130 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 131 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_CLIENT_CERTIFICATE": "invalid"}): |
| 132 | + with pytest.raises( |
| 133 | + ValueError, |
| 134 | + match="Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` must be either `true` or `false`", |
| 135 | + ): |
| 136 | + config.use_client_cert_effective() |
| 137 | + |
| 138 | + |
| 139 | +def test_get_client_cert_source_provided(): |
| 140 | + source = mock.Mock() |
| 141 | + assert config.get_client_cert_source(source, True) == source |
| 142 | + |
| 143 | + |
| 144 | +def test_get_client_cert_source_default(): |
| 145 | + mock_mtls = mock.Mock(spec=["has_default_client_cert_source", "default_client_cert_source"]) |
| 146 | + mock_mtls.has_default_client_cert_source.return_value = True |
| 147 | + mock_source = mock.Mock() |
| 148 | + mock_mtls.default_client_cert_source.return_value = mock_source |
| 149 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 150 | + assert config.get_client_cert_source(None, True) == mock_source |
| 151 | + |
| 152 | + |
| 153 | +def test_get_client_cert_source_none(): |
| 154 | + mock_mtls = mock.Mock(spec=["has_default_client_cert_source", "default_client_cert_source"]) |
| 155 | + mock_mtls.has_default_client_cert_source.return_value = False |
| 156 | + with mock.patch("google.api_core.gapic_v1.config.mtls", mock_mtls): |
| 157 | + with pytest.raises( |
| 158 | + ValueError, |
| 159 | + match="Client certificate is required for mTLS, but no client certificate source was provided or found.", |
| 160 | + ): |
| 161 | + config.get_client_cert_source(None, True) |
| 162 | + |
| 163 | + |
| 164 | +def test_get_client_cert_source_use_cert_flag_false(): |
| 165 | + assert config.get_client_cert_source(None, False) is None |
| 166 | + source = mock.Mock() |
| 167 | + assert config.get_client_cert_source(source, False) is None |
| 168 | + |
| 169 | + |
| 170 | +def test_read_environment_variables(): |
| 171 | + with mock.patch("google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True): |
| 172 | + with mock.patch.dict( |
| 173 | + os.environ, |
| 174 | + {"GOOGLE_API_USE_MTLS_ENDPOINT": "always", "GOOGLE_CLOUD_UNIVERSE_DOMAIN": "my-universe.com"} |
| 175 | + ): |
| 176 | + use_cert, use_mtls, universe = config.read_environment_variables() |
| 177 | + assert use_cert is True |
| 178 | + assert use_mtls == "always" |
| 179 | + assert universe == "my-universe.com" |
| 180 | + |
| 181 | + |
| 182 | +def test_read_environment_variables_invalid_mtls(): |
| 183 | + with mock.patch("google.api_core.gapic_v1.config.use_client_cert_effective", return_value=True): |
| 184 | + with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}): |
| 185 | + with pytest.raises(MutualTLSChannelError): |
| 186 | + config.read_environment_variables() |
0 commit comments