Skip to content

Commit b7016de

Browse files
add test for default mongoose connection
1 parent ecb3f7c commit b7016de

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

test/encryption/encryption.test.js

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ describe('encryption integration tests', () => {
6363
});
6464
});
6565

66+
const algorithm = 'AEAD_AES_256_CBC_HMAC_SHA_512-Random';
67+
68+
6669
let keyId, keyId2, keyId3;
6770
let utilClient;
6871

@@ -90,7 +93,6 @@ describe('encryption integration tests', () => {
9093
});
9194

9295
describe('Tests that fields of valid schema types can be declared as encrypted schemas', function() {
93-
const algorithm = 'AEAD_AES_256_CBC_HMAC_SHA_512-Random';
9496
let connection;
9597
let schema;
9698
let model;
@@ -1126,4 +1128,44 @@ describe('encryption integration tests', () => {
11261128
});
11271129
});
11281130
});
1131+
1132+
describe('Encryption can be configured on the default mongoose connection', function() {
1133+
afterEach(async function() {
1134+
mongoose.deleteModel('Schema');
1135+
await mongoose.disconnect();
1136+
1137+
});
1138+
it('encrypts and decrypts', async function() {
1139+
const schema = new Schema({
1140+
a: {
1141+
type: Schema.Types.Int32,
1142+
encrypt: { keyId: [keyId], algorithm }
1143+
1144+
}
1145+
}, {
1146+
encryptionType: 'csfle'
1147+
});
1148+
1149+
const model = mongoose.model('Schema', schema);
1150+
await mongoose.connect(process.env.MONGOOSE_TEST_URI, {
1151+
dbName: 'db', autoEncryption: {
1152+
keyVaultNamespace: 'keyvault.datakeys',
1153+
kmsProviders: { local: { key: LOCAL_KEY } },
1154+
extraOptions: {
1155+
cryptdSharedLibRequired: true,
1156+
cryptSharedLibPath: process.env.CRYPT_SHARED_LIB_PATH
1157+
}
1158+
}
1159+
});
1160+
1161+
const [{ _id }] = await model.insertMany([{ a: 2 }]);
1162+
const encryptedDoc = await utilClient.db('db').collection('schemas').findOne({ _id });
1163+
1164+
assert.ok(isBsonType(encryptedDoc.a, 'Binary'));
1165+
assert.ok(encryptedDoc.a.sub_type === 6);
1166+
1167+
const doc = await model.findOne({ _id });
1168+
assert.deepEqual(doc.a, 2);
1169+
});
1170+
});
11291171
});

0 commit comments

Comments
 (0)