From 75587d3df4dd169ca916ea1011f8032208d7999f Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Fri, 4 Oct 2024 15:15:22 +1000 Subject: [PATCH] chore: updated tests for vaultOps.mkdir --- src/client/handlers/VaultsSecretsMkdir.ts | 2 +- tests/client/handlers/vaults.test.ts | 59 ++-------------- tests/vaults/VaultOps.test.ts | 82 +++++++++++++++++++---- 3 files changed, 75 insertions(+), 68 deletions(-) diff --git a/src/client/handlers/VaultsSecretsMkdir.ts b/src/client/handlers/VaultsSecretsMkdir.ts index 6c341085b..d5a270d5f 100644 --- a/src/client/handlers/VaultsSecretsMkdir.ts +++ b/src/client/handlers/VaultsSecretsMkdir.ts @@ -54,7 +54,7 @@ class VaultsSecretsMkdir extends DuplexHandler< yield* vaultManager.withVaultsG( [vaultId], async function* (vault): AsyncGenerator { - const response = vaultOps.makeDirectories(vault, dirPaths, { + const response = vaultOps.mkdir(vault, dirPaths, { recursive: metadata?.options?.recursive, }); for await (const data of response) { diff --git a/tests/client/handlers/vaults.test.ts b/tests/client/handlers/vaults.test.ts index 225f3396f..43a5b9566 100644 --- a/tests/client/handlers/vaults.test.ts +++ b/tests/client/handlers/vaults.test.ts @@ -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, [ @@ -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, [ @@ -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); @@ -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(); diff --git a/tests/vaults/VaultOps.test.ts b/tests/vaults/VaultOps.test.ts index bd329d464..5f0112176 100644 --- a/tests/vaults/VaultOps.test.ts +++ b/tests/vaults/VaultOps.test.ts @@ -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', () => { @@ -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'), @@ -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', @@ -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',