fix: harden profile lifecycle validation and dashboard UX flows

This commit is contained in:
Tam Nhu Tran committed 2026-03-05 18:49:16 +07:00
1 parent 02c81743a0
commit d6fc5dd64c
8 files changed
+184 -36

No files matched your search

@@ -6,6 +6,7 @@ import {
copyApiProfile,
discoverApiProfileOrphans,
exportApiProfile,
importApiProfileBundle,
registerApiProfileOrphans,
} from '../../../src/api/services/profile-lifecycle-service';
@@ -113,5 +114,50 @@ describe('profile lifecycle service', () => {
expect(result.success).toBe(false);
expect(result.error).toContain('Invalid source profile name');
});
});
it('rejects import bundle with invalid profile target', () => {
const result = importApiProfileBundle({
schemaVersion: 1,
exportedAt: new Date().toISOString(),
profile: { name: 'glm', target: 'invalid-target' },
settings: {
env: {
ANTHROPIC_BASE_URL: 'https://api.example.com',
ANTHROPIC_AUTH_TOKEN: 'token',
},
},
});
expect(result.success).toBe(false);
expect(result.error).toContain('Invalid bundle profile target');
});
it('clears and warns for all redacted sensitive env keys on import', () => {
const ccsDir = path.join(tempHome, '.ccs');
fs.mkdirSync(ccsDir, { recursive: true });
fs.writeFileSync(path.join(ccsDir, 'config.json'), JSON.stringify({ profiles: {} }, null, 2) + '\n');
const result = importApiProfileBundle({
schemaVersion: 1,
exportedAt: new Date().toISOString(),
profile: { name: 'redacted-import', target: 'claude' },
settings: {
env: {
ANTHROPIC_BASE_URL: 'https://api.example.com',
ANTHROPIC_AUTH_TOKEN: '__CCS_REDACTED__',
OPENROUTER_API_KEY: '__CCS_REDACTED__',
},
},
});
expect(result.success).toBe(true);
expect(result.warnings?.length).toBeGreaterThan(0);
const settingsPath = path.join(ccsDir, 'redacted-import.settings.json');
const parsed = JSON.parse(fs.readFileSync(settingsPath, 'utf8')) as {
env: Record<string, string>;
};
expect(parsed.env.ANTHROPIC_AUTH_TOKEN).toBe('');
expect(parsed.env.OPENROUTER_API_KEY).toBe('');
});
});
@@ -95,6 +95,42 @@ describe('profile-routes lifecycle endpoints', () => {
expect(body.skipped).toEqual([]);
});
it('rejects malformed names payload for orphan registration', async () => {
const response = await fetch(`${baseUrl}/api/profiles/orphans/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ names: 'lonely' }),
});
expect(response.status).toBe(400);
const body = (await response.json()) as { error: string };
expect(body.error).toContain('names must be an array');
});
it('rejects import bundle with invalid profile target', async () => {
const response = await fetch(`${baseUrl}/api/profiles/import`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
bundle: {
schemaVersion: 1,
exportedAt: new Date().toISOString(),
profile: { name: 'demo', target: 'invalid' },
settings: {
env: {
ANTHROPIC_BASE_URL: 'https://api.example.com',
ANTHROPIC_AUTH_TOKEN: 'token',
},
},
},
}),
});
expect(response.status).toBe(400);
const body = (await response.json()) as { error: string };
expect(body.error).toContain('Invalid bundle profile target');
});
it('validates source profile name on export endpoint', async () => {
const response = await fetch(`${baseUrl}/api/profiles/1invalid/export`, {
method: 'POST',
@@ -107,4 +143,3 @@ describe('profile-routes lifecycle endpoints', () => {
expect(body.error).toContain('API name must start with letter');
});
});