12
12
import pytest
13
13
from bson import DBRef , ObjectId
14
14
from bson .decimal128 import Decimal128
15
- from pymongo .errors import ServerSelectionTimeoutError
15
+ from pymongo .errors import OperationFailure
16
16
17
17
from connectors .protocol import Filter
18
18
from connectors .source import ConfigurableFieldValueError
@@ -261,7 +261,13 @@ def future_with_result(result):
261
261
262
262
263
263
@pytest .mark .asyncio
264
- async def test_validate_config_when_database_name_invalid_then_raises_exception ():
264
+ @mock .patch (
265
+ "motor.motor_asyncio.AsyncIOMotorDatabase.validate_collection" ,
266
+ side_effect = OperationFailure ("Unauthorized" ),
267
+ )
268
+ async def test_validate_config_when_database_name_invalid_then_raises_exception (
269
+ patch_validate_collection ,
270
+ ):
265
271
server_database_names = ["hello" , "world" ]
266
272
configured_database_name = "something"
267
273
@@ -283,7 +289,13 @@ async def test_validate_config_when_database_name_invalid_then_raises_exception(
283
289
284
290
285
291
@pytest .mark .asyncio
286
- async def test_validate_config_when_collection_name_invalid_then_raises_exception ():
292
+ @mock .patch (
293
+ "motor.motor_asyncio.AsyncIOMotorDatabase.validate_collection" ,
294
+ side_effect = OperationFailure ("Unauthorized" ),
295
+ )
296
+ async def test_validate_config_when_collection_name_invalid_then_raises_exception (
297
+ patch_validate_collection ,
298
+ ):
287
299
server_database_names = ["hello" ]
288
300
server_collection_names = ["first" , "second" ]
289
301
configured_database_name = "hello"
@@ -310,25 +322,76 @@ async def test_validate_config_when_collection_name_invalid_then_raises_exceptio
310
322
311
323
312
324
@pytest .mark .asyncio
313
- async def test_validate_config_when_configuration_valid_then_does_not_raise ():
314
- server_database_names = ["hello" ]
315
- server_collection_names = ["first" , "second" ]
325
+ @mock .patch (
326
+ "motor.motor_asyncio.AsyncIOMotorDatabase.validate_collection" ,
327
+ side_effect = OperationFailure ("Unauthorized" ),
328
+ )
329
+ @mock .patch (
330
+ "motor.motor_asyncio.AsyncIOMotorClient.list_database_names" ,
331
+ return_value = future_with_result (["hello" ]),
332
+ )
333
+ @mock .patch (
334
+ "motor.motor_asyncio.AsyncIOMotorDatabase.list_collection_names" ,
335
+ return_value = future_with_result (["first" ]),
336
+ )
337
+ async def test_validate_config_when_collection_access_unauthorized (
338
+ patch_validate_collection , patch_list_database_names , patch_list_collection_names
339
+ ):
316
340
configured_database_name = "hello"
317
341
configured_collection_name = "second"
318
342
319
- with mock .patch (
320
- "motor.motor_asyncio.AsyncIOMotorClient.list_database_names" ,
321
- return_value = future_with_result (server_database_names ),
322
- ), mock .patch (
323
- "motor.motor_asyncio.AsyncIOMotorDatabase.list_collection_names" ,
324
- return_value = future_with_result (server_collection_names ),
325
- ):
343
+ with pytest .raises (ConfigurableFieldValueError ) as e :
344
+ async with create_mongo_source (
345
+ database = configured_database_name ,
346
+ collection = configured_collection_name ,
347
+ ) as source :
348
+ await source .validate_config ()
349
+
350
+ assert e is not None
351
+
352
+
353
+ @pytest .mark .asyncio
354
+ @mock .patch (
355
+ "motor.motor_asyncio.AsyncIOMotorDatabase.validate_collection" ,
356
+ side_effect = OperationFailure ("Unauthorized" ),
357
+ )
358
+ @mock .patch (
359
+ "motor.motor_asyncio.AsyncIOMotorClient.list_database_names" ,
360
+ side_effect = OperationFailure ("Unauthorized" ),
361
+ )
362
+ async def test_validate_config_when_collection_access_unauthorized_and_no_admin_access (
363
+ patch_validate_collection , patch_list_database_names
364
+ ):
365
+ configured_database_name = "hello"
366
+ configured_collection_name = "second"
367
+
368
+ with pytest .raises (ConfigurableFieldValueError ) as e :
326
369
async with create_mongo_source (
327
370
database = configured_database_name ,
328
371
collection = configured_collection_name ,
329
372
) as source :
330
373
await source .validate_config ()
331
374
375
+ assert e is not None
376
+
377
+
378
+ @pytest .mark .asyncio
379
+ @mock .patch (
380
+ "motor.motor_asyncio.AsyncIOMotorDatabase.validate_collection" ,
381
+ return_value = future_with_result (None ),
382
+ )
383
+ async def test_validate_config_when_configuration_valid_then_does_not_raise (
384
+ patch_validate_connection ,
385
+ ):
386
+ configured_database_name = "hello"
387
+ configured_collection_name = "second"
388
+
389
+ async with create_mongo_source (
390
+ database = configured_database_name ,
391
+ collection = configured_collection_name ,
392
+ ) as source :
393
+ await source .validate_config ()
394
+
332
395
333
396
@pytest .mark .asyncio
334
397
@pytest .mark .parametrize (
@@ -347,22 +410,6 @@ async def test_serialize(raw, output):
347
410
assert source .serialize (raw ) == output
348
411
349
412
350
- @pytest .mark .asyncio
351
- @mock .patch ("ssl.SSLContext.load_verify_locations" )
352
- async def test_ssl_connection_with_invalid_certificate (mock_ssl ):
353
- mock_ssl .return_value = True
354
- async with create_mongo_source (
355
- ssl_enabled = True ,
356
- ssl_ca = "-----BEGIN CERTIFICATE----- Invalid-Certificate -----END CERTIFICATE-----" ,
357
- ) as source :
358
- with mock .patch (
359
- "motor.motor_asyncio.AsyncIOMotorClient.list_database_names" ,
360
- side_effect = ServerSelectionTimeoutError (),
361
- ):
362
- with pytest .raises (ServerSelectionTimeoutError ):
363
- await source .validate_config ()
364
-
365
-
366
413
@pytest .mark .asyncio
367
414
@pytest .mark .parametrize (
368
415
"certificate_value, tls_insecure" ,
@@ -379,19 +426,12 @@ async def test_ssl_connection_with_invalid_certificate(mock_ssl):
379
426
),
380
427
],
381
428
)
382
- @mock .patch ("ssl.SSLContext.load_verify_locations" )
429
+ @mock .patch ("ssl.SSLContext.load_verify_locations" , return_value = True )
383
430
async def test_ssl_with_successful_connection (
384
431
mock_ssl , certificate_value , tls_insecure
385
432
):
386
- mock_ssl .return_value = True
387
433
async with create_mongo_source (
388
- ssl_enabled = True , ssl_ca = certificate_value , tls_insecure = tls_insecure
389
- ) as source :
390
- with mock .patch (
391
- "motor.motor_asyncio.AsyncIOMotorClient.list_database_names" ,
392
- return_value = future_with_result (["db" ]),
393
- ), mock .patch (
394
- "motor.motor_asyncio.AsyncIOMotorDatabase.list_collection_names" ,
395
- return_value = future_with_result (["col" ]),
396
- ):
397
- await source .validate_config ()
434
+ ssl_enabled = True ,
435
+ ssl_ca = "-----BEGIN CERTIFICATE----- Invalid-Certificate -----END CERTIFICATE-----" ,
436
+ ):
437
+ assert True
0 commit comments