Skip to content

Commit 74fe82a

Browse files
test(NODE-4331): add prose test 13 (#3335)
1 parent d2b6ad8 commit 74fe82a

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

test/integration/client-side-encryption/client_side_encryption.prose.test.js

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { dropCollection, APMEventCollector } = require('../shared');
99

1010
const { EJSON, Binary } = BSON;
1111
const { LEGACY_HELLO_COMMAND } = require('../../../src/constants');
12-
const { MongoNetworkError } = require('../../../src/error');
12+
const { MongoNetworkError, MongoServerError } = require('../../../src/error');
1313

1414
const getKmsProviders = (localKey, kmipEndpoint, azureEndpoint, gcpEndpoint) => {
1515
const result = BSON.EJSON.parse(process.env.CSFLE_KMS_PROVIDERS || '{}');
@@ -1723,6 +1723,110 @@ describe('Client Side Encryption Prose Tests', metadata, function () {
17231723
});
17241724
});
17251725

1726+
context('13. Unique Index on keyAltNames', function () {
1727+
let client, clientEncryption, setupKeyId;
1728+
1729+
beforeEach(async function () {
1730+
// Create a MongoClient object (referred to as client).
1731+
client = this.configuration.newClient();
1732+
await client.connect();
1733+
1734+
// Using client, drop the collection keyvault.datakeys.
1735+
await client
1736+
.db('keyvault')
1737+
.dropCollection('datakeys')
1738+
.catch(() => null);
1739+
1740+
await client
1741+
.db('keyvault')
1742+
.collection('datakeys')
1743+
.createIndex(
1744+
{ keyAltNames: 1 },
1745+
{
1746+
unique: true,
1747+
partialFilterExpression: { keyAltNames: { $exists: true } },
1748+
writeConcern: { w: 'majority' }
1749+
}
1750+
);
1751+
1752+
// Create a ClientEncryption object (referred to as client_encryption) with client set as the keyVaultClient.
1753+
clientEncryption = new this.configuration.mongodbClientEncryption.ClientEncryption(client, {
1754+
keyVaultNamespace: 'keyvault.datakeys',
1755+
kmsProviders: getKmsProviders()
1756+
});
1757+
1758+
// Using client_encryption, create a data key with a local KMS provider and the keyAltName "def".
1759+
setupKeyId = await clientEncryption.createDataKey('local', {
1760+
keyAltNames: ['def']
1761+
});
1762+
});
1763+
1764+
afterEach(async () => {
1765+
clientEncryption = null;
1766+
setupKeyId = null;
1767+
await client.close();
1768+
});
1769+
1770+
context('Case 1', metadata, function () {
1771+
it('createDataKey() handles duplicate key errors on the keyvault collection', async function () {
1772+
// 1. Use client_encryption to create a new local data key with a keyAltName "abc" and assert the operation does not fail.
1773+
await clientEncryption.createDataKey('local', {
1774+
keyAltNames: ['abc']
1775+
});
1776+
1777+
// 2. Repeat Step 1 and assert the operation fails due to a duplicate key server error (error code 11000).
1778+
const resultStep2 = await clientEncryption
1779+
.createDataKey('local', {
1780+
keyAltNames: ['abc']
1781+
})
1782+
.catch(e => e);
1783+
expect(
1784+
resultStep2,
1785+
'Error in step 2) expected clientEncryption.createDataKey to throw duplicate key error but it did not'
1786+
).to.be.instanceof(MongoServerError);
1787+
expect(resultStep2).have.property('code', 11000);
1788+
1789+
// 3. Use client_encryption to create a new local data key with a keyAltName "def" and assert the operation fails due to a duplicate key server error (error code 11000).
1790+
const resultStep3 = await clientEncryption
1791+
.createDataKey('local', {
1792+
keyAltNames: ['def']
1793+
})
1794+
.catch(e => e);
1795+
expect(
1796+
resultStep3,
1797+
'Error in step 3) expected clientEncryption.createDataKey to throw duplicate key error but it did not'
1798+
).to.be.instanceof(MongoServerError);
1799+
expect(resultStep3).have.property('code', 11000);
1800+
});
1801+
});
1802+
1803+
context('Case 2', metadata, function () {
1804+
it('addKeyAltName() handles duplicate key errors on the keyvault collection', async function () {
1805+
// 1. Use client_encryption to create a new local data key and assert the operation does not fail.
1806+
const _id = await clientEncryption.createDataKey('local');
1807+
1808+
// 2. Use client_encryption to add a keyAltName "abc" to the key created in Step 1 and assert the operation does not fail.
1809+
await clientEncryption.addKeyAltName(_id, 'abc');
1810+
1811+
// 3. Repeat Step 2, assert the operation does not fail, and assert the returned key document contains the keyAltName "abc" added in Step 2.
1812+
const resultStep3 = await clientEncryption.addKeyAltName(_id, 'abc');
1813+
expect(resultStep3).to.have.property('keyAltNames').to.include('abc');
1814+
1815+
// 4. Use client_encryption to add a keyAltName "def" to the key created in Step 1 and assert the operation fails due to a duplicate key server error (error code 11000).
1816+
const resultStep4 = await clientEncryption.addKeyAltName(_id, 'def').catch(e => e);
1817+
expect(
1818+
resultStep4,
1819+
'Error in step 4) expected clientEncryption.addKeyAltName to throw duplicate key error but it did not'
1820+
).to.be.instanceof(MongoServerError);
1821+
expect(resultStep4).to.have.property('code', 11000);
1822+
1823+
// 5. Use client_encryption to add a keyAltName "def" to the existing key, assert the operation does not fail, and assert the returned key document contains the keyAltName "def" added during Setup.
1824+
const resultStep5 = await clientEncryption.addKeyAltName(setupKeyId, 'def');
1825+
expect(resultStep5).to.have.property('keyAltNames').to.include('def');
1826+
});
1827+
});
1828+
});
1829+
17261830
context('14. Decryption Events', metadata, function () {
17271831
let setupClient;
17281832
let clientEncryption;

0 commit comments

Comments
 (0)