From cebb21c737cd21eb805014acde8b5661c23b3236 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Mon, 26 Aug 2024 19:33:45 +1000 Subject: [PATCH] chore: updated tests and added edge cases --- tests/secrets/list.test.ts | 103 +++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 4 deletions(-) diff --git a/tests/secrets/list.test.ts b/tests/secrets/list.test.ts index caeed2ab..3f39c2b6 100644 --- a/tests/secrets/list.test.ts +++ b/tests/secrets/list.test.ts @@ -41,6 +41,21 @@ 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 () => { @@ -48,13 +63,12 @@ describe('commandListSecrets', () => { 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, @@ -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, + ); });