Skip to content

Commit

Permalink
chore: updated tests for vaultOps.mkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 4, 2024
1 parent 3fad8cf commit 75587d3
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 68 deletions.
2 changes: 1 addition & 1 deletion src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class VaultsSecretsMkdir extends DuplexHandler<
yield* vaultManager.withVaultsG(
[vaultId],
async function* (vault): AsyncGenerator<SuccessWithErrorMessage> {
const response = vaultOps.makeDirectories(vault, dirPaths, {
const response = vaultOps.mkdir(vault, dirPaths, {
recursive: metadata?.options?.recursive,
});
for await (const data of response) {
Expand Down
59 changes: 6 additions & 53 deletions tests/client/handlers/vaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ import * as vaultsUtils from '@/vaults/utils';
import * as vaultsErrors from '@/vaults/errors';
import * as networkUtils from '@/network/utils';
import * as testsUtils from '../../utils';
import { encode } from 'punycode';

describe('vaultsClone', () => {
const logger = new Logger('vaultsClone test', LogLevel.WARN, [
Expand Down Expand Up @@ -1484,53 +1483,6 @@ describe('vaultsSecretsMkdir', () => {
});
});
});
// test('makes directories in all vaults in a single log message', async () => {
// const vaultName1 = 'test-vault1';
// const vaultName2 = 'test-vault2';
// const vaultId1 = await vaultManager.createVault(vaultName1);
// const vaultId2 = await vaultManager.createVault(vaultName2);
// const vaultIdEncoded1 = vaultsUtils.encodeVaultId(vaultId1);
// const vaultIdEncoded2 = vaultsUtils.encodeVaultId(vaultId2);
// const dirPath1 = 'dir-1';
// const dirPath2 = 'dir-2';
// const dirPath3 = 'dir-3';
// let logLength1 = 0;
// let logLength2 = 0;
// await vaultManager.withVaults(
// [vaultId1, vaultId2],
// async (vault1, vault2) => {
// const log1 = await vault1.log();
// const log2 = await vault2.log();
// logLength1 = log1.length;
// logLength2 = log2.length;
// },
// );
// // Attempt to make directories
// const response = await rpcClient.methods.vaultsSecretsMkdir();
// const writer = response.writable.getWriter();
// await writer.write({
// nameOrId: vaultIdEncoded1,
// dirName: dirPath1,
// metadata: { options: { recursive: true } },
// });
// await writer.write({ nameOrId: vaultIdEncoded2, dirName: dirPath2 });
// await writer.write({ nameOrId: vaultIdEncoded1, dirName: dirPath3 });
// await writer.close();
// // Check if the operation concluded as expected
// for await (const data of response.readable) {
// expect(data.success).toBeTruthy();
// expect(data.error).toBeUndefined();
// }
// await vaultManager.withVaults(
// [vaultId1, vaultId2],
// async (vault1, vault2) => {
// const log1 = await vault1.log();
// const log2 = await vault2.log();
// expect(log1.length).toEqual(logLength1 + 1);
// expect(log2.length).toEqual(logLength2 + 1);
// },
// );
// });
});
describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
const logger = new Logger('vaultsSecretsNewDeleteGet test', LogLevel.WARN, [
Expand All @@ -1555,7 +1507,11 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
}>;
let vaultManager: VaultManager;
// Helper function to create secrets in a vault
const createVaultSecret = async (vaultId: VaultId, secretName: string, content: string) => {
const createVaultSecret = async (
vaultId: VaultId,
secretName: string,
content: string,
) => {
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.writeF(async (efs) => {
await efs.writeFile(secretName, content);
Expand All @@ -1564,10 +1520,7 @@ describe('vaultsSecretsNew and vaultsSecretsDelete, vaultsSecretsGet', () => {
});
};
// Helper function to ensure each file path was deleted
const checkSecretIsDeleted = async (
vaultId: VaultId,
secretName: string,
) => {
const checkSecretIsDeleted = async (vaultId: VaultId, secretName: string) => {
await vaultManager.withVaults([vaultId], async (vault) => {
await vault.readF(async (efs) => {
expect(await efs.exists(secretName)).toBeFalsy();
Expand Down
82 changes: 68 additions & 14 deletions tests/vaults/VaultOps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,32 +323,86 @@ describe('VaultOps', () => {
});
describe('mkdir', () => {
test('can create directory', async () => {
await vaultOps.mkdir(vault, dirName);
const response = vaultOps.mkdir(vault, [dirName]);
// For await (const _ of response);
for await (const data of response) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await expectDirExists(dirName);
});
test('can create recursive directory', async () => {
const dirPath = path.join(dirName, dirName);
await vaultOps.mkdir(vault, dirPath, { recursive: true });
const response = vaultOps.mkdir(vault, [dirPath], {
recursive: true,
});
// For await (const _ of response);
for await (const data of response) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await expectDirExists(dirPath);
});
test('creating recursive directory fails without recursive set', async () => {
const dirPath = path.join(dirName, dirName);
await expect(vaultOps.mkdir(vault, dirPath)).rejects.toThrow(
vaultsErrors.ErrorVaultsRecursive,
);
const response = vaultOps.mkdir(vault, [dirPath]);
for await (const data of response) {
expect(data.success).toBeFalsy();
expect(data.error).toBeDefined();
}
await expectDirExistsNot(dirPath);
});
test('creating existing directory should fail', async () => {
await mkdir(dirName);
await expect(vaultOps.mkdir(vault, dirName)).rejects.toThrow(
vaultsErrors.ErrorSecretsSecretDefined,
);
const response = vaultOps.mkdir(vault, [dirName]);
for await (const data of response) {
expect(data.success).toBeFalsy();
expect(data.error).toBeDefined();
}
});
test('creating existing secret should fail', async () => {
await writeSecret(secretName, secretContent);
await expect(vaultOps.mkdir(vault, secretName)).rejects.toThrow(
vaultsErrors.ErrorSecretsSecretDefined,
);
const response = vaultOps.mkdir(vault, [secretName]);
for await (const data of response) {
expect(data.success).toBeFalsy();
expect(data.error).toBeDefined();
}
});
test('can create multiple directories at the same time', async () => {
const dirName1 = `${dirName}1`;
const dirName2 = `${dirName}2`;
const dirName3 = `${dirName}3`;
const response = vaultOps.mkdir(vault, [dirName1, dirName2, dirName3]);
for await (const data of response) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await expectDirExists(dirName1);
await expectDirExists(dirName2);
await expectDirExists(dirName3);
});
test('can create multiple directories recursively', async () => {
const dirName1 = `${dirName}1`;
const dirName2 = `${dirName}/${dirName}2`;
const response = vaultOps.mkdir(vault, [dirName1, dirName2], {
recursive: true,
});
for await (const data of response) {
expect(data.success).toBeTruthy();
expect(data.error).toBeUndefined();
}
await expectDirExists(dirName1);
await expectDirExists(dirName2);
});
test('fails to make recursive directories without recursive', async () => {
const dirNameRecursive = `${dirName}/${dirName}2`;
const response = vaultOps.mkdir(vault, [dirNameRecursive]);
for await (const data of response) {
expect(data.success).toBeFalsy();
expect(data.error).toBeDefined();
}
await expectDirExistsNot(dirName);
await expectDirExistsNot(dirNameRecursive);
});
});
describe('addSecretDirectory', () => {
Expand Down Expand Up @@ -441,7 +495,7 @@ describe('VaultOps', () => {
'secret5',
);

await vaultOps.mkdir(vault, secretDirName, { recursive: true });
vaultOps.mkdir(vault, [secretDirName], { recursive: true });
await vaultOps.addSecret(
vault,
path.join(secretDirName, 'secret1'),
Expand Down Expand Up @@ -520,7 +574,7 @@ describe('VaultOps', () => {
});
test('adding hidden files and directories', async () => {
await vaultOps.addSecret(vault, '.hiddenSecret', 'hidden_contents');
await vaultOps.mkdir(vault, '.hiddenDir', { recursive: true });
vaultOps.mkdir(vault, ['.hiddenDir'], { recursive: true });
await vaultOps.addSecret(
vault,
'.hiddenDir/.hiddenInSecret',
Expand All @@ -535,7 +589,7 @@ describe('VaultOps', () => {
'updating and deleting hidden files and directories',
async () => {
await vaultOps.addSecret(vault, '.hiddenSecret', 'hidden_contents');
await vaultOps.mkdir(vault, '.hiddenDir', { recursive: true });
vaultOps.mkdir(vault, ['.hiddenDir'], { recursive: true });
await vaultOps.addSecret(
vault,
'.hiddenDir/.hiddenInSecret',
Expand Down

0 comments on commit 75587d3

Please sign in to comment.