@@ -175,7 +175,8 @@ def test__get_request_header(self, mock_refresh_authentication):
175
175
176
176
@patch ("weaviate.connect.connection.requests" )
177
177
@patch ("weaviate.connect.connection.get_epoch_time" )
178
- def test__refresh_authentication (self , mock_get_epoch_time , mock_requests ):
178
+ @patch ("weaviate.connect.connection.Connection._set_bearer" )
179
+ def test__refresh_authentication (self , mock_set_bearer , mock_get_epoch_time , mock_requests ):
179
180
"""
180
181
Test the `_refresh_authentication` method.
181
182
"""
@@ -190,10 +191,64 @@ def test__refresh_authentication(self, mock_get_epoch_time, mock_requests):
190
191
self .check_connection_attributes (connection ) # after the `_refresh_authentication` call
191
192
mock_get_epoch_time .assert_called ()
192
193
mock_requests .get .assert_not_called ()
194
+ mock_set_bearer .assert_not_called ()
193
195
194
196
# error messages
195
197
data_error_message = "Cannot connect to weaviate."
196
198
data_status_code_error_message = "Cannot authenticate http status not ok."
199
+
200
+ mock_get_epoch_time .return_value = 200
201
+ get_kwargs = {
202
+ 'headers' : {"content-type" : "application/json" },
203
+ 'timeout' : (30 , 45 )
204
+ }
205
+ # test the expired connection
206
+ ## requests.get exception (get data)
207
+ connection = Connection ('test_url' , auth_client_secret = None )
208
+ mock_requests .get .configure_mock (side_effect = RequestException ('Test!' ))
209
+ with self .assertRaises (AuthenticationFailedException ) as error :
210
+ connection ._refresh_authentication ()
211
+
212
+ check_error_message (self , error , data_error_message )
213
+ mock_requests .get .assert_called_with ("test_url/v1/.well-known/openid-configuration" , ** get_kwargs )
214
+ mock_set_bearer .assert_not_called ()
215
+
216
+ ## bad status_code (get data)
217
+ mock_get_epoch_time .reset_mock () # reset mock.called
218
+ ### reset 'requests' mock because it is called in the `__init__`
219
+ mock_requests .get .reset_mock (side_effect = True , return_value = True )
220
+ connection = Connection ('test_url' , auth_client_secret = None )
221
+ mock_requests .get .return_value = Mock (status_code = 404 )
222
+ with self .assertRaises (AuthenticationFailedException ) as error :
223
+ connection ._refresh_authentication ()
224
+ check_error_message (self , error , data_status_code_error_message )
225
+ mock_requests .get .assert_called_with ("test_url/v1/.well-known/openid-configuration" , ** get_kwargs )
226
+ mock_set_bearer .assert_not_called ()
227
+
228
+ # valid call
229
+ mock_get_epoch_time .reset_mock () # reset mock.called
230
+ ## reset 'requests' mock because it is called in the `__init__`
231
+ mock_requests .get .reset_mock (side_effect = True , return_value = True )
232
+ connection = Connection ('test_url' , auth_client_secret = None )
233
+ mock_requests .get .return_value = Mock (** {'status_code' : 200 , 'json.return_value' : {'clientId' : 'Test1!' , 'href' : 'Test2!' }})
234
+ connection ._refresh_authentication ()
235
+ mock_requests .get .assert_called_with ("test_url/v1/.well-known/openid-configuration" , ** get_kwargs )
236
+ mock_set_bearer .assert_called_with (client_id = 'Test1!' , href = 'Test2!' )
237
+
238
+ @patch ("weaviate.connect.connection.requests" )
239
+ @patch ("weaviate.connect.connection.Connection._refresh_authentication" )
240
+ def test__set_bearer (self , mock_refresh_authentication , mock_requests ):
241
+ """
242
+ Test the `_set_bearer` method.
243
+ """
244
+
245
+ get_kwargs = {
246
+ 'headers' : {"content-type" : "application/json" },
247
+ 'timeout' : (30 , 45 )
248
+ }
249
+ mock_refresh_authentication .return_value = None
250
+
251
+ # error messages
197
252
add_info_error_message = ("Can't connect to the third party authentication service. "
198
253
"Check that it is running." )
199
254
add_info_status_code_error_message = "Status not OK in connection to the third party authentication service."
@@ -203,11 +258,8 @@ def test__refresh_authentication(self, mock_get_epoch_time, mock_requests):
203
258
"and URLs correct?" )
204
259
oauth_status_code_error_message = "Authtentication access denied. Are the credentials correct?"
205
260
206
- # test the expired connection
207
- mock_get_epoch_time .return_value = 200
208
-
209
261
# helper function
210
- def helper_before_call (* args , * *kwargs ):
262
+ def helper_before_call (** kwargs ):
211
263
"""
212
264
initialize mock objects and connection before testing th exception.
213
265
@@ -217,18 +269,14 @@ def helper_before_call(*args, **kwargs):
217
269
Connection.
218
270
"""
219
271
220
- mock_second_get_call .called = False
221
- mock_get_epoch_time .reset_mock () # reset mock.called
222
272
# reset 'requests' mock because it is called in the `__init__`
223
273
mock_requests .get .reset_mock (side_effect = True , return_value = True )
224
- connection = Connection (* args , auth_client_secret = kwargs .get ("auth_client_secret" , None ))
225
- mock_requests .get .configure_mock (** kwargs ['get' ])
226
- if 'post' in kwargs :
227
- mock_requests .post .reset_mock (side_effect = True , return_value = True )
228
- mock_requests .post .configure_mock (** kwargs ['post' ])
274
+ mock_requests .post .reset_mock (side_effect = True , return_value = True )
275
+ connection = Connection (kwargs ['url' ], auth_client_secret = kwargs .get ("auth_client_secret" , None ))
276
+ mock_requests .configure_mock (** kwargs ['requests' ])
229
277
self .check_connection_attributes (
230
278
connection ,
231
- url = kwargs .get ("url" , 'test_url/v1' ) ,
279
+ url = kwargs .get ("url" , 'test_url' ) + ' /v1' ,
232
280
timeout_config = kwargs .get ("timeout_config" , (2 , 20 )),
233
281
auth_expires = kwargs .get ("auth_expires" , 0 ),
234
282
auth_bearer = kwargs .get ("auth_bearer" , 0 ),
@@ -258,123 +306,66 @@ def helper_after_call(message, *args, **kwargs):
258
306
auth_client_secret = kwargs .get ("auth_client_secret" , None ),
259
307
is_authentication_required = kwargs .get ("is_authentication_required" , False ),
260
308
)
261
- mock_get_epoch_time .assert_called ()
262
309
if 'get' in kwargs :
263
310
mock_requests .get .assert_called_with (* kwargs ['get_args' ], ** kwargs ['get' ]) # only last call of this method
264
311
if 'post' in kwargs :
265
312
mock_requests .post .assert_called_with (* kwargs ['post_args' ], ** kwargs ['post' ]) # only last call of this method
266
313
267
- def mock_second_get_call (first_call_func , second_call_func , * args , ** kwargs ):
268
- """
269
- Mock different results from requests.get
270
- NOTE: Use >>mock_second_get_call.called = False before calling/using this function.
271
- It is used in the `helper_before_call`.
272
- """
273
-
274
- if mock_second_get_call .called :
275
- return second_call_func ()
276
- mock_second_get_call .called = True
277
- return first_call_func () # first call of request.get
278
-
279
- # requests.get exception (get data)
280
- connection = helper_before_call ('test_url' , get = {'side_effect' :RequestException ('Test!' )})
281
- with self .assertRaises (AuthenticationFailedException ) as error :
282
- connection ._refresh_authentication ()
283
- get_kwargs = {
284
- 'headers' : {"content-type" : "application/json" },
285
- 'timeout' : (30 , 45 )
286
- }
287
- helper_after_call (data_error_message , get_args = ["test_url/v1/.well-known/openid-configuration" ], get = get_kwargs )
288
-
289
- # bad status_code (get data)
290
- connection = helper_before_call ('test_url' , get = {'status_code' : 404 })
291
- with self .assertRaises (AuthenticationFailedException ) as error :
292
- connection ._refresh_authentication ()
293
- get_kwargs = {
294
- 'headers' : {"content-type" : "application/json" },
295
- 'timeout' : (30 , 45 )
296
- }
297
- helper_after_call (data_status_code_error_message , get_args = ["test_url/v1/.well-known/openid-configuration" ], get = get_kwargs )
298
-
299
314
# requests.get exception (get additional info)
300
- response = Mock (status_code = 200 )
301
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
302
- first_call_behaviour = lambda : response
303
- second_call_behaviour = lambda : exec ('raise RequestException("Test!")' )
304
315
connection = helper_before_call (
305
- 'test_url' ,
306
- get = {'side_effect' : lambda * args , ** kwargs : mock_second_get_call ( first_call_behaviour , second_call_behaviour )}
316
+ url = 'test_url' ,
317
+ requests = {'get. side_effect' : RequestException ( "Test!" )}
307
318
)
308
319
with self .assertRaises (AuthenticationFailedException ) as error :
309
- connection ._refresh_authentication ()
310
- get_kwargs = {
311
- 'headers' : {"content-type" : "application/json" },
312
- 'timeout' : (30 , 45 )
313
- }
320
+ connection ._set_bearer ('test_id' , 'test_href' )
314
321
helper_after_call (add_info_error_message , get_args = ["test_href" ], get = get_kwargs )
315
322
316
323
# bad status_code (get additional info)
317
- response = Mock (status_code = 200 )
318
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
319
- first_call_behaviour = lambda : response
320
- second_call_behaviour = lambda : Mock (status_code = 204 )
321
324
connection = helper_before_call (
322
- 'test_url' ,
323
- get = {'side_effect ' : lambda * args , ** kwargs : mock_second_get_call ( first_call_behaviour , second_call_behaviour )}
325
+ url = 'test_url' ,
326
+ requests = {'get.return_value ' : Mock ( status_code = 204 )}
324
327
)
325
328
with self .assertRaises (AuthenticationFailedException ) as error :
326
- connection ._refresh_authentication ()
327
- get_kwargs = {
328
- 'headers' : {"content-type" : "application/json" },
329
- 'timeout' : (30 , 45 )
330
- }
329
+ connection ._set_bearer ('test_id' , 'test_href' )
331
330
helper_after_call (add_info_status_code_error_message , get_args = ["test_href" ], get = get_kwargs )
332
331
333
332
# client_credentials error
334
- response = Mock (status_code = 200 )
335
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
336
- first_call_behaviour = lambda : response
337
333
request_third_part = Mock (status_code = 200 )
338
334
request_third_part .json .return_value = {'grant_types_supported' : {'Test_key' : 'Test_value' }}
339
- second_call_behaviour = lambda : request_third_part
340
335
connection = helper_before_call (
341
- 'test_url' ,
342
- get = {'side_effect ' : lambda * args , ** kwargs : mock_second_get_call ( first_call_behaviour , second_call_behaviour ) }
336
+ url = 'test_url' ,
337
+ requests = {'get.return_value ' : request_third_part }
343
338
)
344
339
with self .assertRaises (AuthenticationFailedException ) as error :
345
- connection ._refresh_authentication ( )
340
+ connection ._set_bearer ( 'test_id' , 'test_href' )
346
341
get_kwargs = {
347
342
'headers' : {"content-type" : "application/json" },
348
343
'timeout' : (30 , 45 )
349
344
}
350
345
helper_after_call (credentials_error_message , get_args = ["test_href" ], get = get_kwargs )
351
346
352
347
# OAuth error
353
- response = Mock (status_code = 200 )
354
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
355
- first_call_behaviour = lambda : response
356
348
request_third_part = Mock (status_code = 200 )
357
349
request_third_part .json .return_value = {
358
350
'grant_types_supported' : {'client_credentials' : 'Test_cred!' },
359
351
'token_endpoint' : 'Test' }
360
- second_call_behaviour = lambda : request_third_part
361
- mock_auth = Mock ()
362
- mock_auth .get_credentials .return_value = {'test_key' : 'Value' }
352
+ mock_auth = Mock (** {'get_credentials.return_value' : {'test_key' : 'Value' }})
363
353
connection = helper_before_call (
364
- 'test_url' ,
354
+ url = 'test_url' ,
365
355
auth_client_secret = mock_auth ,
366
- get = {'side_effect' : lambda * args , ** kwargs : mock_second_get_call (first_call_behaviour , second_call_behaviour )},
367
- post = {'side_effect' : RequestException ('Test' )}
368
- )
356
+ requests = {
357
+ 'get.return_value' : request_third_part ,
358
+ 'post.side_effect' : RequestException ('Test' )}
359
+ )
369
360
with self .assertRaises (AuthenticationFailedException ) as error :
370
- connection ._refresh_authentication ( )
361
+ connection ._set_bearer ( 'test_id' , 'test_href' )
371
362
get_kwargs = {
372
363
'headers' : {"content-type" : "application/json" },
373
364
'timeout' : (30 , 45 )
374
365
}
375
366
get_args = ["test_href" ]
376
367
post_kwargs = {'timeout' : (30 , 45 )}
377
- post_args = ["Test" , {'client_id' : 'Test!ID ' , 'test_key' : 'Value' }]
368
+ post_args = ["Test" , {'client_id' : 'test_id ' , 'test_key' : 'Value' }]
378
369
helper_after_call (
379
370
oauth_error_message ,
380
371
get = get_kwargs ,
@@ -385,24 +376,20 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
385
376
)
386
377
387
378
# OAuth status_code error
388
- response = Mock (status_code = 200 )
389
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
390
- first_call_behaviour = lambda : response
391
379
request_third_part = Mock (status_code = 200 )
392
380
request_third_part .json .return_value = {
393
381
'grant_types_supported' : {'client_credentials' : 'Test_cred!' },
394
382
'token_endpoint' : 'Test' }
395
- second_call_behaviour = lambda : request_third_part
396
- mock_auth = Mock ()
397
- mock_auth .get_credentials .return_value = {'test_key' : 'Value' }
383
+ mock_auth = Mock (** {'get_credentials.return_value' : {'test_key' : 'Value' }})
398
384
connection = helper_before_call (
399
- 'test_url' ,
385
+ url = 'test_url' ,
400
386
auth_client_secret = mock_auth ,
401
- get = {'side_effect' : lambda * args , ** kwargs : mock_second_get_call (first_call_behaviour , second_call_behaviour )},
402
- post = {'return_value' : Mock (status_code = 401 )}
403
- )
387
+ requests = {
388
+ 'get.return_value' : request_third_part ,
389
+ 'post.return_value' : Mock (status_code = 401 )}
390
+ )
404
391
with self .assertRaises (AuthenticationFailedException ) as error :
405
- connection ._refresh_authentication ( )
392
+ connection ._set_bearer ( 'Test!ID' , 'test_href' )
406
393
get_kwargs = {
407
394
'headers' : {"content-type" : "application/json" },
408
395
'timeout' : (30 , 45 )
@@ -420,28 +407,24 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
420
407
)
421
408
422
409
# valid call
423
- response = Mock (status_code = 200 )
424
- response .json .return_value = {'clientId' : 'Test!ID' , 'href' : "test_href" }
425
- first_call_behaviour = lambda : response
426
410
request_third_part = Mock (status_code = 200 )
427
411
request_third_part .json .return_value = {
428
412
'grant_types_supported' : {'client_credentials' : 'Test_cred!' },
429
413
'token_endpoint' : 'Test' }
430
- second_call_behaviour = lambda : request_third_part
431
- mock_auth = Mock ()
432
- mock_auth .get_credentials .return_value = {'test_key' : 'Value' }
414
+ mock_auth = Mock (** {'get_credentials.return_value' : {'test_key' : 'Value' }})
433
415
mock_post_response = Mock (status_code = 400 )
434
416
mock_post_response .json .return_value = {
435
417
'access_token' : 'TestBearer!' ,
436
418
'expires_in' : 1234
437
419
}
438
420
connection = helper_before_call (
439
- 'test_url' ,
421
+ url = 'test_url' ,
440
422
auth_client_secret = mock_auth ,
441
- get = {'side_effect' : lambda * args , ** kwargs : mock_second_get_call (first_call_behaviour , second_call_behaviour )},
442
- post = {'return_value' : mock_post_response }
423
+ requests = {
424
+ 'get.return_value' : request_third_part ,
425
+ 'post.return_value' : mock_post_response }
443
426
)
444
- connection ._refresh_authentication ( )
427
+ connection ._set_bearer ( 'Test!ID' , 'test_href' )
445
428
get_kwargs = {
446
429
'headers' : {"content-type" : "application/json" },
447
430
'timeout' : (30 , 45 )
@@ -460,8 +443,6 @@ def mock_second_get_call(first_call_func, second_call_func, *args, **kwargs):
460
443
auth_bearer = 'TestBearer!'
461
444
)
462
445
463
-
464
-
465
446
def test_timeout_config (self ):
466
447
"""
467
448
Test the setter and getter of `timeout_config`.
0 commit comments