Skip to content

Commit 755cb20

Browse files
committed
adjust message and add test for missing auto_encryption_opts error
1 parent bbf38be commit 755cb20

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

django_mongodb_backend/schema.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,13 @@ def _create_collection(self, model):
468468
auto_encryption_opts = getattr(client._options, "auto_encryption_opts", None)
469469
if not auto_encryption_opts:
470470
raise ImproperlyConfigured(
471-
f"Encrypted fields found but DATABASES['{self.connection.alias}']['OPTIONS'] "
472-
"is missing auto_encryption_opts."
471+
f"Tried to create model {model._meta.label} in "
472+
f"'{self.connection.alias}' database. The model has "
473+
"encrypted fields but "
474+
f"DATABASES['{self.connection.alias}']['OPTIONS'] is "
475+
'missing the "auto_encryption_opts" parameter. If the '
476+
"model should not be created in this database, adjust "
477+
"your database routers."
473478
)
474479
encrypted_fields = self._get_encrypted_fields(model)
475480
db.create_collection(db_table, encryptedFields=encrypted_fields)

tests/encryption_/test_schema.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from bson.binary import Binary
2+
from django.core.exceptions import ImproperlyConfigured
23
from django.db import connections
34

45
from . import models
@@ -134,3 +135,18 @@ def test_key_creation_and_lookup(self):
134135
key = EncryptionKey.objects.get(key_alt_name=test_key_alt_name)
135136
self.assertEqual(key.id, field_info["keyId"])
136137
self.assertEqual(key.key_alt_name, [test_key_alt_name])
138+
139+
def test_missing_auto_encryption_opts(self):
140+
connection = connections["default"]
141+
msg = (
142+
"Tried to create model encryption_.Patient in 'default' database. "
143+
"The model has encrypted fields but DATABASES['default']['OPTIONS'] "
144+
'is missing the "auto_encryption_opts" parameter. If the model '
145+
"should not be created in this database, adjust your database "
146+
"routers."
147+
)
148+
with (
149+
self.assertRaisesMessage(ImproperlyConfigured, msg),
150+
connection.schema_editor() as editor,
151+
):
152+
editor.create_model(models.Patient)

0 commit comments

Comments
 (0)