(self, opts)
| 299 | |
| 300 | class TestClientSimple(EncryptionIntegrationTest): |
| 301 | def _test_auto_encrypt(self, opts): |
| 302 | client = self.rs_or_single_client(auto_encryption_opts=opts) |
| 303 | |
| 304 | # Create the encrypted field's data key. |
| 305 | key_vault = create_key_vault( |
| 306 | self.client.keyvault.datakeys, json_data("custom", "key-document-local.json") |
| 307 | ) |
| 308 | self.addCleanup(key_vault.drop) |
| 309 | |
| 310 | # Collection.insert_one/insert_many auto encrypts. |
| 311 | docs = [ |
| 312 | {"_id": 0, "ssn": "000"}, |
| 313 | {"_id": 1, "ssn": "111"}, |
| 314 | {"_id": 2, "ssn": "222"}, |
| 315 | {"_id": 3, "ssn": "333"}, |
| 316 | {"_id": 4, "ssn": "444"}, |
| 317 | {"_id": 5, "ssn": "555"}, |
| 318 | ] |
| 319 | encrypted_coll = client.pymongo_test.test |
| 320 | encrypted_coll.insert_one(docs[0]) |
| 321 | encrypted_coll.insert_many(docs[1:3]) |
| 322 | unack = encrypted_coll.with_options(write_concern=WriteConcern(w=0)) |
| 323 | unack.insert_one(docs[3]) |
| 324 | unack.insert_many(docs[4:], ordered=False) |
| 325 | |
| 326 | def count_documents(): |
| 327 | return self.db.test.count_documents({}) == len(docs) |
| 328 | |
| 329 | wait_until(count_documents, "insert documents with w=0") |
| 330 | |
| 331 | # Database.command auto decrypts. |
| 332 | res = client.pymongo_test.command("find", "test", filter={"ssn": "000"}) |
| 333 | decrypted_docs = res["cursor"]["firstBatch"] |
| 334 | self.assertEqual(decrypted_docs, [{"_id": 0, "ssn": "000"}]) |
| 335 | |
| 336 | # Collection.find auto decrypts. |
| 337 | decrypted_docs = encrypted_coll.find().to_list() |
| 338 | self.assertEqual(decrypted_docs, docs) |
| 339 | |
| 340 | # Collection.find auto decrypts getMores. |
| 341 | decrypted_docs = encrypted_coll.find(batch_size=1).to_list() |
| 342 | self.assertEqual(decrypted_docs, docs) |
| 343 | |
| 344 | # Collection.aggregate auto decrypts. |
| 345 | decrypted_docs = (encrypted_coll.aggregate([])).to_list() |
| 346 | self.assertEqual(decrypted_docs, docs) |
| 347 | |
| 348 | # Collection.aggregate auto decrypts getMores. |
| 349 | decrypted_docs = (encrypted_coll.aggregate([], batchSize=1)).to_list() |
| 350 | self.assertEqual(decrypted_docs, docs) |
| 351 | |
| 352 | # Collection.distinct auto decrypts. |
| 353 | decrypted_ssns = encrypted_coll.distinct("ssn") |
| 354 | self.assertEqual(set(decrypted_ssns), {d["ssn"] for d in docs}) |
| 355 | |
| 356 | # Make sure the field is actually encrypted. |
| 357 | for encrypted_doc in self.db.test.find(): |
| 358 | self.assertIsInstance(encrypted_doc["_id"], int) |
no test coverage detected