Skip to content

Commit

Permalink
feat: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 10, 2024
1 parent 8fe57a3 commit 9782f35
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
},
Expand Down
11 changes: 7 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,16 @@ type SecretPathMessage = {

type SecretPathsMessage = {
secretNames: Array<string>;
}
};

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 = {
Expand Down
105 changes: 98 additions & 7 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
VaultsPermissionSet,
VaultsPermissionUnset,
VaultsRename,
VaultsSecretsDelete,
VaultsSecretsRemove,
VaultsSecretsEdit,
VaultsSecretsEnv,
VaultsSecretsGet,
Expand All @@ -52,7 +52,7 @@ import {
vaultsPermissionSet,
vaultsPermissionUnset,
vaultsRename,
vaultsSecretsDelete,
vaultsSecretsRemove,
vaultsSecretsEdit,
vaultsSecretsEnv,
vaultsSecretsGet,
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -1396,14 +1397,19 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
db,
vaultManager,
}),
vaultsSecretsDelete: new VaultsSecretsDelete({
vaultsSecretsRemove: new VaultsSecretsRemove({
db,
vaultManager,
}),
vaultsSecretsGet: new VaultsSecretsGet({
db,
vaultManager,
}),
vaultsSecretsNewDir: new VaultsSecretsNewDir({
db,
fs,
vaultManager,
}),
},
host: localhost,
});
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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, [
Expand Down

0 comments on commit 9782f35

Please sign in to comment.