File tree Expand file tree Collapse file tree 2 files changed +18
-20
lines changed Expand file tree Collapse file tree 2 files changed +18
-20
lines changed Original file line number Diff line number Diff line change @@ -421,20 +421,26 @@ def _field_should_have_unique(self, field):
421
421
422
422
def _create_collection (self , model ):
423
423
"""
424
- Create a collection or encrypted collection for the model.
424
+ If the model is not encrypted, create a normal collection otherwise
425
+ create an encrypted collection with the encrypted fields map.
425
426
"""
426
427
427
- if hasattr (model , "encrypted" ):
428
+ if not hasattr (model , "encrypted" ):
429
+ self .get_database ().create_collection (model ._meta .db_table )
430
+ else :
431
+ # TODO: Route to the encrypted database connection.
428
432
auto_encryption_opts = self .connection .settings_dict .get ("OPTIONS" , {}).get (
429
433
"auto_encryption_opts"
430
434
)
431
435
client = self .connection .connection
436
+
432
437
client_encryption = get_client_encryption (auto_encryption_opts , client )
433
438
client_encryption .create_encrypted_collection (
434
439
client .database ,
435
440
model ._meta .db_table ,
436
- { "fields" : []} ,
441
+ self . _get_encrypted_fields_map ( model ) ,
437
442
"local" , # TODO: KMS provider should be configurable
438
443
)
439
- else :
440
- self .get_database ().create_collection (model ._meta .db_table )
444
+
445
+ def _get_encrypted_fields_map (self , model ):
446
+ return {"fields" : []}
Original file line number Diff line number Diff line change
1
+ from django .db import connection
1
2
from django .test import TestCase
2
3
3
4
from .models import Person
4
5
5
6
6
7
class EncryptedModelTests (TestCase ):
7
- databases = ["encryption" ]
8
-
9
8
@classmethod
10
9
def setUpTestData (cls ):
11
- cls .objs = [Person .objects .create ()]
12
-
13
- def test_encrypted_fields_map_on_class (self ):
14
- expected = {
15
- "fields" : {
16
- "ssn" : "EncryptedCharField" ,
17
- }
18
- }
19
- self .assertEqual (Person .encrypted_fields_map , expected )
10
+ cls .person = Person (ssn = "123-45-6789" )
20
11
21
12
def test_encrypted_fields_map_on_instance (self ):
22
- instance = Person (ssn = "123-45-6789" )
23
13
expected = {
24
14
"fields" : {
25
15
"ssn" : "EncryptedCharField" ,
26
16
}
27
17
}
28
- self .assertEqual (instance .encrypted_fields_map , expected )
18
+ with connection .schema_editor () as editor :
19
+ self .assertEqual (editor ._get_encrypted_fields_map (self .person ), expected )
29
20
30
21
def test_non_encrypted_fields_not_included (self ):
31
- encrypted_field_names = Person .encrypted_fields_map .get ("fields" ).keys ()
32
- self .assertNotIn ("name" , encrypted_field_names )
22
+ with connection .schema_editor () as editor :
23
+ encrypted_field_names = editor ._get_encrypted_fields_map (self .person ).get ("fields" )
24
+ self .assertNotIn ("name" , encrypted_field_names )
You can’t perform that action at this time.
0 commit comments