@@ -422,6 +422,90 @@ def test_speech_translation_service_client_mtls_env_auto(
422
422
)
423
423
424
424
425
+ @pytest .mark .parametrize (
426
+ "client_class" ,
427
+ [SpeechTranslationServiceClient , SpeechTranslationServiceAsyncClient ],
428
+ )
429
+ @mock .patch .object (
430
+ SpeechTranslationServiceClient ,
431
+ "DEFAULT_ENDPOINT" ,
432
+ modify_default_endpoint (SpeechTranslationServiceClient ),
433
+ )
434
+ @mock .patch .object (
435
+ SpeechTranslationServiceAsyncClient ,
436
+ "DEFAULT_ENDPOINT" ,
437
+ modify_default_endpoint (SpeechTranslationServiceAsyncClient ),
438
+ )
439
+ def test_speech_translation_service_client_get_mtls_endpoint_and_cert_source (
440
+ client_class ,
441
+ ):
442
+ mock_client_cert_source = mock .Mock ()
443
+
444
+ # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
445
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
446
+ mock_api_endpoint = "foo"
447
+ options = client_options .ClientOptions (
448
+ client_cert_source = mock_client_cert_source , api_endpoint = mock_api_endpoint
449
+ )
450
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source (
451
+ options
452
+ )
453
+ assert api_endpoint == mock_api_endpoint
454
+ assert cert_source == mock_client_cert_source
455
+
456
+ # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
457
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "false" }):
458
+ mock_client_cert_source = mock .Mock ()
459
+ mock_api_endpoint = "foo"
460
+ options = client_options .ClientOptions (
461
+ client_cert_source = mock_client_cert_source , api_endpoint = mock_api_endpoint
462
+ )
463
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source (
464
+ options
465
+ )
466
+ assert api_endpoint == mock_api_endpoint
467
+ assert cert_source is None
468
+
469
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
470
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_MTLS_ENDPOINT" : "never" }):
471
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
472
+ assert api_endpoint == client_class .DEFAULT_ENDPOINT
473
+ assert cert_source is None
474
+
475
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
476
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_MTLS_ENDPOINT" : "always" }):
477
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
478
+ assert api_endpoint == client_class .DEFAULT_MTLS_ENDPOINT
479
+ assert cert_source is None
480
+
481
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
482
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
483
+ with mock .patch (
484
+ "google.auth.transport.mtls.has_default_client_cert_source" ,
485
+ return_value = False ,
486
+ ):
487
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
488
+ assert api_endpoint == client_class .DEFAULT_ENDPOINT
489
+ assert cert_source is None
490
+
491
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
492
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
493
+ with mock .patch (
494
+ "google.auth.transport.mtls.has_default_client_cert_source" ,
495
+ return_value = True ,
496
+ ):
497
+ with mock .patch (
498
+ "google.auth.transport.mtls.default_client_cert_source" ,
499
+ return_value = mock_client_cert_source ,
500
+ ):
501
+ (
502
+ api_endpoint ,
503
+ cert_source ,
504
+ ) = client_class .get_mtls_endpoint_and_cert_source ()
505
+ assert api_endpoint == client_class .DEFAULT_MTLS_ENDPOINT
506
+ assert cert_source == mock_client_cert_source
507
+
508
+
425
509
@pytest .mark .parametrize (
426
510
"client_class,transport_class,transport_name" ,
427
511
[
@@ -603,6 +687,25 @@ def test_credentials_transport_error():
603
687
transport = transport ,
604
688
)
605
689
690
+ # It is an error to provide an api_key and a transport instance.
691
+ transport = transports .SpeechTranslationServiceGrpcTransport (
692
+ credentials = ga_credentials .AnonymousCredentials (),
693
+ )
694
+ options = client_options .ClientOptions ()
695
+ options .api_key = "api_key"
696
+ with pytest .raises (ValueError ):
697
+ client = SpeechTranslationServiceClient (
698
+ client_options = options , transport = transport ,
699
+ )
700
+
701
+ # It is an error to provide an api_key and a credential.
702
+ options = mock .Mock ()
703
+ options .api_key = "api_key"
704
+ with pytest .raises (ValueError ):
705
+ client = SpeechTranslationServiceClient (
706
+ client_options = options , credentials = ga_credentials .AnonymousCredentials ()
707
+ )
708
+
606
709
# It is an error to provide scopes and a transport instance.
607
710
transport = transports .SpeechTranslationServiceGrpcTransport (
608
711
credentials = ga_credentials .AnonymousCredentials (),
@@ -1144,3 +1247,39 @@ def test_client_ctx():
1144
1247
with client :
1145
1248
pass
1146
1249
close .assert_called ()
1250
+
1251
+
1252
+ @pytest .mark .parametrize (
1253
+ "client_class,transport_class" ,
1254
+ [
1255
+ (
1256
+ SpeechTranslationServiceClient ,
1257
+ transports .SpeechTranslationServiceGrpcTransport ,
1258
+ ),
1259
+ (
1260
+ SpeechTranslationServiceAsyncClient ,
1261
+ transports .SpeechTranslationServiceGrpcAsyncIOTransport ,
1262
+ ),
1263
+ ],
1264
+ )
1265
+ def test_api_key_credentials (client_class , transport_class ):
1266
+ with mock .patch .object (
1267
+ google .auth ._default , "get_api_key_credentials" , create = True
1268
+ ) as get_api_key_credentials :
1269
+ mock_cred = mock .Mock ()
1270
+ get_api_key_credentials .return_value = mock_cred
1271
+ options = client_options .ClientOptions ()
1272
+ options .api_key = "api_key"
1273
+ with mock .patch .object (transport_class , "__init__" ) as patched :
1274
+ patched .return_value = None
1275
+ client = client_class (client_options = options )
1276
+ patched .assert_called_once_with (
1277
+ credentials = mock_cred ,
1278
+ credentials_file = None ,
1279
+ host = client .DEFAULT_ENDPOINT ,
1280
+ scopes = None ,
1281
+ client_cert_source_for_mtls = None ,
1282
+ quota_project_id = None ,
1283
+ client_info = transports .base .DEFAULT_CLIENT_INFO ,
1284
+ always_use_jwt_access = True ,
1285
+ )
0 commit comments