From b4eb8fa9d3de1f3b946b7ab5b0cdda8b69bc8706 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Mon, 30 Sep 2024 14:45:37 +1000 Subject: [PATCH] chore: added tests for vaultOps --- tests/client/handlers/vaults.test.ts | 162 ++++++++++++++------------- tests/vaults/VaultOps.test.ts | 18 +++ 2 files changed, 100 insertions(+), 80 deletions(-) diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index 825a15ad8..df44c11ce 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -1511,14 +1511,12 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(createResponse.success).toBeTruthy(); // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await (async () => { - const writer = getStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded, - secretName: secretName, - }); - await writer.close(); - })(); + const getWriter = getStream.writable.getWriter(); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName, + }); + await getWriter.close(); const secretContent: Array = []; for await (const data of getStream.readable) { secretContent.push(data.secretContent); @@ -1527,25 +1525,21 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { expect(concatenatedContent).toStrictEqual(secretName); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await (async () => { - const writer = deleteStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded, - secretName: secretName, - }); - await writer.close(); - })(); + const deleteWriter = deleteStream.writable.getWriter(); + await deleteWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName, + }); + await deleteWriter.close(); expect((await deleteStream.output).success).toBeTruthy(); // Check secret was deleted const deleteGetStream = await rpcClient.methods.vaultsSecretsGet(); - await (async () => { - const writer = deleteGetStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded, - secretName: secretName, - }); - await writer.close(); - })(); + const deleteGetWriter = deleteGetStream.writable.getWriter(); + await deleteGetWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName, + }); + await deleteGetWriter.close(); await testsUtils.expectRemoteError( (async () => { for await (const _ of deleteGetStream.readable); @@ -1565,13 +1559,20 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { await createVaultSecret(vaultIdEncoded, secretName3); // Get secrets const getStream = await rpcClient.methods.vaultsSecretsGet(); - await (async () => { - const writer = getStream.writable.getWriter(); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName1 }); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName2 }); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName3 }); - await writer.close(); - })(); + const getWriter = getStream.writable.getWriter(); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName1, + }); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName2, + }); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName3, + }); + await getWriter.close(); let secretContent: string = ''; for await (const data of getStream.readable) { secretContent += data.secretContent; @@ -1590,17 +1591,18 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { await createVaultSecret(vaultIdEncoded, secretName2); // Get secrets const getStream = await rpcClient.methods.vaultsSecretsGet(); - await (async () => { - const writer = getStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded, - secretName: secretName1, - metadata: { options: { continueOnError: true } }, - }); - await writer.write({ nameOrId: vaultIdEncoded, secretName: 'invalid' }); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName2 }); - await writer.close(); - })(); + const getWriter = getStream.writable.getWriter(); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName1, + metadata: { options: { continueOnError: true } }, + }); + await getWriter.write({ nameOrId: vaultIdEncoded, secretName: 'invalid' }); + await getWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName2, + }); + await getWriter.close(); let secretContent: string = ''; let errorContent: string = ''; await expect( @@ -1624,12 +1626,16 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { await createVaultSecret(vaultIdEncoded, secretName2); // Delete secrets const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await (async () => { - const writer = deleteStream.writable.getWriter(); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName1 }); - await writer.write({ nameOrId: vaultIdEncoded, secretName: secretName2 }); - await writer.close(); - })(); + const deleteWriter = deleteStream.writable.getWriter(); + await deleteWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName1, + }); + await deleteWriter.write({ + nameOrId: vaultIdEncoded, + secretName: secretName2, + }); + await deleteWriter.close(); expect((await deleteStream.output).success).toBeTruthy(); // Check each secret was deleted await checkSecretIsDeleted(vaultIdEncoded, secretName1); @@ -1649,22 +1655,20 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { await createVaultSecret(vaultIdEncoded2, secretName3); // Get secret const getStream = await rpcClient.methods.vaultsSecretsGet(); - await (async () => { - const writer = getStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded1, - secretName: secretName1, - }); - await writer.write({ - nameOrId: vaultIdEncoded1, - secretName: secretName2, - }); - await writer.write({ - nameOrId: vaultIdEncoded2, - secretName: secretName3, - }); - await writer.close(); - })(); + const getWriter = getStream.writable.getWriter(); + await getWriter.write({ + nameOrId: vaultIdEncoded1, + secretName: secretName1, + }); + await getWriter.write({ + nameOrId: vaultIdEncoded1, + secretName: secretName2, + }); + await getWriter.write({ + nameOrId: vaultIdEncoded2, + secretName: secretName3, + }); + await getWriter.close(); let secretContent: string = ''; for await (const data of getStream.readable) { secretContent += data.secretContent; @@ -1697,22 +1701,20 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => { ); // Delete secret const deleteStream = await rpcClient.methods.vaultsSecretsRemove(); - await (async () => { - const writer = deleteStream.writable.getWriter(); - await writer.write({ - nameOrId: vaultIdEncoded1, - secretName: secretName1, - }); - await writer.write({ - nameOrId: vaultIdEncoded1, - secretName: secretName2, - }); - await writer.write({ - nameOrId: vaultIdEncoded2, - secretName: secretName3, - }); - await writer.close(); - })(); + const deleteWriter = deleteStream.writable.getWriter(); + await deleteWriter.write({ + nameOrId: vaultIdEncoded1, + secretName: secretName1, + }); + await deleteWriter.write({ + nameOrId: vaultIdEncoded1, + secretName: secretName2, + }); + await deleteWriter.write({ + nameOrId: vaultIdEncoded2, + secretName: secretName3, + }); + await deleteWriter.close(); expect((await deleteStream.output).success).toBeTruthy(); // Ensure single log message for deleting the secrets await vaultManager.withVaults( diff --git a/tests/vaults/VaultOps.test.ts b/tests/vaults/VaultOps.test.ts index 8c1f5da32..bd329d464 100644 --- a/tests/vaults/VaultOps.test.ts +++ b/tests/vaults/VaultOps.test.ts @@ -500,6 +500,24 @@ describe('VaultOps', () => { expect(secretList).toHaveLength(0); }); }); + describe('writeSecret', () => { + const secretName = 'secret'; + const secretContent = 'secret-content'; + const newSecretContent = 'updated-secret-content'; + + test('updates existing secret', async () => { + await vaultOps.addSecret(vault, secretName, secretContent); + await vaultOps.writeSecret(vault, secretName, newSecretContent); + const result = await vaultOps.getSecret(vault, secretName); + expect(result.toString()).toStrictEqual(newSecretContent); + }); + + test('creates new secret if it does not exist', async () => { + await vaultOps.writeSecret(vault, secretName, newSecretContent); + const result = await vaultOps.getSecret(vault, secretName); + expect(result.toString()).toStrictEqual(newSecretContent); + }); + }); test('adding hidden files and directories', async () => { await vaultOps.addSecret(vault, '.hiddenSecret', 'hidden_contents'); await vaultOps.mkdir(vault, '.hiddenDir', { recursive: true });