Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"@anthropic-ai/sdk": "^0.124.0",
"@inquirer/prompts": "^8.5.2",
"@napi-rs/keyring": "^1.2.0",
"@workos-inc/node": "^8.7.0",
"@workos/emulate": "^0.12.0",
"@workos/migrations": "^2.4.0",
"@workos/openapi-spec": "^0.83.0",
"@workos/skills": "0.7.1",
"@workos-inc/node": "^10.13.0",
"@workos/emulate": "^0.13.1",
"@workos/migrations": "^2.6.0",
"@workos/openapi-spec": "^0.100.0",
"@workos/skills": "0.7.3",
"chalk": "^5.6.2",
"diff": "^8.0.3",
"fast-glob": "^3.3.3",
Expand Down
48 changes: 27 additions & 21 deletions src/commands/api-key-mgmt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { WorkOS } from '@workos-inc/node';

const mockSdk = {
organizations: {
apiKeys: {
listOrganizationApiKeys: vi.fn(),
createOrganizationApiKey: vi.fn(),
},
apiKeys: {
validateApiKey: vi.fn(),
createValidation: vi.fn(),
deleteApiKey: vi.fn(),
},
};
} satisfies { apiKeys: Partial<WorkOS['apiKeys']> };

vi.mock('../lib/workos-client.js', () => ({
createWorkOSClient: () => ({ sdk: mockSdk }),
Expand Down Expand Up @@ -45,22 +44,29 @@ describe('api-key-mgmt commands', () => {
vi.restoreAllMocks();
});

it('mocks methods that exist on the installed SDK', () => {
const sdk = new WorkOS('sk_test');
for (const method of Object.keys(mockSdk.apiKeys)) {
expect(sdk.apiKeys).toHaveProperty(method, expect.any(Function));
}
});

describe('runApiKeyList', () => {
it('lists keys in table', async () => {
mockSdk.organizations.listOrganizationApiKeys.mockResolvedValue({
mockSdk.apiKeys.listOrganizationApiKeys.mockResolvedValue({
data: [mockApiKey],
listMetadata: { before: null, after: null },
});
await runApiKeyList({ organizationId: 'org_456' }, 'sk_test');
expect(mockSdk.organizations.listOrganizationApiKeys).toHaveBeenCalledWith(
expect(mockSdk.apiKeys.listOrganizationApiKeys).toHaveBeenCalledWith(
expect.objectContaining({ organizationId: 'org_456' }),
);
expect(consoleOutput.some((l) => l.includes('key_123'))).toBe(true);
expect(consoleOutput.some((l) => l.includes('My Key'))).toBe(true);
});

it('handles empty results', async () => {
mockSdk.organizations.listOrganizationApiKeys.mockResolvedValue({
mockSdk.apiKeys.listOrganizationApiKeys.mockResolvedValue({
data: [],
listMetadata: { before: null, after: null },
});
Expand All @@ -69,39 +75,39 @@ describe('api-key-mgmt commands', () => {
});

it('passes pagination params', async () => {
mockSdk.organizations.listOrganizationApiKeys.mockResolvedValue({
mockSdk.apiKeys.listOrganizationApiKeys.mockResolvedValue({
data: [],
listMetadata: { before: null, after: null },
});
await runApiKeyList({ organizationId: 'org_456', limit: 5, order: 'desc' }, 'sk_test');
expect(mockSdk.organizations.listOrganizationApiKeys).toHaveBeenCalledWith(
expect(mockSdk.apiKeys.listOrganizationApiKeys).toHaveBeenCalledWith(
expect.objectContaining({ limit: 5, order: 'desc' }),
);
});
});

describe('runApiKeyCreate', () => {
it('creates API key with org and name', async () => {
mockSdk.organizations.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
mockSdk.apiKeys.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
await runApiKeyCreate({ organizationId: 'org_456', name: 'My Key' }, 'sk_test');
expect(mockSdk.organizations.createOrganizationApiKey).toHaveBeenCalledWith({
expect(mockSdk.apiKeys.createOrganizationApiKey).toHaveBeenCalledWith({
organizationId: 'org_456',
name: 'My Key',
});
});

it('displays key value warning in human mode', async () => {
mockSdk.organizations.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
mockSdk.apiKeys.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
await runApiKeyCreate({ organizationId: 'org_456', name: 'My Key' }, 'sk_test');
expect(consoleOutput.some((l) => l.includes('Created API key'))).toBe(true);
expect(consoleOutput.some((l) => l.includes('sk_test_full_key'))).toBe(true);
expect(consoleOutput.some((l) => l.includes('not be shown again'))).toBe(true);
});

it('passes permissions when provided', async () => {
mockSdk.organizations.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
mockSdk.apiKeys.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
await runApiKeyCreate({ organizationId: 'org_456', name: 'My Key', permissions: ['read', 'write'] }, 'sk_test');
expect(mockSdk.organizations.createOrganizationApiKey).toHaveBeenCalledWith({
expect(mockSdk.apiKeys.createOrganizationApiKey).toHaveBeenCalledWith({
organizationId: 'org_456',
name: 'My Key',
permissions: ['read', 'write'],
Expand All @@ -111,14 +117,14 @@ describe('api-key-mgmt commands', () => {

describe('runApiKeyValidate', () => {
it('validates API key', async () => {
mockSdk.apiKeys.validateApiKey.mockResolvedValue({ apiKey: mockApiKey });
mockSdk.apiKeys.createValidation.mockResolvedValue({ apiKey: mockApiKey });
await runApiKeyValidate('sk_test_value', 'sk_test');
expect(mockSdk.apiKeys.validateApiKey).toHaveBeenCalledWith({ value: 'sk_test_value' });
expect(mockSdk.apiKeys.createValidation).toHaveBeenCalledWith({ value: 'sk_test_value' });
expect(consoleOutput.some((l) => l.includes('valid'))).toBe(true);
});

it('handles invalid key (null result)', async () => {
mockSdk.apiKeys.validateApiKey.mockResolvedValue({ apiKey: null });
mockSdk.apiKeys.createValidation.mockResolvedValue({ apiKey: null });
await runApiKeyValidate('sk_test_invalid', 'sk_test');
expect(consoleOutput.some((l) => l.includes('invalid'))).toBe(true);
});
Expand All @@ -138,7 +144,7 @@ describe('api-key-mgmt commands', () => {
afterEach(() => setOutputMode('human'));

it('list outputs { data, listMetadata }', async () => {
mockSdk.organizations.listOrganizationApiKeys.mockResolvedValue({
mockSdk.apiKeys.listOrganizationApiKeys.mockResolvedValue({
data: [mockApiKey],
listMetadata: { before: null, after: 'cursor_a' },
});
Expand All @@ -150,15 +156,15 @@ describe('api-key-mgmt commands', () => {
});

it('create includes key value in output', async () => {
mockSdk.organizations.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
mockSdk.apiKeys.createOrganizationApiKey.mockResolvedValue({ ...mockApiKey, value: 'sk_test_full_key' });
await runApiKeyCreate({ organizationId: 'org_456', name: 'My Key' }, 'sk_test');
const output = JSON.parse(consoleOutput[0]);
expect(output.status).toBe('ok');
expect(output.data.value).toBe('sk_test_full_key');
});

it('validate outputs raw JSON', async () => {
mockSdk.apiKeys.validateApiKey.mockResolvedValue({ apiKey: mockApiKey });
mockSdk.apiKeys.createValidation.mockResolvedValue({ apiKey: mockApiKey });
await runApiKeyValidate('sk_test_value', 'sk_test');
const output = JSON.parse(consoleOutput[0]);
expect(output.apiKey.id).toBe('key_123');
Expand Down
6 changes: 3 additions & 3 deletions src/commands/api-key-mgmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function runApiKeyList(options: ApiKeyListOptions, apiKey: string,
const client = createWorkOSClient(apiKey, baseUrl);

try {
const result = await client.sdk.organizations.listOrganizationApiKeys({
const result = await client.sdk.apiKeys.listOrganizationApiKeys({
organizationId: options.organizationId,
limit: options.limit,
before: options.before,
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function runApiKeyCreate(options: ApiKeyCreateOptions, apiKey: stri
const client = createWorkOSClient(apiKey, baseUrl);

try {
const result = await client.sdk.organizations.createOrganizationApiKey({
const result = await client.sdk.apiKeys.createOrganizationApiKey({
organizationId: options.organizationId,
name: options.name,
...(options.permissions && { permissions: options.permissions }),
Expand All @@ -86,7 +86,7 @@ export async function runApiKeyValidate(value: string, apiKey: string, baseUrl?:
const client = createWorkOSClient(apiKey, baseUrl);

try {
const result = await client.sdk.apiKeys.validateApiKey({ value });
const result = await client.sdk.apiKeys.createValidation({ value });

if (isJsonMode()) {
outputJson(result);
Expand Down
Loading
Loading