Skip to content

Commit

Permalink
feat: updated tests for VaultsSecretsMkdir
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 1, 2024
1 parent fa6c728 commit f187d69
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 144 deletions.
1 change: 0 additions & 1 deletion src/client/handlers/VaultsSecretsList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class VaultsSecretsList extends ServerHandler<
> {
public async *handle(
input: ClientRPCRequestParams<SecretIdentifierMessage>,
_cancel: any,
): AsyncGenerator<ClientRPCResponseResult<SecretFilesMessage>, void, void> {
const { vaultManager, db } = this.container;
const vaultId = await db.withTransactionF(async (tran) => {
Expand Down
8 changes: 1 addition & 7 deletions src/client/handlers/VaultsSecretsMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,7 @@ class VaultsSecretsMkdir extends DuplexHandler<
});
for await (const data of response) {
if (data.success) yield { success: true };
if (data.error != null) {
yield {
success: false,
error: data.error.toJSON(),
};
}
throw data.error;
else yield { success: false, error: data.error };
}
},
tran,
Expand Down
4 changes: 2 additions & 2 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ type ContentMessage = {
};

type ContentWithErrorMessage = ContentMessage & {
error?: JSONObject;
error?: string;
};

type SecretContentMessage = SecretIdentifierMessage & ContentMessage;
Expand All @@ -322,7 +322,7 @@ type SecretDirMessage = VaultIdentifierMessage & {
};

type SuccessWithErrorMessage = SuccessMessage & {
error?: JSONObject;
error?: string;
};

type SecretRenameMessage = SecretIdentifierMessage & {
Expand Down
30 changes: 14 additions & 16 deletions src/vaults/VaultOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import type { Stat } from 'encryptedfs';
import path from 'path';
import * as vaultsErrors from './errors';
import * as vaultsUtils from './utils';
import * as errors from '../errors';

type FileOptions = {
recursive?: boolean;
};

type SuccessMessage = {
success: boolean;
error?: errors.ErrorPolykey<any>;
error?: string;
};

async function addSecret(
Expand Down Expand Up @@ -208,7 +207,6 @@ async function mkdir(
* Adds an empty directory to the root of the vault.
* i.e. mkdir("folder", { recursive = false }) creates the "<vaultDir>/folder" directory
*/
// TODO: add log messages back
async function* makeDirectories(
vault: Vault,
dirPaths: Array<string>,
Expand All @@ -220,23 +218,23 @@ async function* makeDirectories(
for (const dirPath of dirPaths) {
try {
await efs.mkdir(dirPath, fileOptions);
logger?.info(`Created secret directory at '${dirPath}'`);
yield { success: true };
} catch (e) {
logger?.info(`Failed to create directory '${dirPath}'. Why: ${e.code}`);
if (e.code === 'ENOENT' && !recursive) {
const error = new vaultsErrors.ErrorVaultsRecursive(
`could not create directory '${dirPath}' without recursive option`,
{ cause: e },
);
yield { success: false, error: error };
}
if (e.code === 'EEXIST') {
const error = new vaultsErrors.ErrorSecretsSecretDefined(
`${dirPath} already exists`,
{ cause: e },
);
yield { success: false, error: error };
yield {
success: false,
error: `${e.code}: cannot create directory ${dirPath}: No such secret or directory`,
};
} else if (e.code === 'EEXIST') {
yield {
success: false,
error: `${e.code}: cannot create directory ${dirPath}: Secret or directory exists`,
};
} else {
throw e;
}
throw e;
}
}
});
Expand Down
Loading

0 comments on commit f187d69

Please sign in to comment.