Skip to content

Commit

Permalink
chore: updated tests and added edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 26, 2024
1 parent f20b1e6 commit cebb21c
Showing 1 changed file with 99 additions and 4 deletions.
103 changes: 99 additions & 4 deletions tests/secrets/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,34 @@ describe('commandListSecrets', () => {
});
});

test(
'should fail when vault does not exist',
async () => {
command = ['secrets', 'ls', '-np', dataDir, 'DoesntExist'];
const result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(64); // sysexits.USAGE
},
globalThis.defaultTimeout * 2,
);

test(
'should list secrets',
async () => {
const vaultName = 'Vault4' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.addSecret(vault, 'MySecret1', 'this is the secret 1');
await vaultOps.addSecret(vault, 'MySecret2', 'this is the secret 2');
await vaultOps.addSecret(vault, 'MySecret3', 'this is the secret 3');
await vaultOps.addSecret(vault, 'MySecret1', '');
await vaultOps.addSecret(vault, 'MySecret2', '');
await vaultOps.addSecret(vault, 'MySecret3', '');
});

command = ['secrets', 'ls', '-np', dataDir, vaultName];

const result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
Expand All @@ -66,4 +80,85 @@ describe('commandListSecrets', () => {
},
globalThis.defaultTimeout * 2,
);

test(
'should fail when path is not a directory',
async () => {
const vaultName = 'Vault5' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.mkdir(vault, 'SecretDir');
await vaultOps.addSecret(vault, 'SecretDir/MySecret1', '');
await vaultOps.addSecret(vault, 'SecretDir/MySecret2', '');
await vaultOps.addSecret(vault, 'SecretDir/MySecret3', '');
});

command = ['secrets', 'ls', '-np', dataDir, `${vaultName}:WrongDirName`];
let result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(64);

command = [
'secrets',
'ls',
'-np',
dataDir,
`${vaultName}:SecretDir/MySecret1`,
];
result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(64);
},
globalThis.defaultTimeout * 2,
);

test(
'should list secrets within directories',
async () => {
const vaultName = 'Vault6' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
await vaultOps.mkdir(vault, 'SecretDir/NestedDir', { recursive: true });
await vaultOps.addSecret(vault, 'SecretDir/MySecret1', '');
await vaultOps.addSecret(vault, 'SecretDir/NestedDir/MySecret2', '');
});

command = ['secrets', 'ls', '-np', dataDir, `${vaultName}:SecretDir`];
let result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('SecretDir/MySecret1\nSecretDir/NestedDir\n');

command = [
'secrets',
'ls',
'-np',
dataDir,
`${vaultName}:SecretDir/NestedDir`,
];
result = await testUtils.pkStdio([...command], {
env: {
PK_PASSWORD: password,
},
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('SecretDir/NestedDir/MySecret2\n');
},
globalThis.defaultTimeout * 2,
);
});

0 comments on commit cebb21c

Please sign in to comment.