Skip to content

Commit

Permalink
feat: added tests
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
aryanjassal committed Sep 10, 2024
1 parent 150040e commit e324e4e
Showing 1 changed file with 89 additions and 2 deletions.
91 changes: 89 additions & 2 deletions tests/secrets/remove.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('commandDeleteSecret', () => {
});
});

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

Expand All @@ -53,7 +53,7 @@ describe('commandDeleteSecret', () => {

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

const result = await testUtils.pkStdio([...command], {
const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
cwd: dataDir,
});
Expand All @@ -64,4 +64,91 @@ describe('commandDeleteSecret', () => {
expect(list.sort()).toStrictEqual([]);
});
});
test('should remove multiple secrets', async () => {
const vaultName = 'Vault2' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretNames = ['secret1', 'secret2', 'secret3'];

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

const secretPaths = secretNames.map((v) => `${vaultName}:${v}`);
command = ['secrets', 'rm', '-np', dataDir, ...secretPaths];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
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('should remove secrets recursively', async () => {
const vaultName = 'Vault2' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretDir = 'secretDir';
const secretNames = ['secret1', 'secret2', 'secret3'].map((v) =>
path.join(secretDir, v),
);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
vaultOps.mkdir(vault, secretDir);
for (const secretName of secretNames) {
await vaultOps.addSecret(vault, secretName, secretName);
}
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual(secretNames);
});

command = ['secrets', 'rm', '-np', dataDir, `${vaultName}:secretDir`, '-r'];

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
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('should fail to remove directory without recursive flag', async () => {
const vaultName = 'Vault2' as VaultName;
const vaultId = await polykeyAgent.vaultManager.createVault(vaultName);
const secretDir = 'secretDir';
const secretNames = ['secret1', 'secret2', 'secret3'].map((v) =>
path.join(secretDir, v),
);

await polykeyAgent.vaultManager.withVaults([vaultId], async (vault) => {
vaultOps.mkdir(vault, secretDir);
for (const secretName of secretNames) {
await vaultOps.addSecret(vault, secretName, secretName);
}
const list = await vaultOps.listSecrets(vault);
expect(list.sort()).toStrictEqual(secretNames);
});

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

const result = await testUtils.pkStdio(command, {
env: { PK_PASSWORD: password },
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(secretNames);
});
});
});

0 comments on commit e324e4e

Please sign in to comment.