Skip to content

Commit 9535992

Browse files
committed
make the test runner use a test key vault namespace
1 parent 415e4c5 commit 9535992

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

.github/workflows/encrypted_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"NAME": "djangotests_encrypted",
99
"OPTIONS": {
1010
"auto_encryption_opts": AutoEncryptionOpts(
11-
key_vault_namespace="test_djangotests_encrypted.__keyVault",
11+
key_vault_namespace="djangotests_encrypted.__keyVault",
1212
kms_providers={"local": {"key": os.urandom(96)}},
1313
),
1414
"directConnection": True,

django_mongodb_backend/creation.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
from django.conf import settings
2-
from django.db.backends.base.creation import BaseDatabaseCreation
2+
from django.db.backends.base.creation import TEST_DATABASE_PREFIX, BaseDatabaseCreation
33

44

55
class DatabaseCreation(BaseDatabaseCreation):
66
def _execute_create_test_db(self, cursor, parameters, keepdb=False):
77
# Close the connection (which may point to the non-test database) so
88
# that a new connection to the test database can be established later.
99
self.connection.close_pool()
10+
# Use a test _key_vault_namespace. This assumes the key vault database
11+
# is the same as the encrypted database so that _destroy_test_db() can
12+
# reset the collection by dropping it.
13+
opts = self.connection.settings_dict["OPTIONS"].get("auto_encryption_opts")
14+
if opts:
15+
self.connection.settings_dict["OPTIONS"][
16+
"auto_encryption_opts"
17+
]._key_vault_namespace = TEST_DATABASE_PREFIX + opts._key_vault_namespace
1018
if not keepdb:
1119
self._destroy_test_db(parameters["dbname"], verbosity=0)
1220

@@ -24,3 +32,9 @@ def destroy_test_db(self, old_database_name=None, verbosity=1, keepdb=False, suf
2432
super().destroy_test_db(old_database_name, verbosity, keepdb, suffix)
2533
# Close the connection to the test database.
2634
self.connection.close_pool()
35+
# Restore the original _key_vault_namespace.
36+
opts = self.connection.settings_dict["OPTIONS"].get("auto_encryption_opts")
37+
if opts:
38+
self.connection.settings_dict["OPTIONS"][
39+
"auto_encryption_opts"
40+
]._key_vault_namespace = opts._key_vault_namespace[len(TEST_DATABASE_PREFIX) :]

0 commit comments

Comments
 (0)