Update main.ts to automatically generate API keys on first load, encrypt them when saving to disk, and decrypt them when loading. Also migrate legacy settings by removing enableCORS and allowedOrigins fields. Changes: - Auto-generate API key if empty on plugin load - Encrypt API key before saving to data.json - Decrypt API key after loading from data.json - Migrate legacy settings by removing CORS-related fields - Add imports for generateApiKey, encryptApiKey, decryptApiKey - Add comprehensive migration tests in main-migration.test.ts This implements Task 4 of the CORS simplification plan.
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
import { generateApiKey } from '../src/utils/auth-utils';
|
|
import { encryptApiKey, decryptApiKey } from '../src/utils/encryption-utils';
|
|
import { DEFAULT_SETTINGS } from '../src/types/settings-types';
|
|
|
|
// Mock electron
|
|
jest.mock('electron', () => ({
|
|
safeStorage: {
|
|
isEncryptionAvailable: jest.fn(() => true),
|
|
encryptString: jest.fn((data: string) => Buffer.from(`encrypted:${data}`)),
|
|
decryptString: jest.fn((buffer: Buffer) => {
|
|
const str = buffer.toString();
|
|
return str.replace('encrypted:', '');
|
|
})
|
|
}
|
|
}));
|
|
|
|
describe('Settings Migration', () => {
|
|
describe('API key initialization', () => {
|
|
it('should generate API key if empty', () => {
|
|
const settings = { ...DEFAULT_SETTINGS, apiKey: '' };
|
|
|
|
// Simulate what plugin should do
|
|
if (!settings.apiKey) {
|
|
settings.apiKey = generateApiKey();
|
|
}
|
|
|
|
expect(settings.apiKey).toBeTruthy();
|
|
expect(settings.apiKey.length).toBeGreaterThanOrEqual(32);
|
|
});
|
|
|
|
it('should encrypt API key on save', () => {
|
|
const plainKey = generateApiKey();
|
|
const encrypted = encryptApiKey(plainKey);
|
|
|
|
expect(encrypted).toMatch(/^encrypted:/);
|
|
expect(encrypted).not.toBe(plainKey);
|
|
});
|
|
|
|
it('should decrypt API key on load', () => {
|
|
const plainKey = generateApiKey();
|
|
const encrypted = encryptApiKey(plainKey);
|
|
const decrypted = decryptApiKey(encrypted);
|
|
|
|
expect(decrypted).toBe(plainKey);
|
|
});
|
|
});
|
|
|
|
describe('Legacy settings migration', () => {
|
|
it('should remove enableCORS from legacy settings', () => {
|
|
const legacySettings: any = {
|
|
...DEFAULT_SETTINGS,
|
|
enableCORS: true,
|
|
allowedOrigins: ['*']
|
|
};
|
|
|
|
// Simulate migration
|
|
delete legacySettings.enableCORS;
|
|
delete legacySettings.allowedOrigins;
|
|
|
|
expect(legacySettings.enableCORS).toBeUndefined();
|
|
expect(legacySettings.allowedOrigins).toBeUndefined();
|
|
});
|
|
|
|
it('should preserve other settings during migration', () => {
|
|
const legacySettings: any = {
|
|
...DEFAULT_SETTINGS,
|
|
port: 4000,
|
|
enableCORS: false,
|
|
allowedOrigins: ['http://localhost:8080'],
|
|
notificationsEnabled: true
|
|
};
|
|
|
|
// Simulate migration
|
|
const { enableCORS, allowedOrigins, ...migrated } = legacySettings;
|
|
|
|
expect(migrated.port).toBe(4000);
|
|
expect(migrated.notificationsEnabled).toBe(true);
|
|
});
|
|
});
|
|
});
|