Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions test/asyncio_tests/test_asyncio_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,11 @@

OPTS = CodecOptions(uuid_representation=STANDARD)

try:
import pymongocrypt # noqa: F401

_HAVE_PYMONGOCRYPT = True
except ImportError:
_HAVE_PYMONGOCRYPT = False


@env.require_version_min(4, 2, -1)
class TestExplicitSimple(AsyncIOTestCase):
@env.require_csfle
def setUp(self):
super().setUp()
if not _HAVE_PYMONGOCRYPT:
self.fail("PyMongoCrypt is a required dependency")

def assertEncrypted(self, val):
self.assertIsInstance(val, Binary)
Expand Down Expand Up @@ -142,7 +133,6 @@ async def test_codec_options(self):
client_encryption_legacy = AsyncIOMotorClientEncryption(
KMS_PROVIDERS, "keyvault.datakeys", client, opts
)
# self.addCleanup(client_encryption_legacy.close)

# Create the encrypted field's data key.
key_id = await client_encryption_legacy.create_data_key("local")
Expand All @@ -168,9 +158,13 @@ async def test_codec_options(self):
# Test that codec_options is applied during encryption.
self.assertNotEqual(encrypted_standard, encrypted_legacy)
# Test that codec_options is applied during decryption.
self.assertEqual(await client_encryption_legacy.decrypt(encrypted_standard), value)
self.assertEqual(
await client_encryption_legacy.decrypt(encrypted_standard),
Binary.from_uuid(value, uuid_representation=STANDARD),
)
self.assertNotEqual(await client_encryption.decrypt(encrypted_legacy), value)

await client_encryption_legacy.close()
await client_encryption.close()

@asyncio_test
Expand Down
15 changes: 15 additions & 0 deletions test/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@
HAVE_AIOHTTP = False
aiohttp = None

HAVE_PYMONGOCRYPT = True
try:
import pymongocrypt # noqa: F401
except ImportError:
HAVE_PYMONGOCRYPT = False


# Copied from PyMongo.
def partition_node(node):
Expand Down Expand Up @@ -294,6 +300,7 @@ def require(self, condition, msg, func=None):
def make_wrapper(f):
@wraps(f)
def wrap(*args, **kwargs):
assert self.initialized
if condition():
return f(*args, **kwargs)
raise SkipTest(msg)
Expand Down Expand Up @@ -341,6 +348,14 @@ def require_transactions(self, func):
"""
return self.require(self.supports_transactions, "Transactions are not supported", func=func)

def require_csfle(self, func):
"""Run a test only if the deployment supports CSFLE."""
return self.require(
lambda: HAVE_PYMONGOCRYPT and self.version >= Version(4, 2),
"CSFLE requires pymongocrypt and MongoDB >=4.2",
func=func,
)

def create_user(self, dbname, user, pwd=None, roles=None, **kwargs):
kwargs["writeConcern"] = {"w": self.w}
return create_user(self.sync_cx[dbname], user, pwd, roles, **kwargs)
Expand Down