Skip to content

Commit

Permalink
test: fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken committed Apr 25, 2024
1 parent 3ee316d commit c0b0a86
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 87 deletions.
1 change: 0 additions & 1 deletion src/operations/find.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Document } from '../bson';
import { CursorResponse } from '../cmap/wire_protocol/responses';
import type { Collection } from '../collection';
import { MongoInvalidArgumentError } from '../error';
import { ReadConcern } from '../read_concern';
import type { Server } from '../sdam/server';
Expand Down
173 changes: 88 additions & 85 deletions test/integration/client-side-encryption/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,107 +298,110 @@ describe('Client Side Encryption Functional', function () {
});
});

describe('when @@mdb.decorateDecryptionResult is set on autoEncrypter', () => {
let client: MongoClient;
let encryptedClient: MongoClient;

beforeEach(async function () {
client = this.configuration.newClient();

const encryptSchema = (keyId: unknown, bsonType: string) => ({
encrypt: {
bsonType,
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random',
keyId: [keyId]
}
});
describe(
'when @@mdb.decorateDecryptionResult is set on autoEncrypter',
{ requires: { mongodb: '>=7.0' } },
() => {
let client: MongoClient;
let encryptedClient: MongoClient;

beforeEach(async function () {
client = this.configuration.newClient();

const encryptSchema = (keyId: unknown, bsonType: string) => ({
encrypt: {
bsonType,
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Random',
keyId: [keyId]
}
});

const kmsProviders = this.configuration.kmsProviders(crypto.randomBytes(96));
const kmsProviders = this.configuration.kmsProviders(crypto.randomBytes(96));

await client.connect();
await client.connect();

const encryption = new ClientEncryption(client, {
keyVaultNamespace,
kmsProviders,
extraOptions: getEncryptExtraOptions()
});

const dataDb = client.db(dataDbName);
const keyVaultDb = client.db(keyVaultDbName);

await dataDb.dropCollection(dataCollName).catch(() => null);
await keyVaultDb.dropCollection(keyVaultCollName).catch(() => null);
await keyVaultDb.createCollection(keyVaultCollName);
const dataKey = await encryption.createDataKey('local');
const encryption = new ClientEncryption(client, {
keyVaultNamespace,
kmsProviders,
extraOptions: getEncryptExtraOptions()
});

const $jsonSchema = {
bsonType: 'object',
properties: {
a: encryptSchema(dataKey, 'int'),
b: encryptSchema(dataKey, 'string'),
c: {
bsonType: 'object',
properties: {
d: {
encrypt: {
keyId: [dataKey],
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
bsonType: 'string'
const dataDb = client.db(dataDbName);
const keyVaultDb = client.db(keyVaultDbName);

await dataDb.dropCollection(dataCollName).catch(() => null);
await keyVaultDb.dropCollection(keyVaultCollName).catch(() => null);
await keyVaultDb.createCollection(keyVaultCollName);
const dataKey = await encryption.createDataKey('local');

const $jsonSchema = {
bsonType: 'object',
properties: {
a: encryptSchema(dataKey, 'int'),
b: encryptSchema(dataKey, 'string'),
c: {
bsonType: 'object',
properties: {
d: {
encrypt: {
keyId: [dataKey],
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
bsonType: 'string'
}
}
}
}
}
}
};
};

await dataDb.createCollection(dataCollName, {
validator: { $jsonSchema }
});
await dataDb.createCollection(dataCollName, {
validator: { $jsonSchema }
});

encryptedClient = this.configuration.newClient(
{},
{
autoEncryption: {
keyVaultNamespace,
kmsProviders,
extraOptions: getEncryptExtraOptions()
encryptedClient = this.configuration.newClient(
{},
{
autoEncryption: {
keyVaultNamespace,
kmsProviders,
extraOptions: getEncryptExtraOptions()
}
}
}
);
);

encryptedClient.autoEncrypter[Symbol.for('@@mdb.decorateDecryptionResult')] = true;
await encryptedClient.connect();
});
encryptedClient.autoEncrypter[Symbol.for('@@mdb.decorateDecryptionResult')] = true;
await encryptedClient.connect();
});

afterEach(function () {
return Promise.resolve()
.then(() => encryptedClient?.close())
.then(() => client?.close());
});
afterEach(async function () {
await encryptedClient?.close();
await client?.close();
});

it('adds decrypted keys to result at @@mdb.decryptedKeys', async function () {
const coll = encryptedClient.db(dataDbName).collection(dataCollName);
it('adds decrypted keys to result at @@mdb.decryptedKeys', async function () {
const coll = encryptedClient.db(dataDbName).collection(dataCollName);

const data = {
_id: new BSON.ObjectId(),
a: 1,
b: 'abc',
c: { d: 'def' }
};
const data = {
_id: new BSON.ObjectId(),
a: 1,
b: 'abc',
c: { d: 'def' }
};

const result = await coll.insertOne(data);
const decrypted = await coll.findOne({ _id: result.insertedId });
const result = await coll.insertOne(data);
const decrypted = await coll.findOne({ _id: result.insertedId });

expect(decrypted).to.deep.equal(data);
expect(decrypted)
.to.have.property(Symbol.for('@@mdb.decryptedKeys'))
.that.deep.equals(['a', 'b']);
expect(decrypted).to.deep.equal(data);
expect(decrypted)
.to.have.property(Symbol.for('@@mdb.decryptedKeys'))
.that.deep.equals(['a', 'b']);

// Nested
expect(decrypted).to.have.property('c');
expect(decrypted.c)
.to.have.property(Symbol.for('@@mdb.decryptedKeys'))
.that.deep.equals(['d']);
});
});
// Nested
expect(decrypted).to.have.property('c');
expect(decrypted.c)
.to.have.property(Symbol.for('@@mdb.decryptedKeys'))
.that.deep.equals(['d']);
});
}
);
});
2 changes: 1 addition & 1 deletion test/integration/crud/abstract_operation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('abstract operation', function () {
correctCommandName: 'count'
},
{
subclassCreator: () => new mongodb.FindOperation(undefined, collection.fullNamespace),
subclassCreator: () => new mongodb.FindOperation(collection.fullNamespace),
subclassType: mongodb.FindOperation,
correctCommandName: 'find'
},
Expand Down

0 comments on commit c0b0a86

Please sign in to comment.