refactor(agent): move the connector credential helpers out of VaultStore (P1.2)
getConnectorCredential, setConnectorCredential and setConnectorCredentialBundle are now plain functions over a SecretStore in packages/agent/src/connectors/connector-credentials.ts. The secret names, metadata layout and the rule that tokens never go into plaintext metadata are unchanged, so existing vault files keep working. A bundle still lands as one update: SecretStore gains writeBatch, which VaultStore applies with a single vault-file replacement. Connector tests seed a Map-backed SecretStore instead of mocking VaultStore methods; the helper tests from core and server moved to the agent package.
This commit is contained in:
186
packages/agent/tests/connectors/connector-credentials.test.ts
Normal file
186
packages/agent/tests/connectors/connector-credentials.test.ts
Normal file
@@ -0,0 +1,186 @@
|
||||
import { describe, it, expect, afterEach, vi } from 'vitest';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { VaultStore } from '@waggle/core';
|
||||
import {
|
||||
getConnectorCredential,
|
||||
setConnectorCredential,
|
||||
setConnectorCredentialBundle,
|
||||
} from '../../src/connectors/connector-credentials.js';
|
||||
|
||||
describe('connector credentials in the secret store', () => {
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function openVault(): { vault: VaultStore; dir: string } {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'waggle-connector-cred-'));
|
||||
tempDirs.push(dir);
|
||||
return { vault: new VaultStore(dir), dir };
|
||||
}
|
||||
|
||||
function readVaultFile(dir: string): Record<string, { encrypted: string; metadata?: Record<string, unknown>; updatedAt: string }> {
|
||||
return JSON.parse(fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8'));
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs) fs.rmSync(dir, { recursive: true, force: true });
|
||||
tempDirs.length = 0;
|
||||
});
|
||||
|
||||
it('stores a credential and reads it back with its metadata', () => {
|
||||
const { vault } = openVault();
|
||||
|
||||
setConnectorCredential(vault, 'github', {
|
||||
type: 'bearer',
|
||||
value: 'ghp_test_token_12345',
|
||||
scopes: ['repo', 'user'],
|
||||
});
|
||||
|
||||
const cred = getConnectorCredential(vault, 'github');
|
||||
expect(cred).not.toBeNull();
|
||||
expect(cred!.value).toBe('ghp_test_token_12345');
|
||||
expect(cred!.type).toBe('bearer');
|
||||
expect(cred!.scopes).toEqual(['repo', 'user']);
|
||||
expect(cred!.isExpired).toBe(false);
|
||||
});
|
||||
|
||||
it('reports a credential past its expiry as expired', () => {
|
||||
const { vault } = openVault();
|
||||
|
||||
setConnectorCredential(vault, 'slack', {
|
||||
type: 'oauth2',
|
||||
value: 'xoxb-expired-token',
|
||||
expiresAt: '2020-01-01T00:00:00.000Z',
|
||||
});
|
||||
|
||||
expect(getConnectorCredential(vault, 'slack')!.isExpired).toBe(true);
|
||||
});
|
||||
|
||||
it('returns null for a connector without a credential, and after it is deleted', () => {
|
||||
const { vault } = openVault();
|
||||
expect(getConnectorCredential(vault, 'nonexistent')).toBeNull();
|
||||
|
||||
setConnectorCredential(vault, 'github', { type: 'bearer', value: 'ghp_test_token' });
|
||||
expect(getConnectorCredential(vault, 'github')).not.toBeNull();
|
||||
|
||||
vault.delete('connector:github');
|
||||
expect(getConnectorCredential(vault, 'github')).toBeNull();
|
||||
});
|
||||
|
||||
it('defaults the type to api_key when the stored metadata has none', () => {
|
||||
const { vault } = openVault();
|
||||
vault.set('connector:legacy', 'legacy-token');
|
||||
|
||||
expect(getConnectorCredential(vault, 'legacy')).toMatchObject({
|
||||
value: 'legacy-token',
|
||||
type: 'api_key',
|
||||
isExpired: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('keeps the refresh token out of plaintext, in its own encrypted entry', () => {
|
||||
const { vault, dir } = openVault();
|
||||
const refreshToken = 'rt-super-secret-refresh-token-xyz';
|
||||
|
||||
setConnectorCredential(vault, 'github', {
|
||||
type: 'oauth2',
|
||||
value: 'gho_access_token_123',
|
||||
refreshToken,
|
||||
expiresAt: '2099-01-01T00:00:00Z',
|
||||
scopes: ['repo', 'user'],
|
||||
});
|
||||
|
||||
expect(fs.readFileSync(path.join(dir, 'vault.json'), 'utf-8')).not.toContain(refreshToken);
|
||||
const parsed = readVaultFile(dir);
|
||||
expect(parsed['connector:github'].metadata).toEqual({
|
||||
credentialType: 'oauth2',
|
||||
expiresAt: '2099-01-01T00:00:00Z',
|
||||
scopes: ['repo', 'user'],
|
||||
});
|
||||
expect(parsed['connector:github:refresh'].encrypted).toMatch(/^[0-9a-f]+:[0-9a-f]+:[0-9a-f]+$/);
|
||||
expect(parsed['connector:github:refresh']).not.toHaveProperty('metadata');
|
||||
});
|
||||
|
||||
it('returns the decrypted refresh token', () => {
|
||||
const { vault } = openVault();
|
||||
|
||||
setConnectorCredential(vault, 'github', {
|
||||
type: 'oauth2',
|
||||
value: 'gho_access_token_456',
|
||||
refreshToken: 'rt-secret-refresh-token-abc',
|
||||
expiresAt: '2099-01-01T00:00:00Z',
|
||||
scopes: ['repo'],
|
||||
});
|
||||
|
||||
expect(getConnectorCredential(vault, 'github')).toEqual({
|
||||
value: 'gho_access_token_456',
|
||||
type: 'oauth2',
|
||||
refreshToken: 'rt-secret-refresh-token-abc',
|
||||
expiresAt: '2099-01-01T00:00:00Z',
|
||||
scopes: ['repo'],
|
||||
isExpired: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('does not create a refresh entry for a credential without one', () => {
|
||||
const { vault, dir } = openVault();
|
||||
|
||||
setConnectorCredential(vault, 'slack', { type: 'bearer', value: 'xoxb-token-123' });
|
||||
|
||||
const parsed = readVaultFile(dir);
|
||||
expect(parsed['connector:slack']).toBeDefined();
|
||||
expect(parsed['connector:slack:refresh']).toBeUndefined();
|
||||
expect(getConnectorCredential(vault, 'slack')!.refreshToken).toBeUndefined();
|
||||
});
|
||||
|
||||
it('removes the old refresh token when a credential is stored again without one', () => {
|
||||
const { vault } = openVault();
|
||||
|
||||
setConnectorCredential(vault, 'github', {
|
||||
type: 'oauth2',
|
||||
value: 'gho_token_1',
|
||||
refreshToken: 'rt-old-refresh',
|
||||
});
|
||||
expect(getConnectorCredential(vault, 'github')!.refreshToken).toBe('rt-old-refresh');
|
||||
|
||||
setConnectorCredential(vault, 'github', { type: 'oauth2', value: 'gho_token_2' });
|
||||
|
||||
const cred = getConnectorCredential(vault, 'github');
|
||||
expect(cred!.value).toBe('gho_token_2');
|
||||
expect(cred!.refreshToken).toBeUndefined();
|
||||
expect(vault.has('connector:github:refresh')).toBe(false);
|
||||
});
|
||||
|
||||
it('writes a credential bundle as one secret-store update', () => {
|
||||
const { vault, dir } = openVault();
|
||||
const writeBatch = vi.spyOn(vault, 'writeBatch');
|
||||
const set = vi.spyOn(vault, 'set');
|
||||
|
||||
setConnectorCredentialBundle(vault, 'jira', { type: 'basic', value: 'jira-token' }, {
|
||||
email: 'user@example.com',
|
||||
base_url: 'https://team.atlassian.net',
|
||||
});
|
||||
|
||||
expect(writeBatch).toHaveBeenCalledTimes(1);
|
||||
expect(set).not.toHaveBeenCalled();
|
||||
expect(getConnectorCredential(vault, 'jira')!.value).toBe('jira-token');
|
||||
expect(vault.get('connector:jira:email')!.value).toBe('user@example.com');
|
||||
expect(vault.get('connector:jira:base_url')!.value).toBe('https://team.atlassian.net');
|
||||
|
||||
const parsed = readVaultFile(dir);
|
||||
expect(parsed['connector:jira:email']).not.toHaveProperty('metadata');
|
||||
expect(new Set(Object.values(parsed).map(entry => entry.updatedAt)).size).toBe(1);
|
||||
});
|
||||
|
||||
it('rejects an invalid companion suffix before writing anything', () => {
|
||||
const { vault } = openVault();
|
||||
setConnectorCredential(vault, 'jira', { type: 'basic', value: 'old-token' });
|
||||
|
||||
expect(() => setConnectorCredentialBundle(vault, 'jira', { type: 'basic', value: 'new-token' }, {
|
||||
'Base-URL': 'https://team.atlassian.net',
|
||||
})).toThrow(TypeError);
|
||||
|
||||
expect(getConnectorCredential(vault, 'jira')!.value).toBe('old-token');
|
||||
expect(vault.list().map(entry => entry.name)).toEqual(['connector:jira']);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { ComposioConnector } from '../../src/connectors/composio-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'composio' && cred) return { ...cred, type: 'api_key' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { composio: seedCredential(cred, 'api_key') } : {} });
|
||||
}
|
||||
|
||||
describe('ComposioConnector', () => {
|
||||
@@ -66,7 +54,7 @@ describe('ComposioConnector', () => {
|
||||
it('connect() retrieves API key from vault', async () => {
|
||||
const vault = createMockVault({ value: 'cmp_test_key', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('composio');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:composio');
|
||||
});
|
||||
|
||||
it('execute returns error when not connected', async () => {
|
||||
|
||||
@@ -8,7 +8,7 @@ import { BitbucketConnector } from '../../src/connectors/bitbucket-connector.js'
|
||||
import { DropboxConnector } from '../../src/connectors/dropbox-connector.js';
|
||||
import { PostgresConnector } from '../../src/connectors/postgres-connector.js';
|
||||
import { safeFetch } from '../../src/url-egress-guard.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
vi.mock('../../src/url-egress-guard.js', () => ({
|
||||
safeFetch: vi.fn((url: string, init?: RequestInit) => globalThis.fetch(url, {
|
||||
@@ -17,23 +17,11 @@ vi.mock('../../src/url-egress-guard.js', () => ({
|
||||
})),
|
||||
}));
|
||||
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((key: string) => {
|
||||
if (extras && extras[key]) return { value: extras[key] };
|
||||
return null;
|
||||
}),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): MockSecretStore {
|
||||
return createMemorySecretStore({
|
||||
connectors: cred ? { [connectorId]: seedCredential(cred) } : {},
|
||||
secrets: extras,
|
||||
});
|
||||
}
|
||||
|
||||
// ── CRM Connectors ──────────────────────────────────────────
|
||||
|
||||
@@ -3,22 +3,10 @@ import { GmailConnector } from '../../src/connectors/gmail-connector.js';
|
||||
import { GoogleDocsConnector } from '../../src/connectors/gdocs-connector.js';
|
||||
import { GoogleDriveConnector } from '../../src/connectors/gdrive-connector.js';
|
||||
import { GoogleSheetsConnector } from '../../src/connectors/gsheets-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { [connectorId]: seedCredential(cred) } : {} });
|
||||
}
|
||||
|
||||
// ─── Gmail Connector ───
|
||||
@@ -91,7 +79,7 @@ describe('GmailConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('gmail', { value: 'ya29.test_token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gmail');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gmail');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
@@ -201,7 +189,7 @@ describe('GoogleDocsConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('gdocs', { value: 'ya29.test_token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gdocs');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gdocs');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
@@ -317,7 +305,7 @@ describe('GoogleDriveConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('gdrive', { value: 'ya29.test_token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gdrive');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gdrive');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
@@ -431,7 +419,7 @@ describe('GoogleSheetsConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('gsheets', { value: 'ya29.test_token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gsheets');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gsheets');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
|
||||
@@ -5,7 +5,7 @@ import path from 'node:path';
|
||||
import { NotionConnector } from '../../src/connectors/notion-connector.js';
|
||||
import { ConfluenceConnector } from '../../src/connectors/confluence-connector.js';
|
||||
import { ObsidianConnector } from '../../src/connectors/obsidian-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
/** Shape of ConnectorResult.data fields asserted by these knowledge-connector tests. */
|
||||
type KnowledgeData = {
|
||||
@@ -21,20 +21,11 @@ function createMockVault(
|
||||
connectorId: string,
|
||||
cred?: { value: string; isExpired: boolean },
|
||||
extras?: Record<string, { value: string }>,
|
||||
): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === connectorId && cred) return { ...cred, type: 'api_key' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((key: string) => extras?.[key] ?? null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
): MockSecretStore {
|
||||
return createMemorySecretStore({
|
||||
connectors: cred ? { [connectorId]: seedCredential(cred, 'api_key') } : {},
|
||||
secrets: Object.fromEntries(Object.entries(extras ?? {}).map(([name, entry]) => [name, entry.value])),
|
||||
});
|
||||
}
|
||||
|
||||
// ── Notion Connector ──────────────────────────────────────────────────
|
||||
@@ -85,7 +76,7 @@ describe('NotionConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault('notion', { value: 'ntn_test123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('notion');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:notion');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when API responds OK', async () => {
|
||||
@@ -193,7 +184,7 @@ describe('ConfluenceConnector', () => {
|
||||
'connector:confluence:domain': { value: 'mycompany' },
|
||||
});
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('confluence');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:confluence');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:confluence:email');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:confluence:domain');
|
||||
});
|
||||
@@ -314,7 +305,7 @@ describe('ObsidianConnector', () => {
|
||||
it('connect() retrieves vault path from vault', async () => {
|
||||
const vault = createMockVault('obsidian', { value: tmpDir, isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('obsidian');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:obsidian');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when directory exists', async () => {
|
||||
|
||||
@@ -3,25 +3,13 @@ import { MSTeamsConnector } from '../../src/connectors/ms-teams-connector.js';
|
||||
import { OutlookConnector } from '../../src/connectors/outlook-connector.js';
|
||||
import { OneDriveConnector } from '../../src/connectors/onedrive-connector.js';
|
||||
import { OneNoteConnector } from '../../src/connectors/onenote-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(
|
||||
connectorId: string,
|
||||
cred?: { value: string; isExpired: boolean },
|
||||
): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { [connectorId]: seedCredential(cred) } : {} });
|
||||
}
|
||||
|
||||
// ── Microsoft Teams Connector ─────────────────────────────────────────
|
||||
@@ -79,7 +67,7 @@ describe('MSTeamsConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault('ms-teams', { value: 'graph-token-123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('ms-teams');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:ms-teams');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when Graph API responds OK', async () => {
|
||||
@@ -209,7 +197,7 @@ describe('OutlookConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault('outlook', { value: 'graph-token-456', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('outlook');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:outlook');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when Graph API responds OK', async () => {
|
||||
@@ -368,7 +356,7 @@ describe('OneDriveConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault('onedrive', { value: 'graph-token-789', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('onedrive');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:onedrive');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when Graph API responds OK', async () => {
|
||||
@@ -562,7 +550,7 @@ describe('OneNoteConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault('onenote', { value: 'graph-token-onenote', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('onenote');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:onenote');
|
||||
});
|
||||
|
||||
it('healthCheck() probes /me/onenote/notebooks to exercise the Notes.Read scope', async () => {
|
||||
|
||||
@@ -3,25 +3,13 @@ import { LinearConnector } from '../../src/connectors/linear-connector.js';
|
||||
import { AsanaConnector } from '../../src/connectors/asana-connector.js';
|
||||
import { TrelloConnector } from '../../src/connectors/trello-connector.js';
|
||||
import { MondayConnector } from '../../src/connectors/monday-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === connectorId && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((key: string) => {
|
||||
if (extras && extras[key]) return { value: extras[key] };
|
||||
return null;
|
||||
}),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(connectorId: string, cred?: { value: string; isExpired: boolean }, extras?: Record<string, string>): MockSecretStore {
|
||||
return createMemorySecretStore({
|
||||
connectors: cred ? { [connectorId]: seedCredential(cred) } : {},
|
||||
secrets: extras,
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Linear Connector ───
|
||||
@@ -81,7 +69,7 @@ describe('LinearConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('linear', { value: 'lin_api_test123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('linear');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:linear');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
@@ -239,7 +227,7 @@ describe('AsanaConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('asana', { value: 'asana_pat_test123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('asana');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:asana');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
@@ -341,7 +329,7 @@ describe('TrelloConnector', () => {
|
||||
'connector:trello:api_key': 'trello_key_test',
|
||||
});
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('trello');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:trello');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:trello:api_key');
|
||||
});
|
||||
|
||||
@@ -453,7 +441,7 @@ describe('MondayConnector', () => {
|
||||
it('connect retrieves token from vault', async () => {
|
||||
const vault = createMockVault('monday', { value: 'monday_api_test123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('monday');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:monday');
|
||||
});
|
||||
|
||||
it('healthCheck returns connected when API responds OK', async () => {
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { DiscordConnector } from '../../src/connectors/discord-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'discord' && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { discord: seedCredential(cred) } : {} });
|
||||
}
|
||||
|
||||
describe('DiscordConnector', () => {
|
||||
@@ -50,7 +38,7 @@ describe('DiscordConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault({ value: 'bot-test-token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('discord');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:discord');
|
||||
});
|
||||
|
||||
it('execute() returns error when not connected', async () => {
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { EmailConnector } from '../../src/connectors/email-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(cred?: { value: string }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'email' && cred) return { ...cred, type: 'api_key', isExpired: false };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(cred?: { value: string }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { email: { type: 'api_key', value: cred.value } } : {} });
|
||||
}
|
||||
|
||||
describe('EmailConnector', () => {
|
||||
@@ -41,7 +29,7 @@ describe('EmailConnector', () => {
|
||||
it('connect() retrieves API key from vault', async () => {
|
||||
const vault = createMockVault({ value: 'SG.test_key' });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('email');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:email');
|
||||
});
|
||||
|
||||
it('healthCheck() validates API key', async () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { GoogleCalendarConnector } from '../../src/connectors/gcal-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { getConnectorCredential } from '../../src/connectors/connector-credentials.js';
|
||||
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(opts?: {
|
||||
accessToken?: string;
|
||||
@@ -8,32 +9,23 @@ function createMockVault(opts?: {
|
||||
expiresAt?: string;
|
||||
clientId?: string;
|
||||
clientSecret?: string;
|
||||
}): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'gcal' && opts?.accessToken) {
|
||||
return {
|
||||
value: opts.accessToken,
|
||||
}): MockSecretStore {
|
||||
const secrets: Record<string, string> = {};
|
||||
if (opts?.clientId) secrets['connector:gcal:client_id'] = opts.clientId;
|
||||
if (opts?.clientSecret) secrets['connector:gcal:client_secret'] = opts.clientSecret;
|
||||
return createMemorySecretStore({
|
||||
connectors: opts?.accessToken
|
||||
? {
|
||||
gcal: {
|
||||
type: 'oauth2',
|
||||
isExpired: false,
|
||||
value: opts.accessToken,
|
||||
refreshToken: opts.refreshToken,
|
||||
expiresAt: opts.expiresAt,
|
||||
};
|
||||
},
|
||||
}
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((name: string) => {
|
||||
if (name === 'connector:gcal:client_id' && opts?.clientId) return { value: opts.clientId };
|
||||
if (name === 'connector:gcal:client_secret' && opts?.clientSecret) return { value: opts.clientSecret };
|
||||
return null;
|
||||
}),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
: {},
|
||||
secrets,
|
||||
});
|
||||
}
|
||||
|
||||
describe('GoogleCalendarConnector', () => {
|
||||
@@ -63,7 +55,7 @@ describe('GoogleCalendarConnector', () => {
|
||||
expiresAt: new Date(Date.now() + 3600000).toISOString(),
|
||||
});
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('gcal');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:gcal');
|
||||
});
|
||||
|
||||
it('healthCheck() validates access token', async () => {
|
||||
@@ -104,10 +96,10 @@ describe('GoogleCalendarConnector', () => {
|
||||
const health = await connector.healthCheck();
|
||||
expect(health.status).toBe('connected');
|
||||
// Verify tokens were stored back in vault
|
||||
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
|
||||
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
|
||||
type: 'oauth2',
|
||||
value: 'ya29.refreshed',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
it('execute(list_events) returns events', async () => {
|
||||
@@ -205,10 +197,10 @@ describe('OAuth2 token refresh', () => {
|
||||
|
||||
await connector.execute('list_events', {});
|
||||
|
||||
expect(vault.setConnectorCredential).toHaveBeenCalledWith('gcal', expect.objectContaining({
|
||||
expect(getConnectorCredential(vault, 'gcal')).toMatchObject({
|
||||
value: 'ya29.new',
|
||||
refreshToken: 'rt_new',
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
it('returns error when refresh token is invalid', async () => {
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { GitHubConnector } from '../../src/connectors/github-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'github' && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { github: seedCredential(cred) } : {} });
|
||||
}
|
||||
|
||||
describe('GitHubConnector', () => {
|
||||
@@ -41,7 +29,7 @@ describe('GitHubConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault({ value: 'ghp_test123', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('github');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:github');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when API responds OK', async () => {
|
||||
|
||||
@@ -1,25 +1,15 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { JiraConnector } from '../../src/connectors/jira-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(opts?: { token?: string; email?: string; baseUrl?: string }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'jira' && opts?.token) return { value: opts.token, type: 'bearer', isExpired: false };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn((name: string) => {
|
||||
if (name === 'connector:jira:email' && opts?.email) return { value: opts.email };
|
||||
if (name === 'connector:jira:base_url' && opts?.baseUrl) return { value: opts.baseUrl };
|
||||
return null;
|
||||
}),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(opts?: { token?: string; email?: string; baseUrl?: string }): MockSecretStore {
|
||||
const secrets: Record<string, string> = {};
|
||||
if (opts?.email) secrets['connector:jira:email'] = opts.email;
|
||||
if (opts?.baseUrl) secrets['connector:jira:base_url'] = opts.baseUrl;
|
||||
return createMemorySecretStore({
|
||||
connectors: opts?.token ? { jira: { type: 'bearer', value: opts.token } } : {},
|
||||
secrets,
|
||||
});
|
||||
}
|
||||
|
||||
describe('JiraConnector', () => {
|
||||
@@ -48,7 +38,7 @@ describe('JiraConnector', () => {
|
||||
baseUrl: 'https://mycompany.atlassian.net',
|
||||
});
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('jira');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:jira');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:jira:email');
|
||||
});
|
||||
|
||||
|
||||
@@ -1,21 +1,9 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { SlackConnector } from '../../src/connectors/slack-connector.js';
|
||||
import type { VaultStore } from '@waggle/core';
|
||||
import { createMemorySecretStore, seedCredential, type MockSecretStore } from '../helpers/memory-secret-store.js';
|
||||
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): VaultStore {
|
||||
return {
|
||||
getConnectorCredential: vi.fn((id: string) => {
|
||||
if (id === 'slack' && cred) return { ...cred, type: 'bearer' };
|
||||
return null;
|
||||
}),
|
||||
get: vi.fn(() => null),
|
||||
set: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
list: vi.fn(() => []),
|
||||
has: vi.fn(() => false),
|
||||
setConnectorCredential: vi.fn(),
|
||||
migrateFromConfig: vi.fn(() => 0),
|
||||
} as unknown as VaultStore;
|
||||
function createMockVault(cred?: { value: string; isExpired: boolean }): MockSecretStore {
|
||||
return createMemorySecretStore({ connectors: cred ? { slack: seedCredential(cred) } : {} });
|
||||
}
|
||||
|
||||
describe('SlackConnector', () => {
|
||||
@@ -41,7 +29,7 @@ describe('SlackConnector', () => {
|
||||
it('connect() retrieves token from vault', async () => {
|
||||
const vault = createMockVault({ value: 'xoxb-test-token', isExpired: false });
|
||||
await connector.connect(vault);
|
||||
expect(vault.getConnectorCredential).toHaveBeenCalledWith('slack');
|
||||
expect(vault.get).toHaveBeenCalledWith('connector:slack');
|
||||
});
|
||||
|
||||
it('healthCheck() returns connected when auth.test succeeds', async () => {
|
||||
|
||||
Reference in New Issue
Block a user