From 9782f35f22e35a111fac1bd830766ef5b102103b Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Tue, 10 Sep 2024 15:13:15 +1000 Subject: [PATCH] feat: added tests --- src/client/handlers/VaultsSecretsRemove.ts | 2 +- src/client/types.ts | 11 ++- tests/client/handlers/vaults.test.ts | 105 +++++++++++++++++++-- 3 files changed, 106 insertions(+), 12 deletions(-) diff --git a/src/client/handlers/VaultsSecretsRemove.ts b/src/client/handlers/VaultsSecretsRemove.ts index a49eb4191..705f22ac3 100644 --- a/src/client/handlers/VaultsSecretsRemove.ts +++ b/src/client/handlers/VaultsSecretsRemove.ts @@ -36,7 +36,7 @@ class VaultsSecretsRemove extends UnaryHandler< async (vault) => { for (const secretName of input.secretNames) { await vaultOps.deleteSecret(vault, secretName, { - recursive: input.recursive, + recursive: input.options?.recursive, }); } }, diff --git a/src/client/types.ts b/src/client/types.ts index 9b09a80bb..2b3c913ec 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -306,13 +306,16 @@ type SecretPathMessage = { type SecretPathsMessage = { secretNames: Array; -} +}; type SecretIdentifierMessage = VaultIdentifierMessage & SecretPathMessage; -type SecretRemoveMessage = VaultIdentifierMessage & SecretPathsMessage & { - recursive: boolean; -}; +type SecretRemoveMessage = VaultIdentifierMessage & + SecretPathsMessage & { + options?: { + recursive?: boolean; + }; + }; // Contains binary content as a binary string 'toString('binary')' type ContentMessage = { diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index b847ee018..e2bb47e3a 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -31,7 +31,7 @@ import { VaultsPermissionSet, VaultsPermissionUnset, VaultsRename, - VaultsSecretsDelete, + VaultsSecretsRemove, VaultsSecretsEdit, VaultsSecretsEnv, VaultsSecretsGet, @@ -52,7 +52,7 @@ import { vaultsPermissionSet, vaultsPermissionUnset, vaultsRename, - vaultsSecretsDelete, + vaultsSecretsRemove, vaultsSecretsEdit, vaultsSecretsEnv, vaultsSecretsGet, @@ -1352,8 +1352,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { let webSocketClient: WebSocketClient; let rpcClient: RPCClient<{ vaultsSecretsNew: typeof vaultsSecretsNew; - vaultsSecretsDelete: typeof vaultsSecretsDelete; + vaultsSecretsRemove: typeof vaultsSecretsRemove; vaultsSecretsGet: typeof vaultsSecretsGet; + vaultsSecretsNewDir: typeof vaultsSecretsNewDir; }>; let vaultManager: VaultManager; beforeEach(async () => { @@ -1396,7 +1397,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { db, vaultManager, }), - vaultsSecretsDelete: new VaultsSecretsDelete({ + vaultsSecretsRemove: new VaultsSecretsRemove({ db, vaultManager, }), @@ -1404,6 +1405,11 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { db, vaultManager, }), + vaultsSecretsNewDir: new VaultsSecretsNewDir({ + db, + fs, + vaultManager, + }), }, host: localhost, }); @@ -1418,8 +1424,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { rpcClient = new RPCClient({ manifest: { vaultsSecretsNew, - vaultsSecretsDelete, + vaultsSecretsRemove, vaultsSecretsGet, + vaultsSecretsNewDir, }, streamFactory: () => webSocketClient.connection.newStream(), toError: networkUtils.toError, @@ -1456,9 +1463,9 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { const secretContent = getResponse1.secretContent; expect(secretContent).toStrictEqual(secret); // Delete secret - const deleteResponse = await rpcClient.methods.vaultsSecretsDelete({ + const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({ nameOrId: vaultIdEncoded, - secretName: secret, + secretNames: [secret], }); expect(deleteResponse.success).toBeTruthy(); // Check secret was deleted @@ -1470,6 +1477,90 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { vaultsErrors.ErrorSecretsSecretUndefined, ); }); + test('deletes multiple secrets', async () => { + // Create secret + const secret1 = 'test-secret1'; + const secret2 = 'test-secret2'; + const vaultId = await vaultManager.createVault('test-vault'); + const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId); + await rpcClient.methods.vaultsSecretsNew({ + nameOrId: vaultIdEncoded, + secretName: secret1, + secretContent: Buffer.from(secret1).toString('binary'), + }); + await rpcClient.methods.vaultsSecretsNew({ + nameOrId: vaultIdEncoded, + secretName: secret2, + secretContent: Buffer.from(secret2).toString('binary'), + }); + // Get secret + const getResponse1 = await rpcClient.methods.vaultsSecretsGet({ + nameOrId: vaultIdEncoded, + secretName: secret1, + }); + const getResponse2 = await rpcClient.methods.vaultsSecretsGet({ + nameOrId: vaultIdEncoded, + secretName: secret2, + }); + expect(getResponse1.secretContent).toStrictEqual(secret1); + expect(getResponse2.secretContent).toStrictEqual(secret2); + // Delete secret + const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({ + nameOrId: vaultIdEncoded, + secretNames: [secret1, secret2], + }); + expect(deleteResponse.success).toBeTruthy(); + // Check secret was deleted + await testsUtils.expectRemoteError( + rpcClient.methods.vaultsSecretsGet({ + nameOrId: vaultIdEncoded, + secretName: secret1, + }), + vaultsErrors.ErrorSecretsSecretUndefined, + ); + await testsUtils.expectRemoteError( + rpcClient.methods.vaultsSecretsGet({ + nameOrId: vaultIdEncoded, + secretName: secret2, + }), + vaultsErrors.ErrorSecretsSecretUndefined, + ); + }); + test('deletes directory recursively', async () => { + // Create secret + const vaultName = 'test-vault'; + const secretList = ['test-secret1', 'test-secret2', 'test-secret3']; + const secretDir = path.join(dataDir, 'secretDir'); + await fs.promises.mkdir(secretDir); + 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); + const vaultsIdEncoded = vaultsUtils.encodeVaultId(vaultId); + const addResponse = await rpcClient.methods.vaultsSecretsNewDir({ + nameOrId: vaultsIdEncoded, + dirName: secretDir, + }); + expect(addResponse.success).toBeTruthy(); + // Delete secret + await testsUtils.expectRemoteError( + rpcClient.methods.vaultsSecretsRemove({ + nameOrId: vaultsIdEncoded, + secretNames: ['secretDir'], + }), + vaultsErrors.ErrorVaultsRecursive, + ); + const deleteResponse = await rpcClient.methods.vaultsSecretsRemove({ + nameOrId: vaultsIdEncoded, + secretNames: ['secretDir'], + options: { + recursive: true, + }, + }); + expect(deleteResponse.success).toBeTruthy(); + }); }); describe('vaultsSecretsNewDir and vaultsSecretsList', () => { const logger = new Logger('vaultsSecretsNewDirList test', LogLevel.WARN, [