Skip to content

Commit

Permalink
chore: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Aug 23, 2024
1 parent 369da6c commit a47ca61
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 30 deletions.
31 changes: 4 additions & 27 deletions src/secrets/CommandList.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
import type PolykeyClient from 'polykey/dist/PolykeyClient';
import CommandPolykey from '../CommandPolykey';
import { VaultFileNode } from 'polykey/dist/client/types';
import { SecretFiles } from 'polykey/dist/client/types';
import * as binUtils from '../utils';
import * as binOptions from '../utils/options';
import * as binProcessors from '../utils/processors';
import * as binParsers from '../utils/parsers';

function formatDate(date: Date): string {
const now = new Date();
const sixMonthsAgo = new Date(
now.getFullYear(),
now.getMonth() - 6,
now.getDate(),
);
const isRecent = date > sixMonthsAgo && date <= now;

const month = date.toLocaleString('en-US', { month: 'short' });
const day = date.getDate().toString().padStart(2, ' ');

if (isRecent) {
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
return `${month} ${day} ${hours}:${minutes}`;
} else {
const year = date.getFullYear().toString().padStart(5, ' ');
return `${month} ${day} ${year}`;
}
}

class CommandList extends CommandPolykey {
constructor(...args: ConstructorParameters<typeof CommandPolykey>) {
super(...args);
Expand Down Expand Up @@ -72,13 +50,12 @@ class CommandList extends CommandPolykey {
logger: this.logger.getChild(PolykeyClient.name),
});
const data = await binUtils.retryAuthentication(async (auth) => {
let data: Array<VaultFileNode> = [];
let data: Array<SecretFiles> = [];
const stream =
await pkClient.rpcClient.methods.vaultsSecretsGetFileTree({
await pkClient.rpcClient.methods.vaultsSecretsList({
metadata: auth,
nameOrId: vaultPattern[0],
pattern: vaultPattern[1] ?? '/',
yieldStats: false,
path: vaultPattern[1] ?? '/',
});
for await (const secret of stream) data.push(secret);

Expand Down
4 changes: 2 additions & 2 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function parseCoreCount(v: string): number | undefined {
}
}

function parseSecretName(secretPath: string): [string?, string?] {
function parseSecretName(secretPath: string): [string, string?] {
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
// If 'vault1', ['vault1, undefined] is returned
if (!secretPathNameRegex.test(secretPath)) {
Expand All @@ -76,7 +76,7 @@ function parseSecretName(secretPath: string): [string?, string?] {
}
// Returns [vaultName, secretName?]
const match = secretPath.match(secretPathNameRegex)!;
return [match[1] || undefined, match[2] || undefined];
return [match[1], match[2] || undefined];
}

function parseSecretPath(secretPath: string): [string, string, string?] {
Expand Down
3 changes: 2 additions & 1 deletion tests/secrets/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('commandListSecrets', () => {
await vaultOps.addSecret(vault, 'MySecret3', 'this is the secret 3');
});

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

const result = await testUtils.pkStdio([...command], {
env: {
Expand All @@ -62,6 +62,7 @@ describe('commandListSecrets', () => {
cwd: dataDir,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toBe('MySecret1\nMySecret2\nMySecret3\n');
},
globalThis.defaultTimeout * 2,
);
Expand Down

0 comments on commit a47ca61

Please sign in to comment.