@@ -146,7 +146,7 @@ def reload_module(module):
146
146
)
147
147
@override_settings (DATABASE_ROUTERS = [TestEncryptedRouter ()])
148
148
class EncryptedFieldTests (TransactionTestCase ):
149
- databases = {"default" , "my_encrypted_database " }
149
+ databases = {"default" , "other " }
150
150
available_apps = ["django_mongodb_backend" , "encryption_" ]
151
151
152
152
def setUp (self ):
@@ -216,7 +216,7 @@ def tearDownClass(cls):
216
216
217
217
def test_get_encrypted_fields_map_method (self ):
218
218
self .maxDiff = None
219
- with connections ["my_encrypted_database " ].schema_editor () as editor :
219
+ with connections ["other " ].schema_editor () as editor :
220
220
db_table = self .patient ._meta .db_table
221
221
self .assertCountEqual (
222
222
{"fields" : editor ._get_encrypted_fields_map (self .patient )},
@@ -241,7 +241,7 @@ def test_get_encrypted_fields_map_command(self):
241
241
call_command (
242
242
"get_encrypted_fields_map" ,
243
243
"--database" ,
244
- "my_encrypted_database " ,
244
+ "other " ,
245
245
verbosity = 0 ,
246
246
stdout = out ,
247
247
)
@@ -320,6 +320,18 @@ def test_patientrecord(self):
320
320
self .assertFalse (PatientRecord .objects .filter (patient_age__gte = 200 ).exists ())
321
321
self .assertTrue (PatientRecord .objects .filter (weight__gte = 175.0 ).exists ())
322
322
323
+ # Test encrypted patient record in unencrypted database.
324
+ conn_params = connections ["other" ].get_connection_params ()
325
+ if conn_params .pop ("auto_encryption_opts" , False ):
326
+ # Call MongoClient instead of get_new_connection because
327
+ # get_new_connection will return the encrypted connection
328
+ # from the connection pool.
329
+ connection = pymongo .MongoClient (** conn_params )
330
+ patientrecords = connection ["test_other" ].patientrecord .find ()
331
+ ssn = patientrecords [0 ]["ssn" ]
332
+ self .assertTrue (isinstance (ssn , Binary ))
333
+ connection .close ()
334
+
323
335
def test_patient (self ):
324
336
self .assertEqual (
325
337
Patient .objects .get (patient_notes = "patient notes " * 25 ).patient_notes ,
@@ -337,22 +349,24 @@ def test_patient(self):
337
349
)
338
350
339
351
# Test decrypted patient record in encrypted database.
340
- patients = connections ["my_encrypted_database " ].database .patient .find ()
352
+ patients = connections ["other " ].database .patient .find ()
341
353
self .assertEqual (len (list (patients )), 1 )
342
- records = connections ["my_encrypted_database " ].database .patientrecord .find ()
354
+ records = connections ["other " ].database .patientrecord .find ()
343
355
self .assertTrue ("__safeContent__" in records [0 ])
344
356
345
- # Test encrypted patient record in unencrypted database.
346
- conn_params = connections ["my_encrypted_database" ].get_connection_params ()
347
- if conn_params .pop ("auto_encryption_opts" , False ):
348
- # Call MongoClient instead of get_new_connection because
349
- # get_new_connection will return the encrypted connection
350
- # from the connection pool.
351
- connection = pymongo .MongoClient (** conn_params )
352
- patientrecords = connection ["test_my_encrypted_database" ].patientrecord .find ()
353
- ssn = patientrecords [0 ]["ssn" ]
354
- self .assertTrue (isinstance (ssn , Binary ))
355
- connection .close ()
357
+ def test_no_auto_encryption_opts (self ):
358
+ """
359
+ Ensure that the encrypted fields map is not set in the client
360
+ when auto_encryption_opts is not set.
361
+ """
362
+ Patient .objects .using ("default" ).create (
363
+ patient_id = 2 ,
364
+ patient_name = "John Doe 2" ,
365
+ patient_notes = "patient notes " * 20 ,
366
+ registration_date = datetime (2024 , 10 , 1 , 12 , 0 , 0 ),
367
+ is_active = True ,
368
+
369
+ ).save ()
356
370
357
371
358
372
class KMSCredentialsTests (TestCase ):
0 commit comments