5454 "fallback_env_false_uppercase" ,
5555 "fallback_env_unset" ,
5656 "fallback_env_unsupported" ,
57- ]
57+ ],
5858)
5959def test_use_client_cert_effective (has_method , method_val , env_val , expected ):
6060 # Mock hasattr to control whether should_use_client_cert exists
6161 original_hasattr = hasattr
62+
6263 def custom_hasattr (obj , name ):
6364 if obj is mtls and name == "should_use_client_cert" :
6465 return has_method
6566 return original_hasattr (obj , name )
6667
6768 with mock .patch ("google.api_core.gapic_v1.client_cert.hasattr" , custom_hasattr ):
68- with mock .patch ("google.auth.transport.mtls.should_use_client_cert" , create = True , return_value = method_val ):
69+ with mock .patch (
70+ "google.auth.transport.mtls.should_use_client_cert" ,
71+ create = True ,
72+ return_value = method_val ,
73+ ):
6974 env = {}
7075 if env_val is not None :
7176 env ["GOOGLE_API_USE_CLIENT_CERTIFICATE" ] = env_val
7277 with mock .patch .dict (os .environ , env , clear = True ):
7378 if expected == "value_error" :
74- with pytest .raises (ValueError , match = "must be either `true` or `false`" ):
79+ with pytest .raises (
80+ ValueError , match = "must be either `true` or `false`"
81+ ):
7582 use_client_cert_effective ()
7683 else :
7784 assert use_client_cert_effective () is (expected == "true" )
@@ -84,24 +91,41 @@ def custom_hasattr(obj, name):
8491 (b"provided" , False , True , b"default" , None ),
8592 (b"provided" , True , True , b"default" , b"provided" ),
8693 (None , True , True , b"default" , b"default" ),
87- (None , True , False , b"default" , None ),
94+ (None , True , False , b"default" , "value_error" ),
8895 ],
8996 ids = [
9097 "use_cert_false_no_provided" ,
9198 "use_cert_false_with_provided" ,
9299 "use_cert_true_with_provided" ,
93100 "use_cert_true_no_provided_default_avail" ,
94101 "use_cert_true_no_provided_default_unavail" ,
95- ]
102+ ],
96103)
97- def test_get_client_cert_source (provided , use_cert , has_default_avail , default_val , expected ):
104+ def test_get_client_cert_source (
105+ provided , use_cert , has_default_avail , default_val , expected
106+ ):
98107 original_hasattr = hasattr
108+
99109 def custom_hasattr (obj , name ):
100110 if obj is mtls and name == "has_default_client_cert_source" :
101111 return has_default_avail
102112 return original_hasattr (obj , name )
103113
104114 with mock .patch ("google.api_core.gapic_v1.client_cert.hasattr" , custom_hasattr ):
105- with mock .patch ("google.auth.transport.mtls.has_default_client_cert_source" , create = True , return_value = has_default_avail ):
106- with mock .patch ("google.auth.transport.mtls.default_client_cert_source" , create = True , return_value = default_val ):
107- assert get_client_cert_source (provided , use_cert ) == expected
115+ with mock .patch (
116+ "google.auth.transport.mtls.has_default_client_cert_source" ,
117+ create = True ,
118+ return_value = has_default_avail ,
119+ ):
120+ with mock .patch (
121+ "google.auth.transport.mtls.default_client_cert_source" ,
122+ create = True ,
123+ return_value = default_val ,
124+ ):
125+ if expected == "value_error" :
126+ with pytest .raises (
127+ ValueError , match = "Client certificate is required for mTLS"
128+ ):
129+ get_client_cert_source (provided , use_cert )
130+ else :
131+ assert get_client_cert_source (provided , use_cert ) == expected
0 commit comments