Skip to content

Commit 698599c

Browse files
authored
PYTHON-2267 Test passing UUID to encrypt_expression (#1510)
1 parent 97b9a33 commit 698599c

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

doc/changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ PyMongo 4.7 brings a number of improvements including:
1818
Named KMS providers enables more than one of each KMS provider type to be configured.
1919
See the docstring for :class:`~pymongo.encryption_options.AutoEncryptionOpts`.
2020
Note that named KMS providers requires pymongocrypt >=1.9 and libmongocrypt >=1.9.
21+
- :meth:`~pymongo.encryption.ClientEncryption.encrypt` and
22+
:meth:`~pymongo.encryption.ClientEncryption.encrypt_expression` now allow ``key_id``
23+
to be passed in as a :class:`uuid.UUID`.
2124
- Fixed a bug where :class:`~bson.int64.Int64` instances could not always be encoded by `orjson`_. The following now
2225
works::
2326

pymongo/encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def encrypt(
831831
:return: The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
832832
833833
.. versionchanged:: 4.7
834-
``key_id`` can now be passed in as a :class:`uuid.UUID`.
834+
``key_id`` can now be passed in as a :class:`uuid.UUID`.
835835
836836
.. versionchanged:: 4.2
837837
Added the `query_type` and `contention_factor` parameters.
@@ -883,7 +883,7 @@ def encrypt_expression(
883883
:return: The encrypted expression, a :class:`~bson.RawBSONDocument`.
884884
885885
.. versionchanged:: 4.7
886-
``key_id`` can now be passed in as a :class:`uuid.UUID`.
886+
``key_id`` can now be passed in as a :class:`uuid.UUID`.
887887
888888
.. versionadded:: 4.4
889889
"""

test/test_encryption.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,10 +2623,12 @@ def setUp(self):
26232623
self.db = self.encrypted_client.db
26242624
self.addCleanup(self.encrypted_client.close)
26252625

2626-
def run_expression_find(self, name, expression, expected_elems, range_opts, use_expr=False):
2626+
def run_expression_find(
2627+
self, name, expression, expected_elems, range_opts, use_expr=False, key_id=None
2628+
):
26272629
find_payload = self.client_encryption.encrypt_expression(
26282630
expression=expression,
2629-
key_id=self.key1_id,
2631+
key_id=key_id or self.key1_id,
26302632
algorithm=Algorithm.RANGEPREVIEW,
26312633
query_type=QueryType.RANGEPREVIEW,
26322634
contention_factor=0,
@@ -2668,16 +2670,20 @@ def encrypt_and_cast(i):
26682670
self.assertEqual(self.client_encryption.decrypt(insert_payload), cast_func(6))
26692671

26702672
# Case 2.
2673+
expression = {
2674+
"$and": [
2675+
{f"encrypted{name}": {"$gte": cast_func(6)}},
2676+
{f"encrypted{name}": {"$lte": cast_func(200)}},
2677+
]
2678+
}
2679+
self.run_expression_find(name, expression, [cast_func(i) for i in [6, 30, 200]], range_opts)
2680+
# Case 2, with UUID key_id
26712681
self.run_expression_find(
26722682
name,
2673-
{
2674-
"$and": [
2675-
{f"encrypted{name}": {"$gte": cast_func(6)}},
2676-
{f"encrypted{name}": {"$lte": cast_func(200)}},
2677-
]
2678-
},
2683+
expression,
26792684
[cast_func(i) for i in [6, 30, 200]],
26802685
range_opts,
2686+
key_id=self.key1_id.as_uuid(),
26812687
)
26822688

26832689
# Case 3.

0 commit comments

Comments
 (0)