Skip to content

Commit 8f6249e

Browse files
authored
PYTHON-5091 - Convert test.test_on_demand_csfle to async (#2108)
1 parent 19fdf7c commit 8f6249e

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright 2022-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test client side encryption with on demand credentials."""
16+
from __future__ import annotations
17+
18+
import os
19+
import sys
20+
import unittest
21+
22+
import pytest
23+
24+
sys.path[0:0] = [""]
25+
26+
from test.asynchronous import AsyncIntegrationTest, async_client_context
27+
28+
from bson.codec_options import CodecOptions
29+
from pymongo.asynchronous.encryption import (
30+
_HAVE_PYMONGOCRYPT,
31+
AsyncClientEncryption,
32+
EncryptionError,
33+
)
34+
35+
_IS_SYNC = False
36+
37+
pytestmark = pytest.mark.csfle
38+
39+
40+
class TestonDemandGCPCredentials(AsyncIntegrationTest):
41+
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
42+
@async_client_context.require_version_min(4, 2, -1)
43+
async def asyncSetUp(self):
44+
await super().asyncSetUp()
45+
self.master_key = {
46+
"projectId": "devprod-drivers",
47+
"location": "global",
48+
"keyRing": "key-ring-csfle",
49+
"keyName": "key-name-csfle",
50+
}
51+
52+
@unittest.skipIf(not os.getenv("TEST_FLE_GCP_AUTO"), "Not testing FLE GCP auto")
53+
async def test_01_failure(self):
54+
if os.environ["SUCCESS"].lower() == "true":
55+
self.skipTest("Expecting success")
56+
self.client_encryption = AsyncClientEncryption(
57+
kms_providers={"gcp": {}},
58+
key_vault_namespace="keyvault.datakeys",
59+
key_vault_client=async_client_context.client,
60+
codec_options=CodecOptions(),
61+
)
62+
with self.assertRaises(EncryptionError):
63+
await self.client_encryption.create_data_key("gcp", self.master_key)
64+
65+
@unittest.skipIf(not os.getenv("TEST_FLE_GCP_AUTO"), "Not testing FLE GCP auto")
66+
async def test_02_success(self):
67+
if os.environ["SUCCESS"].lower() == "false":
68+
self.skipTest("Expecting failure")
69+
self.client_encryption = AsyncClientEncryption(
70+
kms_providers={"gcp": {}},
71+
key_vault_namespace="keyvault.datakeys",
72+
key_vault_client=async_client_context.client,
73+
codec_options=CodecOptions(),
74+
)
75+
await self.client_encryption.create_data_key("gcp", self.master_key)
76+
77+
78+
class TestonDemandAzureCredentials(AsyncIntegrationTest):
79+
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
80+
@async_client_context.require_version_min(4, 2, -1)
81+
async def asyncSetUp(self):
82+
await super().asyncSetUp()
83+
self.master_key = {
84+
"keyVaultEndpoint": os.environ["KEY_VAULT_ENDPOINT"],
85+
"keyName": os.environ["KEY_NAME"],
86+
}
87+
88+
@unittest.skipIf(not os.getenv("TEST_FLE_AZURE_AUTO"), "Not testing FLE Azure auto")
89+
async def test_01_failure(self):
90+
if os.environ["SUCCESS"].lower() == "true":
91+
self.skipTest("Expecting success")
92+
self.client_encryption = AsyncClientEncryption(
93+
kms_providers={"azure": {}},
94+
key_vault_namespace="keyvault.datakeys",
95+
key_vault_client=async_client_context.client,
96+
codec_options=CodecOptions(),
97+
)
98+
with self.assertRaises(EncryptionError):
99+
await self.client_encryption.create_data_key("azure", self.master_key)
100+
101+
@unittest.skipIf(not os.getenv("TEST_FLE_AZURE_AUTO"), "Not testing FLE Azure auto")
102+
async def test_02_success(self):
103+
if os.environ["SUCCESS"].lower() == "false":
104+
self.skipTest("Expecting failure")
105+
self.client_encryption = AsyncClientEncryption(
106+
kms_providers={"azure": {}},
107+
key_vault_namespace="keyvault.datakeys",
108+
key_vault_client=async_client_context.client,
109+
codec_options=CodecOptions(),
110+
)
111+
await self.client_encryption.create_data_key("azure", self.master_key)
112+
113+
114+
if __name__ == "__main__":
115+
unittest.main(verbosity=2)

test/test_on_demand_csfle.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626
from test import IntegrationTest, client_context
2727

2828
from bson.codec_options import CodecOptions
29-
from pymongo.synchronous.encryption import _HAVE_PYMONGOCRYPT, ClientEncryption, EncryptionError
29+
from pymongo.synchronous.encryption import (
30+
_HAVE_PYMONGOCRYPT,
31+
ClientEncryption,
32+
EncryptionError,
33+
)
34+
35+
_IS_SYNC = True
3036

3137
pytestmark = pytest.mark.csfle
3238

3339

3440
class TestonDemandGCPCredentials(IntegrationTest):
35-
@classmethod
3641
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
3742
@client_context.require_version_min(4, 2, -1)
38-
def setUpClass(cls):
39-
super().setUpClass()
40-
4143
def setUp(self):
4244
super().setUp()
4345
self.master_key = {
@@ -74,12 +76,8 @@ def test_02_success(self):
7476

7577

7678
class TestonDemandAzureCredentials(IntegrationTest):
77-
@classmethod
7879
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
7980
@client_context.require_version_min(4, 2, -1)
80-
def setUpClass(cls):
81-
super().setUpClass()
82-
8381
def setUp(self):
8482
super().setUp()
8583
self.master_key = {

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def async_only_test(f: str) -> bool:
215215
"test_gridfs_spec.py",
216216
"test_logger.py",
217217
"test_monitoring.py",
218+
"test_on_demand_csfle.py",
218219
"test_raw_bson.py",
219220
"test_read_concern.py",
220221
"test_retryable_reads.py",

0 commit comments

Comments
 (0)