|
1 | 1 | from io import StringIO |
2 | 2 |
|
3 | 3 | from bson import json_util |
| 4 | +from django.core.exceptions import ImproperlyConfigured |
4 | 5 | from django.core.management import call_command |
| 6 | +from django.db import connections |
5 | 7 | from django.test import modify_settings |
6 | 8 |
|
| 9 | +from .models import EncryptionKey |
7 | 10 | from .test_base import EncryptionTestCase |
8 | 11 |
|
9 | 12 |
|
@@ -110,23 +113,23 @@ def test_show_encrypted_fields_map(self): |
110 | 113 | self.assertIn(model_key, command_output) |
111 | 114 | self._compare_output(expected, command_output[model_key]) |
112 | 115 |
|
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