Skip to content

Commit bed1a56

Browse files
authored
MOTOR-1025 Fix test_asyncio_encryption (#179)
1 parent 555863d commit bed1a56

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

test/asyncio_tests/test_asyncio_encryption.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,11 @@
3131

3232
OPTS = CodecOptions(uuid_representation=STANDARD)
3333

34-
try:
35-
import pymongocrypt # noqa: F401
3634

37-
_HAVE_PYMONGOCRYPT = True
38-
except ImportError:
39-
_HAVE_PYMONGOCRYPT = False
40-
41-
42-
@env.require_version_min(4, 2, -1)
4335
class TestExplicitSimple(AsyncIOTestCase):
36+
@env.require_csfle
4437
def setUp(self):
4538
super().setUp()
46-
if not _HAVE_PYMONGOCRYPT:
47-
self.fail("PyMongoCrypt is a required dependency")
4839

4940
def assertEncrypted(self, val):
5041
self.assertIsInstance(val, Binary)
@@ -142,7 +133,6 @@ async def test_codec_options(self):
142133
client_encryption_legacy = AsyncIOMotorClientEncryption(
143134
KMS_PROVIDERS, "keyvault.datakeys", client, opts
144135
)
145-
# self.addCleanup(client_encryption_legacy.close)
146136

147137
# Create the encrypted field's data key.
148138
key_id = await client_encryption_legacy.create_data_key("local")
@@ -168,9 +158,13 @@ async def test_codec_options(self):
168158
# Test that codec_options is applied during encryption.
169159
self.assertNotEqual(encrypted_standard, encrypted_legacy)
170160
# Test that codec_options is applied during decryption.
171-
self.assertEqual(await client_encryption_legacy.decrypt(encrypted_standard), value)
161+
self.assertEqual(
162+
await client_encryption_legacy.decrypt(encrypted_standard),
163+
Binary.from_uuid(value, uuid_representation=STANDARD),
164+
)
172165
self.assertNotEqual(await client_encryption.decrypt(encrypted_legacy), value)
173166

167+
await client_encryption_legacy.close()
174168
await client_encryption.close()
175169

176170
@asyncio_test

test/test_environment.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
HAVE_AIOHTTP = False
5353
aiohttp = None
5454

55+
HAVE_PYMONGOCRYPT = True
56+
try:
57+
import pymongocrypt # noqa: F401
58+
except ImportError:
59+
HAVE_PYMONGOCRYPT = False
60+
5561

5662
# Copied from PyMongo.
5763
def partition_node(node):
@@ -294,6 +300,7 @@ def require(self, condition, msg, func=None):
294300
def make_wrapper(f):
295301
@wraps(f)
296302
def wrap(*args, **kwargs):
303+
assert self.initialized
297304
if condition():
298305
return f(*args, **kwargs)
299306
raise SkipTest(msg)
@@ -341,6 +348,14 @@ def require_transactions(self, func):
341348
"""
342349
return self.require(self.supports_transactions, "Transactions are not supported", func=func)
343350

351+
def require_csfle(self, func):
352+
"""Run a test only if the deployment supports CSFLE."""
353+
return self.require(
354+
lambda: HAVE_PYMONGOCRYPT and self.version >= Version(4, 2),
355+
"CSFLE requires pymongocrypt and MongoDB >=4.2",
356+
func=func,
357+
)
358+
344359
def create_user(self, dbname, user, pwd=None, roles=None, **kwargs):
345360
kwargs["writeConcern"] = {"w": self.w}
346361
return create_user(self.sync_cx[dbname], user, pwd, roles, **kwargs)

0 commit comments

Comments
 (0)