Skip to content

Commit a58edbc

Browse files
committed
INTPYTHON-527 Add test
1 parent cdb9d1a commit a58edbc

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

django_mongodb_backend/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Check Django compatibility before other imports which may fail if the
44
# wrong version of Django is installed.
5-
from .utils import check_django_compatability, parse_uri
5+
from .utils import check_django_compatability, get_auto_encryption_options, parse_uri
66

77
check_django_compatability()
88

@@ -15,7 +15,7 @@
1515
from .lookups import register_lookups # noqa: E402
1616
from .query import register_nodes # noqa: E402
1717

18-
__all__ = ["parse_uri"]
18+
__all__ = ["get_auto_encryption_options", "parse_uri"]
1919

2020
register_aggregates()
2121
register_checks()

django_mongodb_backend/features.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,3 +577,13 @@ def supports_atlas_search(self):
577577
return False
578578
else:
579579
return True
580+
581+
@cached_property
582+
def supports_queryable_encryption(self):
583+
"""
584+
Queryable Encryption is supported if the server is Atlas or Enterprise.
585+
"""
586+
self.connection.ensure_connection()
587+
client = self.connection.connection.admin
588+
build_info = client.command("buildInfo")
589+
return "enterprise" in build_info.get("modules")

django_mongodb_backend/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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
1112
from pymongo.uri_parser import parse_uri as pymongo_parse_uri
1213

1314

@@ -28,6 +29,16 @@ def check_django_compatability():
2829
)
2930

3031

32+
def get_auto_encryption_options(crypt_shared_lib_path=None):
33+
key_vault_database_name = "encryption"
34+
key_vault_collection_name = "__keyVault"
35+
key_vault_namespace = f"{key_vault_database_name}.{key_vault_collection_name}"
36+
kms_providers = {}
37+
return AutoEncryptionOpts(
38+
kms_providers, key_vault_namespace, crypt_shared_lib_path=crypt_shared_lib_path
39+
)
40+
41+
3142
def parse_uri(uri, *, db_name=None, test=None):
3243
"""
3344
Convert the given uri into a dictionary suitable for Django's DATABASES

tests/backend_/utils/test_parse_uri.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from django.core.exceptions import ImproperlyConfigured
55
from django.test import SimpleTestCase
66

7-
from django_mongodb_backend import parse_uri
7+
from django_mongodb_backend import get_auto_encryption_options, parse_uri
88

99

1010
class ParseURITests(SimpleTestCase):
@@ -94,3 +94,6 @@ def test_invalid_credentials(self):
9494
def test_no_scheme(self):
9595
with self.assertRaisesMessage(pymongo.errors.InvalidURI, "Invalid URI scheme"):
9696
parse_uri("cluster0.example.mongodb.net")
97+
98+
def test_queryable_encryption_config(self):
99+
get_auto_encryption_options()

0 commit comments

Comments
 (0)