29
29
"bsonType" : "string" ,
30
30
"path" : "cc_type" ,
31
31
"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),
33
33
},
34
34
{
35
35
"bsonType" : "long" ,
36
36
"path" : "cc_number" ,
37
37
"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),
39
39
},
40
40
{
41
41
"bsonType" : "decimal" ,
42
42
"path" : "account_balance" ,
43
43
"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),
45
45
},
46
46
]
47
47
},
51
51
"bsonType" : "string" ,
52
52
"path" : "ssn" ,
53
53
"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),
55
55
},
56
56
{
57
57
"bsonType" : "date" ,
58
58
"path" : "birth_date" ,
59
59
"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),
61
61
},
62
62
{
63
63
"bsonType" : "binData" ,
64
64
"path" : "profile_picture" ,
65
65
"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),
67
67
},
68
68
{
69
69
"bsonType" : "int" ,
70
70
"path" : "patient_age" ,
71
71
"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),
73
73
},
74
74
{
75
75
"bsonType" : "double" ,
76
76
"path" : "weight" ,
77
77
"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),
79
79
},
80
80
]
81
81
},
85
85
"bsonType" : "int" ,
86
86
"path" : "patient_id" ,
87
87
"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),
89
89
},
90
90
{
91
91
"bsonType" : "string" ,
92
92
"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),
94
94
},
95
95
{
96
96
"bsonType" : "string" ,
97
97
"path" : "patient_notes" ,
98
98
"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),
100
100
},
101
101
{
102
102
"bsonType" : "date" ,
103
103
"path" : "registration_date" ,
104
104
"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),
106
106
},
107
107
{
108
108
"bsonType" : "bool" ,
109
109
"path" : "is_active" ,
110
110
"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),
112
112
},
113
+ {"bsonType" : "string" , "path" : "email" , "queries" : {"queryType" : "equality" }},
113
114
]
114
115
},
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
+ },
115
132
}
116
133
117
134
@@ -144,7 +161,7 @@ def reload_module(module):
144
161
)
145
162
@override_settings (DATABASE_ROUTERS = [TestEncryptedRouter ()])
146
163
class EncryptedFieldTests (TransactionTestCase ):
147
- databases = {"default" , "other " }
164
+ databases = {"default" , "encrypted " }
148
165
available_apps = ["django_mongodb_backend" , "encryption_" ]
149
166
150
167
def setUp (self ):
@@ -259,7 +276,7 @@ def test_get_encrypted_fields_map(self):
259
276
},
260
277
}
261
278
self .maxDiff = None
262
- with connections ["other " ].schema_editor () as editor :
279
+ with connections ["encrypted " ].schema_editor () as editor :
263
280
db_table = self .patient ._meta .db_table
264
281
self .assertEqual (
265
282
editor ._get_encrypted_fields_map (self .patient ),
@@ -272,11 +289,18 @@ def test_show_schema_map(self):
272
289
call_command (
273
290
"showschemamap" ,
274
291
"--database" ,
275
- "other " ,
292
+ "encrypted " ,
276
293
verbosity = 0 ,
277
294
stdout = out ,
278
295
)
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 )
280
304
281
305
def test_set_encrypted_fields_map_in_client (self ):
282
306
# TODO: Create new client with and without schema map provided then
@@ -315,16 +339,17 @@ def test_patientrecord(self):
315
339
self .assertTrue (PatientRecord .objects .filter (weight__gte = 175.0 ).exists ())
316
340
317
341
# Test encrypted patient record in unencrypted database.
318
- conn_params = connections ["other " ].get_connection_params ()
342
+ conn_params = connections ["encrypted " ].get_connection_params ()
319
343
if conn_params .pop ("auto_encryption_opts" , False ):
320
344
# Call MongoClient instead of get_new_connection because
321
345
# get_new_connection will return the encrypted connection
322
346
# 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 ))
328
353
329
354
def test_patient (self ):
330
355
self .assertEqual (
@@ -343,9 +368,9 @@ def test_patient(self):
343
368
)
344
369
345
370
# Test decrypted patient record in encrypted database.
346
- patients = connections ["other " ].database .encryption__patient .find ()
371
+ patients = connections ["encrypted " ].database .encryption__patient .find ()
347
372
self .assertEqual (len (list (patients )), 1 )
348
- records = connections ["other " ].database .encryption__patientrecord .find ()
373
+ records = connections ["encrypted " ].database .encryption__patientrecord .find ()
349
374
self .assertTrue ("__safeContent__" in records [0 ])
350
375
351
376
0 commit comments