@@ -429,6 +429,90 @@ def test_managed_identities_service_client_mtls_env_auto(
429
429
)
430
430
431
431
432
+ @pytest .mark .parametrize (
433
+ "client_class" ,
434
+ [ManagedIdentitiesServiceClient , ManagedIdentitiesServiceAsyncClient ],
435
+ )
436
+ @mock .patch .object (
437
+ ManagedIdentitiesServiceClient ,
438
+ "DEFAULT_ENDPOINT" ,
439
+ modify_default_endpoint (ManagedIdentitiesServiceClient ),
440
+ )
441
+ @mock .patch .object (
442
+ ManagedIdentitiesServiceAsyncClient ,
443
+ "DEFAULT_ENDPOINT" ,
444
+ modify_default_endpoint (ManagedIdentitiesServiceAsyncClient ),
445
+ )
446
+ def test_managed_identities_service_client_get_mtls_endpoint_and_cert_source (
447
+ client_class ,
448
+ ):
449
+ mock_client_cert_source = mock .Mock ()
450
+
451
+ # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "true".
452
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
453
+ mock_api_endpoint = "foo"
454
+ options = client_options .ClientOptions (
455
+ client_cert_source = mock_client_cert_source , api_endpoint = mock_api_endpoint
456
+ )
457
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source (
458
+ options
459
+ )
460
+ assert api_endpoint == mock_api_endpoint
461
+ assert cert_source == mock_client_cert_source
462
+
463
+ # Test the case GOOGLE_API_USE_CLIENT_CERTIFICATE is "false".
464
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "false" }):
465
+ mock_client_cert_source = mock .Mock ()
466
+ mock_api_endpoint = "foo"
467
+ options = client_options .ClientOptions (
468
+ client_cert_source = mock_client_cert_source , api_endpoint = mock_api_endpoint
469
+ )
470
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source (
471
+ options
472
+ )
473
+ assert api_endpoint == mock_api_endpoint
474
+ assert cert_source is None
475
+
476
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "never".
477
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_MTLS_ENDPOINT" : "never" }):
478
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
479
+ assert api_endpoint == client_class .DEFAULT_ENDPOINT
480
+ assert cert_source is None
481
+
482
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "always".
483
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_MTLS_ENDPOINT" : "always" }):
484
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
485
+ assert api_endpoint == client_class .DEFAULT_MTLS_ENDPOINT
486
+ assert cert_source is None
487
+
488
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert doesn't exist.
489
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
490
+ with mock .patch (
491
+ "google.auth.transport.mtls.has_default_client_cert_source" ,
492
+ return_value = False ,
493
+ ):
494
+ api_endpoint , cert_source = client_class .get_mtls_endpoint_and_cert_source ()
495
+ assert api_endpoint == client_class .DEFAULT_ENDPOINT
496
+ assert cert_source is None
497
+
498
+ # Test the case GOOGLE_API_USE_MTLS_ENDPOINT is "auto" and default cert exists.
499
+ with mock .patch .dict (os .environ , {"GOOGLE_API_USE_CLIENT_CERTIFICATE" : "true" }):
500
+ with mock .patch (
501
+ "google.auth.transport.mtls.has_default_client_cert_source" ,
502
+ return_value = True ,
503
+ ):
504
+ with mock .patch (
505
+ "google.auth.transport.mtls.default_client_cert_source" ,
506
+ return_value = mock_client_cert_source ,
507
+ ):
508
+ (
509
+ api_endpoint ,
510
+ cert_source ,
511
+ ) = client_class .get_mtls_endpoint_and_cert_source ()
512
+ assert api_endpoint == client_class .DEFAULT_MTLS_ENDPOINT
513
+ assert cert_source == mock_client_cert_source
514
+
515
+
432
516
@pytest .mark .parametrize (
433
517
"client_class,transport_class,transport_name" ,
434
518
[
@@ -2960,6 +3044,25 @@ def test_credentials_transport_error():
2960
3044
transport = transport ,
2961
3045
)
2962
3046
3047
+ # It is an error to provide an api_key and a transport instance.
3048
+ transport = transports .ManagedIdentitiesServiceGrpcTransport (
3049
+ credentials = ga_credentials .AnonymousCredentials (),
3050
+ )
3051
+ options = client_options .ClientOptions ()
3052
+ options .api_key = "api_key"
3053
+ with pytest .raises (ValueError ):
3054
+ client = ManagedIdentitiesServiceClient (
3055
+ client_options = options , transport = transport ,
3056
+ )
3057
+
3058
+ # It is an error to provide an api_key and a credential.
3059
+ options = mock .Mock ()
3060
+ options .api_key = "api_key"
3061
+ with pytest .raises (ValueError ):
3062
+ client = ManagedIdentitiesServiceClient (
3063
+ client_options = options , credentials = ga_credentials .AnonymousCredentials ()
3064
+ )
3065
+
2963
3066
# It is an error to provide scopes and a transport instance.
2964
3067
transport = transports .ManagedIdentitiesServiceGrpcTransport (
2965
3068
credentials = ga_credentials .AnonymousCredentials (),
@@ -3567,3 +3670,39 @@ def test_client_ctx():
3567
3670
with client :
3568
3671
pass
3569
3672
close .assert_called ()
3673
+
3674
+
3675
+ @pytest .mark .parametrize (
3676
+ "client_class,transport_class" ,
3677
+ [
3678
+ (
3679
+ ManagedIdentitiesServiceClient ,
3680
+ transports .ManagedIdentitiesServiceGrpcTransport ,
3681
+ ),
3682
+ (
3683
+ ManagedIdentitiesServiceAsyncClient ,
3684
+ transports .ManagedIdentitiesServiceGrpcAsyncIOTransport ,
3685
+ ),
3686
+ ],
3687
+ )
3688
+ def test_api_key_credentials (client_class , transport_class ):
3689
+ with mock .patch .object (
3690
+ google .auth ._default , "get_api_key_credentials" , create = True
3691
+ ) as get_api_key_credentials :
3692
+ mock_cred = mock .Mock ()
3693
+ get_api_key_credentials .return_value = mock_cred
3694
+ options = client_options .ClientOptions ()
3695
+ options .api_key = "api_key"
3696
+ with mock .patch .object (transport_class , "__init__" ) as patched :
3697
+ patched .return_value = None
3698
+ client = client_class (client_options = options )
3699
+ patched .assert_called_once_with (
3700
+ credentials = mock_cred ,
3701
+ credentials_file = None ,
3702
+ host = client .DEFAULT_ENDPOINT ,
3703
+ scopes = None ,
3704
+ client_cert_source_for_mtls = None ,
3705
+ quota_project_id = None ,
3706
+ client_info = transports .base .DEFAULT_CLIENT_INFO ,
3707
+ always_use_jwt_access = True ,
3708
+ )
0 commit comments