Skip to content

Commit

Permalink
chore: updated most tests to test for this behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Oct 18, 2024
1 parent 616df30 commit 23a05d0
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 165 deletions.
6 changes: 3 additions & 3 deletions src/secrets/CommandMkdir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CommandMkdir extends CommandPolykey {
this.addOption(binOptions.nodeId);
this.addOption(binOptions.clientHost);
this.addOption(binOptions.clientPort);
this.addOption(binOptions.recursive);
this.addOption(binOptions.parents);
this.action(async (secretPaths, options) => {
secretPaths = secretPaths.map((path: string) =>
binParsers.parseSecretPath(path),
Expand Down Expand Up @@ -67,9 +67,9 @@ class CommandMkdir extends CommandPolykey {
for (const [vault, path] of secretPaths) {
await writer.write({
nameOrId: vault,
dirName: path,
dirName: path ?? '/',
metadata: first
? { ...auth, options: { recursive: options.recursive } }
? { ...auth, options: { recursive: options.parents } }
: undefined,
});
first = false;
Expand Down
2 changes: 1 addition & 1 deletion src/secrets/CommandWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CommandWrite extends CommandPolykey {
await pkClient.rpcClient.methods.vaultsSecretsWriteFile({
metadata: auth,
nameOrId: secretPath[0],
secretName: secretPath[1],
secretName: secretPath[1] ?? '/',
secretContent: stdin,
}),
meta,
Expand Down
8 changes: 7 additions & 1 deletion src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,12 @@ const order = new commander.Option(

const recursive = new commander.Option(
'--recursive',
'If enabled, specified directories will be removed along with their contents',
'If enabled, specified operation will be applied recursively to the directory and its contents',
).default(false);

const parents = new commander.Option(
'--parents',
'If enabled, create all parent directories as well. If the directories exist, do nothing.',
).default(false);

export {
Expand Down Expand Up @@ -349,4 +354,5 @@ export {
limit,
order,
recursive,
parents,
};
18 changes: 17 additions & 1 deletion tests/secrets/cat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ describe('commandCatSecret', () => {
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe(secretContent);
});
test('should fail when reading root without secret path', async () => {
const vaultName = 'Vault3' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret-name';
const secretContent = 'this is the contents of the secret';
await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, secretName, secretContent);
});
const command = ['secrets', 'cat', '-np', dataDir, vaultName];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
expect(result.stderr).toBeDefined();
});
test('should concatenate multiple secrets', async () => {
const vaultName = 'Vault3' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
Expand Down Expand Up @@ -169,7 +185,7 @@ describe('commandCatSecret', () => {
resolve(exitCode);
});
});
expect(exitCode).toStrictEqual(0);
expect(exitCode).toBe(0);
expect(stdout).toBe(stdinData);
});
});
93 changes: 55 additions & 38 deletions tests/secrets/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('commandEditSecret', () => {
let editorFail: string;
let editorView: string;
let polykeyAgent: PolykeyAgent;
let command: Array<string>;

beforeEach(async () => {
dataDir = await fs.promises.mkdtemp(
Expand Down Expand Up @@ -59,6 +58,7 @@ describe('commandEditSecret', () => {
logger: logger,
});
});

afterEach(async () => {
await polykeyAgent.stop();
await fs.promises.rm(dataDir, {
Expand All @@ -68,108 +68,125 @@ describe('commandEditSecret', () => {
});

test('should edit secret', async () => {
const vaultName = 'Vault10' as VaultName;
const vaultName = 'vault' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret';

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, secretName, 'original secret');
});

command = ['secrets', 'edit', '-np', dataDir, `${vaultName}:${secretName}`];

const result = await testUtils.pkStdio([...command], {
const command = [
'secrets',
'edit',
'-np',
dataDir,
`${vaultName}:${secretName}`,
];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorEdit },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const contents = await vaultOps.getSecret(vault, secretName);
expect(contents.toString()).toStrictEqual(`${editedContent}\n`);
});
});

test('should fail to edit without a secret path specified', async () => {
const vaultName = 'vault' as VaultName;
const command = ['secrets', 'edit', '-np', dataDir, vaultName];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorEdit },
cwd: dataDir,
});
expect(result.exitCode).not.toBe(0);
});
test('should create secret if it does not exist', async () => {
const vaultName = 'Vault10' as VaultName;
const vaultName = 'vault' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret';

command = ['secrets', 'edit', '-np', dataDir, `${vaultName}:${secretName}`];

const result = await testUtils.pkStdio([...command], {
const command = [
'secrets',
'edit',
'-np',
dataDir,
`${vaultName}:${secretName}`,
];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorEdit },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const contents = await vaultOps.getSecret(vault, secretName);
expect(contents.toString()).toStrictEqual(`${editedContent}\n`);
});
});

test('should not create secret if editor crashes', async () => {
const vaultName = 'Vault10' as VaultName;
const vaultName = 'vault' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret';

command = ['secrets', 'edit', '-np', dataDir, `${vaultName}:${secretName}`];

const result = await testUtils.pkStdio([...command], {
const command = [
'secrets',
'edit',
'-np',
dataDir,
`${vaultName}:${secretName}`,
];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorFail },
cwd: dataDir,
});
expect(result.exitCode).not.toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual([]);
});
});

test('should not create secret if editor does not write to file', async () => {
const vaultName = 'Vault10' as VaultName;
const vaultName = 'vault' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret';

command = ['secrets', 'edit', '-np', dataDir, `${vaultName}:${secretName}`];

const result = await testUtils.pkStdio([...command], {
const command = [
'secrets',
'edit',
'-np',
dataDir,
`${vaultName}:${secretName}`,
];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorExit },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual([]);
});
});

test('file contents should be fetched correctly', async () => {
const vaultName = 'Vault10' as VaultName;
const vaultName = 'vault' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretName = 'secret';
const secretContent = 'original secret';

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, secretName, secretContent);
});

command = ['secrets', 'edit', '-np', dataDir, `${vaultName}:${secretName}`];

const result = await testUtils.pkStdio([...command], {
const command = [
'secrets',
'edit',
'-np',
dataDir,
`${vaultName}:${secretName}`,
];
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password, EDITOR: editorView },
cwd: dataDir,
});
expect(result.exitCode).toBe(0);

const fetchedSecret = await fs.promises.readFile(
path.join(dataDir, 'secret'),
);
expect(fetchedSecret.toString()).toEqual(secretContent);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
const contents = await vaultOps.getSecret(vault, secretName);
expect(contents.toString()).toStrictEqual(`${editedContent}\n`);
Expand Down
Loading

0 comments on commit 23a05d0

Please sign in to comment.