diff --git a/src/client/handlers/VaultsSecretsRemove.ts b/src/client/handlers/VaultsSecretsRemove.ts index 0395acf8e..47a996f78 100644 --- a/src/client/handlers/VaultsSecretsRemove.ts +++ b/src/client/handlers/VaultsSecretsRemove.ts @@ -51,9 +51,6 @@ class VaultsSecretsRemove extends ClientHandler< await vaultManager.withVaults( [vaultId], async (vault) => { - // console.log('in here', metadata); - // console.log('options', metadata?.options); - // console.log('recursive', metadata?.options?.recursive); await vaultOps.deleteSecret(vault, secretNames, { recursive: metadata?.options?.recursive, }); diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index d6779fdbc..ec03c4a7c 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -5,7 +5,6 @@ import type NodeManager from '@/nodes/NodeManager'; import type { LogEntryMessage, SecretContentMessage, - ContentMessage, VaultListMessage, VaultPermissionMessage, } from '@/client/types'; @@ -1464,15 +1463,14 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(createResponse.success).toBeTruthy(); // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = getStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: secret, }); await writer.close(); - resolve(0); - }); + })(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); @@ -1481,34 +1479,31 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(concatenatedContent).toStrictEqual(secret); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: secret, }); await writer.close(); - resolve(0); - }); + })(); expect((await deleteStream.output).success).toBeTruthy(); // Check secret was deleted const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteGetStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: secret, }); await writer.close(); - resolve(0); - }); - await expect(async () => { - try { + })(); + await testsUtils.expectRemoteError( + (async () => { for await (const _ of deleteGetStream.readable); - } catch (e) { - throw e.cause; - } - }).rejects.toThrow(vaultsErrors.ErrorSecretsSecretUndefined); + })(), + vaultsErrors.ErrorSecretsSecretUndefined, + ); }); test('concatenates multiple secrets together', async () => { // Create secret @@ -1525,7 +1520,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { } // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = getStream.writable.getWriter(); for (const name of secretNames) { await writer.write({ @@ -1534,8 +1529,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); @@ -1556,7 +1550,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { } // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = getStream.writable.getWriter(); for (const name of secretNames) { await writer.write({ @@ -1565,8 +1559,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); @@ -1574,7 +1567,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(secretContent.join('')).toStrictEqual(secretNames.join('')); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteStream.writable.getWriter(); for (const secretName of secretNames) { await writer.write({ @@ -1583,28 +1576,25 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret was deleted for (const secretName of secretNames) { - const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { - const writer = getStream.writable.getWriter(); + const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); + await (async () => { + const writer = deleteGetStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName, }); await writer.close(); - resolve(0); - }); - await expect(async () => { - try { - for await (const _ of getStream.readable); // Consume - } catch (e) { - throw e.cause; - } - }).rejects.toThrow(vaultsErrors.ErrorSecretsSecretUndefined); + })(); + await testsUtils.expectRemoteError( + (async () => { + for await (const _ of deleteGetStream.readable); + })(), + vaultsErrors.ErrorSecretsSecretUndefined, + ); } }); test('gets secrets from multiple vaults', async () => { @@ -1629,7 +1619,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { } // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = getStream.writable.getWriter(); for (const [vault, name] of secretVaultNames) { await writer.write({ @@ -1638,8 +1628,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); @@ -1647,7 +1636,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(secretContent.join('')).toStrictEqual(secretNames.join('')); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteStream.writable.getWriter(); for (const [vault, name] of secretVaultNames) { await writer.write({ @@ -1656,28 +1645,25 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret was deleted for (const [vault, name] of secretVaultNames) { - const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { - const writer = getStream.writable.getWriter(); + const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); + await (async () => { + const writer = deleteGetStream.writable.getWriter(); await writer.write({ nameOrId: vault, secretName: name, }); await writer.close(); - resolve(0); - }); - await expect(async () => { - try { - for await (const _ of getStream.readable); // Consume - } catch (e) { - throw e.cause; - } - }).rejects.toThrow(vaultsErrors.ErrorSecretsSecretUndefined); + })(); + await testsUtils.expectRemoteError( + (async () => { + for await (const _ of deleteGetStream.readable); + })(), + vaultsErrors.ErrorSecretsSecretUndefined, + ); } }); test('deletes secrets from multiple vaults in one log', async () => { @@ -1702,7 +1688,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { } // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { + await (async () => { const writer = getStream.writable.getWriter(); for (const [vault, name] of secretVaultNames) { await writer.write({ @@ -1711,15 +1697,14 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); } expect(secretContent.join('')).toStrictEqual(secretNames.join('')); // Get log size - let logLength: [number, number] = [0, 0]; + const logLength: [number, number] = [0, 0]; await vaultManager.withVaults(vaultIds, async (...vaults) => { for (let i = 0; i < vaults.length; i++) { logLength[i] = (await vaults[i].log()).length; @@ -1727,7 +1712,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteStream.writable.getWriter(); for (const [vault, name] of secretVaultNames) { await writer.write({ @@ -1736,28 +1721,25 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { }); } await writer.close(); - resolve(0); - }); + })(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret was deleted for (const [vault, name] of secretVaultNames) { - const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { - const writer = getStream.writable.getWriter(); + const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); + await (async () => { + const writer = deleteGetStream.writable.getWriter(); await writer.write({ nameOrId: vault, secretName: name, }); await writer.close(); - resolve(0); - }); - await expect(async () => { - try { - for await (const _ of getStream.readable); // Consume - } catch (e) { - throw e.cause; - } - }).rejects.toThrow(vaultsErrors.ErrorSecretsSecretUndefined); + })(); + await testsUtils.expectRemoteError( + (async () => { + for await (const _ of deleteGetStream.readable); + })(), + vaultsErrors.ErrorSecretsSecretUndefined, + ); } // Ensure single log message for deleting the secrets await vaultManager.withVaults(vaultIds, async (...vaults) => { @@ -1774,9 +1756,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretDir = path.join(dataDir, secretDirName); const secretNames = secretList.map((v) => path.join(secretDirName, v)); await fs.promises.mkdir(secretDir); + // Write secrets to files for (const secret of secretList) { const secretFile = path.join(secretDir, secret); - // Write secret to file await fs.promises.writeFile(secretFile, secret); } const vaultId = await vaultManager.createVault(vaultName); @@ -1788,15 +1770,14 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(addResponse.success).toBeTruthy(); // Delete secret const failDeleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = failDeleteStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: secretDirName, }); await writer.close(); - resolve(0); - }); + })(); await expect(async () => { try { (await failDeleteStream.output).success; @@ -1805,7 +1786,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { } }).rejects.toThrow(vaultsErrors.ErrorVaultsRecursive); const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await new Promise(async (resolve) => { + await (async () => { const writer = deleteStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, @@ -1813,28 +1794,25 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { metadata: { options: { recursive: true } }, }); await writer.close(); - resolve(0); - }); + })(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret and the secret directory were deleted for (const name of secretNames) { - const getStream = await rpcClient.methods.vaultsSecretsGet(); - await new Promise(async (resolve) => { - const writer = getStream.writable.getWriter(); + const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); + await (async () => { + const writer = deleteGetStream.writable.getWriter(); await writer.write({ nameOrId: vaultIdEncoded, secretName: name, }); await writer.close(); - resolve(0); - }); - await expect(async () => { - try { - for await (const _ of getStream.readable); // Consume - } catch (e) { - throw e.cause; - } - }).rejects.toThrow(vaultsErrors.ErrorSecretsSecretUndefined); + })(); + await testsUtils.expectRemoteError( + (async () => { + for await (const _ of deleteGetStream.readable); + })(), + vaultsErrors.ErrorSecretsSecretUndefined, + ); } await testsUtils.expectRemoteError( rpcClient.methods.vaultsSecretsStat({