@@ -176,60 +176,157 @@ def test_wrap_errors_streaming(wrap_stream_errors):
176
176
wrap_stream_errors .assert_called_once_with (callable_ )
177
177
178
178
179
+ @mock .patch ('grpc.composite_channel_credentials' )
179
180
@mock .patch (
180
181
'google.auth.default' ,
181
182
return_value = (mock .sentinel .credentials , mock .sentinel .projet ))
182
- @mock .patch ('google.auth.transport.grpc.secure_authorized_channel' )
183
- def test_create_channel_implicit (secure_authorized_channel , default ):
183
+ @mock .patch ('grpc.secure_channel' )
184
+ def test_create_channel_implicit (
185
+ grpc_secure_channel , default , composite_creds_call ):
184
186
target = 'example.com:443'
187
+ composite_creds = composite_creds_call .return_value
185
188
186
189
channel = grpc_helpers .create_channel (target )
187
190
188
- assert channel is secure_authorized_channel .return_value
191
+ assert channel is grpc_secure_channel .return_value
189
192
default .assert_called_once_with (scopes = None )
190
- secure_authorized_channel .assert_called_once_with (
191
- mock .sentinel .credentials , mock .ANY , target )
193
+ if (grpc_helpers .HAS_GRPC_GCP ):
194
+ grpc_secure_channel .assert_called_once_with (
195
+ target , composite_creds , None )
196
+ else :
197
+ grpc_secure_channel .assert_called_once_with (
198
+ target , composite_creds )
192
199
193
200
201
+ @mock .patch ('grpc.composite_channel_credentials' )
194
202
@mock .patch (
195
203
'google.auth.default' ,
196
204
return_value = (mock .sentinel .credentials , mock .sentinel .projet ))
197
- @mock .patch ('google.auth.transport.grpc.secure_authorized_channel' )
205
+ @mock .patch ('grpc.secure_channel' )
206
+ def test_create_channel_implicit_with_ssl_creds (
207
+ grpc_secure_channel , default , composite_creds_call ):
208
+ target = 'example.com:443'
209
+
210
+ ssl_creds = grpc .ssl_channel_credentials ()
211
+
212
+ grpc_helpers .create_channel (target , ssl_credentials = ssl_creds )
213
+
214
+ default .assert_called_once_with (scopes = None )
215
+ composite_creds_call .assert_called_once_with (ssl_creds , mock .ANY )
216
+ composite_creds = composite_creds_call .return_value
217
+ if (grpc_helpers .HAS_GRPC_GCP ):
218
+ grpc_secure_channel .assert_called_once_with (
219
+ target , composite_creds , None )
220
+ else :
221
+ grpc_secure_channel .assert_called_once_with (
222
+ target , composite_creds )
223
+
224
+
225
+ @mock .patch ('grpc.composite_channel_credentials' )
226
+ @mock .patch (
227
+ 'google.auth.default' ,
228
+ return_value = (mock .sentinel .credentials , mock .sentinel .projet ))
229
+ @mock .patch ('grpc.secure_channel' )
198
230
def test_create_channel_implicit_with_scopes (
199
- secure_authorized_channel , default ):
231
+ grpc_secure_channel , default , composite_creds_call ):
200
232
target = 'example.com:443'
233
+ composite_creds = composite_creds_call .return_value
201
234
202
235
channel = grpc_helpers .create_channel (target , scopes = ['one' , 'two' ])
203
236
204
- assert channel is secure_authorized_channel .return_value
237
+ assert channel is grpc_secure_channel .return_value
205
238
default .assert_called_once_with (scopes = ['one' , 'two' ])
206
-
207
-
208
- @mock .patch ('google.auth.transport.grpc.secure_authorized_channel' )
209
- def test_create_channel_explicit (secure_authorized_channel ):
239
+ if (grpc_helpers .HAS_GRPC_GCP ):
240
+ grpc_secure_channel .assert_called_once_with (
241
+ target , composite_creds , None )
242
+ else :
243
+ grpc_secure_channel .assert_called_once_with (
244
+ target , composite_creds )
245
+
246
+
247
+ @mock .patch ('grpc.composite_channel_credentials' )
248
+ @mock .patch ('google.auth.credentials.with_scopes_if_required' )
249
+ @mock .patch ('grpc.secure_channel' )
250
+ def test_create_channel_explicit (
251
+ grpc_secure_channel , auth_creds , composite_creds_call ):
210
252
target = 'example.com:443'
253
+ composite_creds = composite_creds_call .return_value
211
254
212
255
channel = grpc_helpers .create_channel (
213
256
target , credentials = mock .sentinel .credentials )
214
257
215
- assert channel is secure_authorized_channel .return_value
216
- secure_authorized_channel .assert_called_once_with (
217
- mock .sentinel .credentials , mock .ANY , target )
258
+ auth_creds .assert_called_once_with (mock .sentinel .credentials , None )
259
+ assert channel is grpc_secure_channel .return_value
260
+ if (grpc_helpers .HAS_GRPC_GCP ):
261
+ grpc_secure_channel .assert_called_once_with (
262
+ target , composite_creds , None )
263
+ else :
264
+ grpc_secure_channel .assert_called_once_with (
265
+ target , composite_creds )
218
266
219
267
220
- @mock .patch ('google.auth.transport.grpc.secure_authorized_channel' )
221
- def test_create_channel_explicit_scoped (unused_secure_authorized_channel ):
268
+ @mock .patch ('grpc.composite_channel_credentials' )
269
+ @mock .patch ('grpc.secure_channel' )
270
+ def test_create_channel_explicit_scoped (
271
+ grpc_secure_channel , composite_creds_call ):
272
+ target = 'example.com:443'
222
273
scopes = ['1' , '2' ]
274
+ composite_creds = composite_creds_call .return_value
275
+
276
+ credentials = mock .create_autospec (
277
+ google .auth .credentials .Scoped , instance = True )
278
+ credentials .requires_scopes = True
279
+
280
+ channel = grpc_helpers .create_channel (
281
+ target ,
282
+ credentials = credentials ,
283
+ scopes = scopes )
284
+
285
+ credentials .with_scopes .assert_called_once_with (scopes )
286
+ assert channel is grpc_secure_channel .return_value
287
+ if (grpc_helpers .HAS_GRPC_GCP ):
288
+ grpc_secure_channel .assert_called_once_with (
289
+ target , composite_creds , None )
290
+ else :
291
+ grpc_secure_channel .assert_called_once_with (
292
+ target , composite_creds )
293
+
294
+
295
+ @pytest .mark .skipif (not grpc_helpers .HAS_GRPC_GCP ,
296
+ reason = 'grpc_gcp module not available' )
297
+ @mock .patch ('grpc_gcp.secure_channel' )
298
+ def test_create_channel_with_grpc_gcp (grpc_gcp_secure_channel ):
299
+ target = 'example.com:443'
300
+ scopes = ['test_scope' ]
223
301
224
302
credentials = mock .create_autospec (
225
303
google .auth .credentials .Scoped , instance = True )
226
304
credentials .requires_scopes = True
227
305
228
306
grpc_helpers .create_channel (
229
- mock . sentinel . target ,
307
+ target ,
230
308
credentials = credentials ,
231
309
scopes = scopes )
310
+ grpc_gcp_secure_channel .assert_called ()
311
+ credentials .with_scopes .assert_called_once_with (scopes )
232
312
313
+
314
+ @pytest .mark .skipif (grpc_helpers .HAS_GRPC_GCP ,
315
+ reason = 'grpc_gcp module not available' )
316
+ @mock .patch ('grpc.secure_channel' )
317
+ def test_create_channel_without_grpc_gcp (grpc_secure_channel ):
318
+ target = 'example.com:443'
319
+ scopes = ['test_scope' ]
320
+
321
+ credentials = mock .create_autospec (
322
+ google .auth .credentials .Scoped , instance = True )
323
+ credentials .requires_scopes = True
324
+
325
+ grpc_helpers .create_channel (
326
+ target ,
327
+ credentials = credentials ,
328
+ scopes = scopes )
329
+ grpc_secure_channel .assert_called ()
233
330
credentials .with_scopes .assert_called_once_with (scopes )
234
331
235
332
0 commit comments