@@ -105,49 +105,21 @@ def test_{{ service.client_name|snake_case }}_client_options():
105105
106106 # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
107107 # "never".
108- os.environ[ "GOOGLE_API_USE_MTLS"] = "never"
109- with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
110- grpc_transport.return_value = None
111- client = {{ service.client_name }}()
112- grpc_transport.assert_called_once_with(
113- api_mtls_endpoint=client.DEFAULT_ENDPOINT,
114- client_cert_source=None,
115- credentials=None,
116- host=client.DEFAULT_ENDPOINT,
117- )
108+ with mock.patch.dict( os.environ, { "GOOGLE_API_USE_MTLS": "never"}):
109+ with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
110+ grpc_transport.return_value = None
111+ client = {{ service.client_name }}()
112+ grpc_transport.assert_called_once_with(
113+ api_mtls_endpoint=client.DEFAULT_ENDPOINT,
114+ client_cert_source=None,
115+ credentials=None,
116+ host=client.DEFAULT_ENDPOINT,
117+ )
118118
119119 # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS is
120120 # "always".
121- os.environ["GOOGLE_API_USE_MTLS"] = "always"
122- with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
123- grpc_transport.return_value = None
124- client = {{ service.client_name }}()
125- grpc_transport.assert_called_once_with(
126- api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
127- client_cert_source=None,
128- credentials=None,
129- host=client.DEFAULT_MTLS_ENDPOINT,
130- )
131-
132- # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
133- # "auto", and client_cert_source is provided.
134- os.environ["GOOGLE_API_USE_MTLS"] = "auto"
135- options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
136- with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
137- grpc_transport.return_value = None
138- client = {{ service.client_name }}(client_options=options)
139- grpc_transport.assert_called_once_with(
140- api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
141- client_cert_source=client_cert_source_callback,
142- credentials=None,
143- host=client.DEFAULT_MTLS_ENDPOINT,
144- )
145-
146- # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
147- # "auto", and default_client_cert_source is provided.
148- os.environ["GOOGLE_API_USE_MTLS"] = "auto"
149- with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
150- with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
121+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "always"}):
122+ with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
151123 grpc_transport.return_value = None
152124 client = {{ service.client_name }}()
153125 grpc_transport.assert_called_once_with(
@@ -158,26 +130,52 @@ def test_{{ service.client_name|snake_case }}_client_options():
158130 )
159131
160132 # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
161- # "auto", but client_cert_source and default_client_cert_source are None .
162- os.environ[ "GOOGLE_API_USE_MTLS"] = "auto"
163- with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
164- with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False) :
133+ # "auto", and client_cert_source is provided .
134+ with mock.patch.dict( os.environ, { "GOOGLE_API_USE_MTLS": "auto"}):
135+ options = client_options.ClientOptions(client_cert_source=client_cert_source_callback)
136+ with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport :
165137 grpc_transport.return_value = None
166- client = {{ service.client_name }}()
138+ client = {{ service.client_name }}(client_options=options )
167139 grpc_transport.assert_called_once_with(
168- api_mtls_endpoint=client.DEFAULT_ENDPOINT ,
169- client_cert_source=None ,
140+ api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT ,
141+ client_cert_source=client_cert_source_callback ,
170142 credentials=None,
171- host=client.DEFAULT_ENDPOINT ,
143+ host=client.DEFAULT_MTLS_ENDPOINT ,
172144 )
173145
146+ # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
147+ # "auto", and default_client_cert_source is provided.
148+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
149+ with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
150+ with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=True):
151+ grpc_transport.return_value = None
152+ client = {{ service.client_name }}()
153+ grpc_transport.assert_called_once_with(
154+ api_mtls_endpoint=client.DEFAULT_MTLS_ENDPOINT,
155+ client_cert_source=None,
156+ credentials=None,
157+ host=client.DEFAULT_MTLS_ENDPOINT,
158+ )
159+
160+ # Check the case api_endpoint is not provided, GOOGLE_API_USE_MTLS is
161+ # "auto", but client_cert_source and default_client_cert_source are None.
162+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "auto"}):
163+ with mock.patch('{{ (api.naming.module_namespace + (api.naming.versioned_module_name,) + service.meta.address.subpackage)|join(".") }}.services.{{ service.name|snake_case }}.transports.{{ service.name }}GrpcTransport.__init__') as grpc_transport:
164+ with mock.patch('google.auth.transport.mtls.has_default_client_cert_source', return_value=False):
165+ grpc_transport.return_value = None
166+ client = {{ service.client_name }}()
167+ grpc_transport.assert_called_once_with(
168+ api_mtls_endpoint=client.DEFAULT_ENDPOINT,
169+ client_cert_source=None,
170+ credentials=None,
171+ host=client.DEFAULT_ENDPOINT,
172+ )
173+
174174 # Check the case api_endpoint is not provided and GOOGLE_API_USE_MTLS has
175175 # unsupported value.
176- os.environ["GOOGLE_API_USE_MTLS"] = "Unsupported"
177- with pytest.raises(MutualTLSChannelError):
178- client = {{ service.client_name }}()
179-
180- del os.environ["GOOGLE_API_USE_MTLS"]
176+ with mock.patch.dict(os.environ, {"GOOGLE_API_USE_MTLS": "Unsupported"}):
177+ with pytest.raises(MutualTLSChannelError):
178+ client = {{ service.client_name }}()
181179
182180
183181def test_{{ service.client_name|snake_case }}_client_options_from_dict():
0 commit comments