@@ -2099,5 +2099,97 @@ def test_05_roundtrip_encrypted_unindexed(self):
2099
2099
self .assertEqual (decrypted , val )
2100
2100
2101
2101
2102
+ class TestQueryableEncryptionDocsExample (EncryptionIntegrationTest ):
2103
+ # Queryable Encryption is not supported on Standalone topology.
2104
+ @client_context .require_no_standalone
2105
+ @client_context .require_version_min (6 , 0 , - 1 )
2106
+ def setUp (self ):
2107
+ super ().setUp ()
2108
+
2109
+ def test_queryable_encryption (self ):
2110
+ # MongoClient to use in testing that handles auth/tls/etc,
2111
+ # and cleanup.
2112
+ def MongoClient (** kwargs ):
2113
+ c = rs_or_single_client (** kwargs )
2114
+ self .addCleanup (c .close )
2115
+ return c
2116
+
2117
+ # Drop data from prior test runs.
2118
+ self .client .keyvault .datakeys .drop ()
2119
+ self .client .drop_database ("docs_examples" )
2120
+
2121
+ kms_providers_map = {"local" : {"key" : LOCAL_MASTER_KEY }}
2122
+
2123
+ # Create two data keys.
2124
+ key_vault_client = MongoClient ()
2125
+ client_encryption = ClientEncryption (
2126
+ kms_providers_map , "keyvault.datakeys" , key_vault_client , CodecOptions ()
2127
+ )
2128
+ key1_id = client_encryption .create_data_key ("local" )
2129
+ key2_id = client_encryption .create_data_key ("local" )
2130
+
2131
+ # Create an encryptedFieldsMap.
2132
+ encrypted_fields_map = {
2133
+ "docs_examples.encrypted" : {
2134
+ "fields" : [
2135
+ {
2136
+ "path" : "encrypted_indexed" ,
2137
+ "bsonType" : "string" ,
2138
+ "keyId" : key1_id ,
2139
+ "queries" : [
2140
+ {
2141
+ "queryType" : "equality" ,
2142
+ },
2143
+ ],
2144
+ },
2145
+ {
2146
+ "path" : "encrypted_unindexed" ,
2147
+ "bsonType" : "string" ,
2148
+ "keyId" : key2_id ,
2149
+ },
2150
+ ],
2151
+ },
2152
+ }
2153
+
2154
+ # Create an Queryable Encryption collection.
2155
+ opts = AutoEncryptionOpts (
2156
+ kms_providers_map , "keyvault.datakeys" , encrypted_fields_map = encrypted_fields_map
2157
+ )
2158
+ encrypted_client = MongoClient (auto_encryption_opts = opts )
2159
+
2160
+ # Create a Queryable Encryption collection "docs_examples.encrypted".
2161
+ # Because docs_examples.encrypted is in encrypted_fields_map, it is
2162
+ # created with Queryable Encryption support.
2163
+ db = encrypted_client .docs_examples
2164
+ encrypted_coll = db .create_collection ("encrypted" )
2165
+
2166
+ # Auto encrypt an insert and find.
2167
+
2168
+ # Encrypt an insert.
2169
+ encrypted_coll .insert_one (
2170
+ {
2171
+ "_id" : 1 ,
2172
+ "encrypted_indexed" : "indexed_value" ,
2173
+ "encrypted_unindexed" : "unindexed_value" ,
2174
+ }
2175
+ )
2176
+
2177
+ # Encrypt a find.
2178
+ res = encrypted_coll .find_one ({"encrypted_indexed" : "indexed_value" })
2179
+ assert res is not None
2180
+ assert res ["encrypted_indexed" ] == "indexed_value"
2181
+ assert res ["encrypted_unindexed" ] == "unindexed_value"
2182
+
2183
+ # Find documents without decryption.
2184
+ unencrypted_client = MongoClient ()
2185
+ unencrypted_coll = unencrypted_client .docs_examples .encrypted
2186
+ res = unencrypted_coll .find_one ({"_id" : 1 })
2187
+ assert res is not None
2188
+ assert isinstance (res ["encrypted_indexed" ], Binary )
2189
+ assert isinstance (res ["encrypted_unindexed" ], Binary )
2190
+
2191
+ client_encryption .close ()
2192
+
2193
+
2102
2194
if __name__ == "__main__" :
2103
2195
unittest .main ()
0 commit comments