|
20 | 20 |
|
21 | 21 | from google.api_core.gapic_v1.client_utils import ( |
22 | 22 | get_api_endpoint, |
23 | | - get_client_cert_source, |
24 | 23 | get_default_mtls_endpoint, |
25 | 24 | get_universe_domain, |
26 | | - read_environment_variables, |
27 | | - use_client_cert_effective, |
28 | 25 | ) |
29 | 26 |
|
30 | 27 |
|
@@ -205,147 +202,4 @@ def test_get_universe_domain(): |
205 | 202 | assert str(excinfo.value) == "Universe Domain cannot be an empty string." |
206 | 203 |
|
207 | 204 |
|
208 | | -@pytest.mark.parametrize( |
209 | | - "has_method, method_val, env_val, expected", |
210 | | - [ |
211 | | - # should_use_client_cert is available cases |
212 | | - (True, True, None, "true"), |
213 | | - (True, False, None, "false"), |
214 | | - (True, False, "unsupported", "false"), |
215 | | - # should_use_client_cert is unavailable cases |
216 | | - (False, None, "true", "true"), |
217 | | - (False, None, "false", "false"), |
218 | | - (False, None, "True", "true"), |
219 | | - (False, None, "False", "false"), |
220 | | - (False, None, "TRUE", "true"), |
221 | | - (False, None, "FALSE", "false"), |
222 | | - (False, None, None, "false"), |
223 | | - (False, None, "unsupported", "value_error"), |
224 | | - ], |
225 | | - ids=[ |
226 | | - "google_auth_true", |
227 | | - "google_auth_false", |
228 | | - "google_auth_false_env_unsupported", |
229 | | - "fallback_env_true_lowercase", |
230 | | - "fallback_env_false_lowercase", |
231 | | - "fallback_env_true_titlecase", |
232 | | - "fallback_env_false_titlecase", |
233 | | - "fallback_env_true_uppercase", |
234 | | - "fallback_env_false_uppercase", |
235 | | - "fallback_env_unset", |
236 | | - "fallback_env_unsupported", |
237 | | - ], |
238 | | -) |
239 | | -def test_use_client_cert_effective(has_method, method_val, env_val, expected): |
240 | | - # Mock hasattr to control whether should_use_client_cert exists |
241 | | - original_hasattr = hasattr |
242 | | - |
243 | | - def custom_hasattr(obj, name): |
244 | | - if obj is mtls and name == "should_use_client_cert": |
245 | | - return has_method |
246 | | - return original_hasattr(obj, name) |
247 | | - |
248 | | - with mock.patch("google.api_core.gapic_v1.client_utils.hasattr", custom_hasattr): |
249 | | - with mock.patch( |
250 | | - "google.auth.transport.mtls.should_use_client_cert", |
251 | | - create=True, |
252 | | - return_value=method_val, |
253 | | - ): |
254 | | - env = {} |
255 | | - if env_val is not None: |
256 | | - env["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = env_val |
257 | | - with mock.patch.dict(os.environ, env, clear=True): |
258 | | - if expected == "value_error": |
259 | | - with pytest.raises( |
260 | | - ValueError, match="must be either `true` or `false`" |
261 | | - ): |
262 | | - use_client_cert_effective() |
263 | | - else: |
264 | | - assert use_client_cert_effective() is (expected == "true") |
265 | | - |
266 | | - |
267 | | -@pytest.mark.parametrize( |
268 | | - "provided, use_cert, has_default_avail, default_val, expected", |
269 | | - [ |
270 | | - (None, False, True, b"default", None), |
271 | | - (b"provided", False, True, b"default", None), |
272 | | - (b"provided", True, True, b"default", b"provided"), |
273 | | - (None, True, True, b"default", b"default"), |
274 | | - (None, True, False, b"default", "value_error"), |
275 | | - ], |
276 | | - ids=[ |
277 | | - "use_cert_false_no_provided", |
278 | | - "use_cert_false_with_provided", |
279 | | - "use_cert_true_with_provided", |
280 | | - "use_cert_true_no_provided_default_avail", |
281 | | - "use_cert_true_no_provided_default_unavail", |
282 | | - ], |
283 | | -) |
284 | | -def test_get_client_cert_source( |
285 | | - provided, use_cert, has_default_avail, default_val, expected |
286 | | -): |
287 | | - original_hasattr = hasattr |
288 | | - |
289 | | - def custom_hasattr(obj, name): |
290 | | - if obj is mtls and name == "has_default_client_cert_source": |
291 | | - return has_default_avail |
292 | | - return original_hasattr(obj, name) |
293 | | - |
294 | | - with mock.patch("google.api_core.gapic_v1.client_utils.hasattr", custom_hasattr): |
295 | | - with mock.patch( |
296 | | - "google.auth.transport.mtls.has_default_client_cert_source", |
297 | | - create=True, |
298 | | - return_value=has_default_avail, |
299 | | - ): |
300 | | - with mock.patch( |
301 | | - "google.auth.transport.mtls.default_client_cert_source", |
302 | | - create=True, |
303 | | - return_value=default_val, |
304 | | - ): |
305 | | - if expected == "value_error": |
306 | | - with pytest.raises( |
307 | | - ValueError, match="Client certificate is required for mTLS" |
308 | | - ): |
309 | | - get_client_cert_source(provided, use_cert) |
310 | | - else: |
311 | | - assert get_client_cert_source(provided, use_cert) == expected |
312 | 205 |
|
313 | | - |
314 | | -@pytest.mark.parametrize( |
315 | | - "env, mock_cert_val, expected", |
316 | | - [ |
317 | | - ({}, False, (False, "auto", None)), |
318 | | - ({}, True, (True, "auto", None)), |
319 | | - ({"GOOGLE_API_USE_MTLS_ENDPOINT": "never"}, False, (False, "never", None)), |
320 | | - ({"GOOGLE_API_USE_MTLS_ENDPOINT": "always"}, False, (False, "always", None)), |
321 | | - ({"GOOGLE_API_USE_MTLS_ENDPOINT": "auto"}, False, (False, "auto", None)), |
322 | | - ({"GOOGLE_API_USE_MTLS_ENDPOINT": "invalid"}, False, "mutual_tls_error"), |
323 | | - ( |
324 | | - {"GOOGLE_CLOUD_UNIVERSE_DOMAIN": "foo.com"}, |
325 | | - False, |
326 | | - (False, "auto", "foo.com"), |
327 | | - ), |
328 | | - ], |
329 | | - ids=[ |
330 | | - "default_env", |
331 | | - "client_cert_true", |
332 | | - "mtls_never", |
333 | | - "mtls_always", |
334 | | - "mtls_auto", |
335 | | - "mtls_invalid", |
336 | | - "universe_domain", |
337 | | - ], |
338 | | -) |
339 | | -def test_read_environment_variables(env, mock_cert_val, expected): |
340 | | - with mock.patch( |
341 | | - "google.api_core.gapic_v1.client_utils.use_client_cert_effective", |
342 | | - return_value=mock_cert_val, |
343 | | - ): |
344 | | - with mock.patch.dict(os.environ, env, clear=True): |
345 | | - if expected == "mutual_tls_error": |
346 | | - with pytest.raises( |
347 | | - MutualTLSChannelError, match="must be `never`, `auto` or `always`" |
348 | | - ): |
349 | | - read_environment_variables() |
350 | | - else: |
351 | | - assert read_environment_variables() == expected |
0 commit comments