Skip to content

Commit

Permalink
chore: added tests for vaultOps
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 30, 2024
1 parent ce53457 commit b4eb8fa
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 80 deletions.
162 changes: 82 additions & 80 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = [];
for await (const data of getStream.readable) {
secretContent.push(data.secretContent);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions tests/vaults/VaultOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit b4eb8fa

Please sign in to comment.