2929 "bsonType" : "string" ,
3030 "path" : "cc_type" ,
3131 "queries" : {"queryType" : "equality" },
32- "keyId" : Binary (b" \x90 1\x89 \x1f \xaf AX\x9b *\xb1 \xc7 \xc5 \xfd l\xa4 " , 4 ),
32+ # "keyId": Binary(b" \x901\x89\x1f\xafAX\x9b*\xb1\xc7\xc5\xfdl\xa4", 4),
3333 },
3434 {
3535 "bsonType" : "long" ,
3636 "path" : "cc_number" ,
3737 "queries" : {"queryType" : "equality" },
38- "keyId" : Binary (b"\x97 \xb4 \x9d \xb8 \xd5 \xa6 Ay\x85 \xfe \x00 \xc0 \xd4 {\xa2 \xff " , 4 ),
38+ # "keyId": Binary(b"\x97\xb4\x9d\xb8\xd5\xa6Ay\x85\xfe\x00\xc0\xd4{\xa2\xff", 4),
3939 },
4040 {
4141 "bsonType" : "decimal" ,
4242 "path" : "account_balance" ,
4343 "queries" : {"queryType" : "range" },
44- "keyId" : Binary (b"\xcc \x01 -s\xea \xd9 B\x8d \x80 \xd7 \xf8 !n\xc6 \xf5 U" , 4 ),
44+ # "keyId": Binary(b"\xcc\x01-s\xea\xd9B\x8d\x80\xd7\xf8!n\xc6\xf5U", 4),
4545 },
4646 ]
4747 },
5151 "bsonType" : "string" ,
5252 "path" : "ssn" ,
5353 "queries" : {"queryType" : "equality" },
54- "keyId" : Binary (b"\x14 F\x89 \xde \x8d \x04 K7\xa9 \x9a \xaf _\xca \x8a \xfb &" , 4 ),
54+ # "keyId": Binary(b"\x14F\x89\xde\x8d\x04K7\xa9\x9a\xaf_\xca\x8a\xfb&", 4),
5555 },
5656 {
5757 "bsonType" : "date" ,
5858 "path" : "birth_date" ,
5959 "queries" : {"queryType" : "range" },
60- "keyId" : Binary (b"@\xdd \xb4 \xd2 %\xc2 B\x94 \xb5 \x07 \xbc (ER[s" , 4 ),
60+ # "keyId": Binary(b"@\xdd\xb4\xd2%\xc2B\x94\xb5\x07\xbc(ER[s", 4),
6161 },
6262 {
6363 "bsonType" : "binData" ,
6464 "path" : "profile_picture" ,
6565 "queries" : {"queryType" : "equality" },
66- "keyId" : Binary (b"Q\xa2 \xeb c!\xec D,\x8b \xe4 $\xb6 ul9\x9a " , 4 ),
66+ # "keyId": Binary(b"Q\xa2\xebc!\xecD,\x8b\xe4$\xb6ul9\x9a", 4),
6767 },
6868 {
6969 "bsonType" : "int" ,
7070 "path" : "patient_age" ,
7171 "queries" : {"queryType" : "range" },
72- "keyId" : Binary (b"\r o\x80 \x1e \x8e 1K\xde \xbc _\xc3 bi\x95 \xa6 j" , 4 ),
72+ # "keyId": Binary(b"\ro\x80\x1e\x8e1K\xde\xbc_\xc3bi\x95\xa6j", 4),
7373 },
7474 {
7575 "bsonType" : "double" ,
7676 "path" : "weight" ,
7777 "queries" : {"queryType" : "range" },
78- "keyId" : Binary (b"\x9b \xfd :n\xe1 \xd0 N\xdd \xb3 \xe7 e)\x06 \xea \x8a \x1d " , 4 ),
78+ # "keyId": Binary(b"\x9b\xfd:n\xe1\xd0N\xdd\xb3\xe7e)\x06\xea\x8a\x1d", 4),
7979 },
8080 ]
8181 },
8585 "bsonType" : "int" ,
8686 "path" : "patient_id" ,
8787 "queries" : {"queryType" : "equality" },
88- "keyId" : Binary (b"\x8f t\x16 :\x8a \x91 D\xc7 \x8a \xdf \xe5 O\n [\xfd \\ " , 4 ),
88+ # "keyId": Binary(b"\x8ft\x16:\x8a\x91D\xc7\x8a\xdf\xe5O\n[\xfd\\", 4),
8989 },
9090 {
9191 "bsonType" : "string" ,
9292 "path" : "patient_name" ,
93- "keyId" : Binary (b"<\x9b \xba \xeb :\xa4 @m\x93 \x0e \x0c \xca N\x03 \xfb \x05 " , 4 ),
93+ # "keyId": Binary(b"<\x9b\xba\xeb:\xa4@m\x93\x0e\x0c\xcaN\x03\xfb\x05", 4),
9494 },
9595 {
9696 "bsonType" : "string" ,
9797 "path" : "patient_notes" ,
9898 "queries" : {"queryType" : "equality" },
99- "keyId" : Binary (b"\x01 \xe7 \xd1 isnB$\xa9 (gwO\xca \x10 \xbd " , 4 ),
99+ # "keyId": Binary(b"\x01\xe7\xd1isnB$\xa9(gwO\xca\x10\xbd", 4),
100100 },
101101 {
102102 "bsonType" : "date" ,
103103 "path" : "registration_date" ,
104104 "queries" : {"queryType" : "equality" },
105- "keyId" : Binary (b"F\xfb \xae \x82 \xd5 \x9a @\xee \xbf J\xaf #\x9c :-I" , 4 ),
105+ # "keyId": Binary(b"F\xfb\xae\x82\xd5\x9a@\xee\xbfJ\xaf#\x9c:-I", 4),
106106 },
107107 {
108108 "bsonType" : "bool" ,
109109 "path" : "is_active" ,
110110 "queries" : {"queryType" : "equality" },
111- "keyId" : Binary (b"\xb2 \xb5 \xc4 K53A\xda \xb9 V\xa6 \xa9 \x97 \x94 \xea ;" , 4 ),
111+ # "keyId": Binary(b"\xb2\xb5\xc4K53A\xda\xb9V\xa6\xa9\x97\x94\xea;", 4),
112112 },
113+ {"bsonType" : "string" , "path" : "email" , "queries" : {"queryType" : "equality" }},
113114 ]
114115 },
116+ "encryption__patientportaluser" : {
117+ "fields" : [
118+ {"bsonType" : "string" , "path" : "ip_address" , "queries" : {"queryType" : "equality" }},
119+ {"bsonType" : "string" , "path" : "url" , "queries" : {"queryType" : "equality" }},
120+ ]
121+ },
122+ "encryption__encryptednumbers" : {
123+ "fields" : [
124+ {"bsonType" : "int" , "path" : "pos_bigint" , "queries" : {"queryType" : "equality" }},
125+ {"bsonType" : "int" , "path" : "pos_smallint" , "queries" : {"queryType" : "equality" }},
126+ {"bsonType" : "int" , "path" : "smallint" , "queries" : {"queryType" : "equality" }},
127+ ]
128+ },
129+ "encryption__appointment" : {
130+ "fields" : [{"bsonType" : "date" , "path" : "time" , "queries" : {"queryType" : "equality" }}]
131+ },
115132}
116133
117134
@@ -144,7 +161,7 @@ def reload_module(module):
144161)
145162@override_settings (DATABASE_ROUTERS = [TestEncryptedRouter ()])
146163class EncryptedFieldTests (TransactionTestCase ):
147- databases = {"default" , "other " }
164+ databases = {"default" , "encrypted " }
148165 available_apps = ["django_mongodb_backend" , "encryption_" ]
149166
150167 def setUp (self ):
@@ -259,7 +276,7 @@ def test_get_encrypted_fields_map(self):
259276 },
260277 }
261278 self .maxDiff = None
262- with connections ["other " ].schema_editor () as editor :
279+ with connections ["encrypted " ].schema_editor () as editor :
263280 db_table = self .patient ._meta .db_table
264281 self .assertEqual (
265282 editor ._get_encrypted_fields_map (self .patient ),
@@ -272,11 +289,18 @@ def test_show_schema_map(self):
272289 call_command (
273290 "showschemamap" ,
274291 "--database" ,
275- "other " ,
292+ "encrypted " ,
276293 verbosity = 0 ,
277294 stdout = out ,
278295 )
279- self .assertEqual (json_util .dumps (EXPECTED_ENCRYPTED_FIELDS_MAP , indent = 2 ), out .getvalue ())
296+ # Remove keyIds since they are different for each run.
297+ output_json = json_util .loads (out .getvalue ())
298+ for table in output_json :
299+ for field in output_json [table ]["fields" ]:
300+ del field ["keyId" ]
301+ # TODO: probably we don't need to test the entire mapping, otherwise it
302+ # requires updates every time a new model or field is added!
303+ self .assertEqual (EXPECTED_ENCRYPTED_FIELDS_MAP , output_json )
280304
281305 def test_set_encrypted_fields_map_in_client (self ):
282306 # TODO: Create new client with and without schema map provided then
@@ -315,16 +339,17 @@ def test_patientrecord(self):
315339 self .assertTrue (PatientRecord .objects .filter (weight__gte = 175.0 ).exists ())
316340
317341 # Test encrypted patient record in unencrypted database.
318- conn_params = connections ["other " ].get_connection_params ()
342+ conn_params = connections ["encrypted " ].get_connection_params ()
319343 if conn_params .pop ("auto_encryption_opts" , False ):
320344 # Call MongoClient instead of get_new_connection because
321345 # get_new_connection will return the encrypted connection
322346 # from the connection pool.
323- connection = pymongo .MongoClient (** conn_params )
324- patientrecords = connection ["test_other" ].encryption__patientrecord .find ()
325- ssn = patientrecords [0 ]["ssn" ]
326- self .assertTrue (isinstance (ssn , Binary ))
327- connection .close ()
347+ with pymongo .MongoClient (** conn_params ) as new_connection :
348+ patientrecords = new_connection [
349+ "test_django_encrypted"
350+ ].encryption__patientrecord .find ()
351+ ssn = patientrecords [0 ]["ssn" ]
352+ self .assertTrue (isinstance (ssn , Binary ))
328353
329354 def test_patient (self ):
330355 self .assertEqual (
@@ -343,9 +368,9 @@ def test_patient(self):
343368 )
344369
345370 # Test decrypted patient record in encrypted database.
346- patients = connections ["other " ].database .encryption__patient .find ()
371+ patients = connections ["encrypted " ].database .encryption__patient .find ()
347372 self .assertEqual (len (list (patients )), 1 )
348- records = connections ["other " ].database .encryption__patientrecord .find ()
373+ records = connections ["encrypted " ].database .encryption__patientrecord .find ()
349374 self .assertTrue ("__safeContent__" in records [0 ])
350375
351376
0 commit comments