Skip to content

Commit ea923dc

Browse files
committed
Update test
1 parent 09fd8ec commit ea923dc

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

django_mongodb_backend/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from django.utils.functional import SimpleLazyObject
99
from django.utils.text import format_lazy
1010
from django.utils.version import get_version_tuple
11-
from pymongo.encryption_options import AutoEncryptionOpts
1211
from pymongo.uri_parser import parse_uri as pymongo_parse_uri
1312

1413

@@ -34,12 +33,14 @@ def get_auto_encryption_options(crypt_shared_lib_path=None):
3433
key_vault_collection_name = "__keyVault"
3534
key_vault_namespace = f"{key_vault_database_name}.{key_vault_collection_name}"
3635
kms_providers = {}
37-
return AutoEncryptionOpts(
38-
kms_providers, key_vault_namespace, crypt_shared_lib_path=crypt_shared_lib_path
39-
)
36+
return {
37+
"kms_providers": kms_providers,
38+
"key_vault_namespace": key_vault_namespace,
39+
"crypt_shared_lib_path": crypt_shared_lib_path,
40+
}
4041

4142

42-
def parse_uri(uri, *, db_name=None, test=None):
43+
def parse_uri(uri, *, auto_encryption_options=None, db_name=None, test=None):
4344
"""
4445
Convert the given uri into a dictionary suitable for Django's DATABASES
4546
setting.
@@ -59,14 +60,17 @@ def parse_uri(uri, *, db_name=None, test=None):
5960
db_name = db_name or uri["database"]
6061
if not db_name:
6162
raise ImproperlyConfigured("You must provide the db_name parameter.")
63+
options = uri.get("options")
64+
if auto_encryption_options:
65+
options = {**uri.get("options"), **auto_encryption_options}
6266
settings_dict = {
6367
"ENGINE": "django_mongodb_backend",
6468
"NAME": db_name,
6569
"HOST": host,
6670
"PORT": port,
6771
"USER": uri.get("username"),
6872
"PASSWORD": uri.get("password"),
69-
"OPTIONS": uri.get("options"),
73+
"OPTIONS": options,
7074
}
7175
if "authSource" not in settings_dict["OPTIONS"] and uri["database"]:
7276
settings_dict["OPTIONS"]["authSource"] = uri["database"]

tests/backend_/utils/test_parse_uri.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ def test_no_scheme(self):
9696
parse_uri("cluster0.example.mongodb.net")
9797

9898

99-
# TODO: This can go in `test_features` once transaction support is added.
99+
# TODO: This can be moved to `test_features` once transaction support is merged.
100100
class ParseUriOptionsTests(TestCase):
101101
@skipUnlessDBFeature("supports_queryable_encryption")
102-
def test_queryable_encryption_config(self):
102+
def test_auto_encryption_options(self):
103103
auto_encryption_options = get_auto_encryption_options()
104-
self.assertEqual(auto_encryption_options._key_vault_namespace, "encryption.__keyVault")
104+
settings_dict = parse_uri(
105+
"mongodb://cluster0.example.mongodb.net/myDatabase",
106+
auto_encryption_options=auto_encryption_options,
107+
)
108+
self.assertEqual(settings_dict["OPTIONS"]["key_vault_namespace"], "encryption.__keyVault")

0 commit comments

Comments
 (0)