Skip to content

Commit c89be3d

Browse files
committed
Fix test_management
1 parent 3fc2d77 commit c89be3d

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

tests/encryption_/test_base.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import os
2+
13
import pymongo
24
from bson.binary import Binary
35
from django.conf import settings
@@ -19,3 +21,18 @@ def assertEncrypted(self, model, field):
1921
collection = db[model._meta.db_table]
2022
data = collection.find_one({}, {field: 1, "_id": 0})
2123
self.assertIsInstance(data[field], Binary)
24+
25+
def __init__(self, *args, **kwargs):
26+
super().__init__(*args, **kwargs)
27+
28+
AWS_CREDS = {
29+
"accessKeyId": os.environ.get("FLE_AWS_KEY", ""),
30+
"secretAccessKey": os.environ.get("FLE_AWS_SECRET", ""),
31+
}
32+
_USE_AWS_KMS = any(AWS_CREDS.values())
33+
34+
if _USE_AWS_KMS:
35+
self.DEFAULT_KMS_PROVIDER = "aws"
36+
else:
37+
# Local-only fallback
38+
self.DEFAULT_KMS_PROVIDER = "local"

tests/encryption_/test_management.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from io import StringIO
22

33
from bson import json_util
4+
from django.core.exceptions import ImproperlyConfigured
45
from django.core.management import call_command
6+
from django.db import connections
57
from django.test import modify_settings
68

9+
from .models import EncryptionKey
710
from .test_base import EncryptionTestCase
811

912

@@ -110,23 +113,23 @@ def test_show_encrypted_fields_map(self):
110113
self.assertIn(model_key, command_output)
111114
self._compare_output(expected, command_output[model_key])
112115

113-
# FIXME ValueError: master_key is required for kms_provider: 'aws'
114-
#
115-
# Get master_key from KMS_CREDENTIALS["aws"]["key"] and pass to create_data_key
116-
#
117-
# def test_missing_key(self):
118-
# test_key = "encryption__patient.patient_record.ssn"
119-
# msg = (
120-
# f"Encryption key {test_key} not found. Have migrated the "
121-
# "<class 'encryption_.models.PatientRecord'> model?"
122-
# )
123-
# EncryptionKey.objects.filter(key_alt_name=test_key).delete()
124-
# try:
125-
# with self.assertRaisesMessage(ImproperlyConfigured, msg):
126-
# call_command("showencryptedfieldsmap", "--database", "encrypted", verbosity=0)
127-
# finally:
128-
# # Replace the deleted key.
129-
# connections["encrypted"].client_encryption.create_data_key(
130-
# kms_provider="aws",
131-
# key_alt_names=[test_key],
132-
# )
116+
def test_missing_key(self):
117+
test_key = "encryption__patient.patient_record.ssn"
118+
msg = (
119+
f"Encryption key {test_key} not found. Have migrated the "
120+
"<class 'encryption_.models.PatientRecord'> model?"
121+
)
122+
EncryptionKey.objects.filter(key_alt_name=test_key).delete()
123+
try:
124+
with self.assertRaisesMessage(ImproperlyConfigured, msg):
125+
call_command("showencryptedfieldsmap", "--database", "encrypted", verbosity=0)
126+
finally:
127+
# Replace the deleted key.
128+
master_key = connections["encrypted"].settings_dict["KMS_CREDENTIALS"][
129+
self.DEFAULT_KMS_PROVIDER
130+
]
131+
connections["encrypted"].client_encryption.create_data_key(
132+
kms_provider=self.DEFAULT_KMS_PROVIDER,
133+
master_key=master_key,
134+
key_alt_names=[test_key],
135+
)

0 commit comments

Comments
 (0)