fix(core): Create instance settings directory recursively (no-changelog) (#7506)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-10-24 15:10:22 +02:00 committed by GitHub
parent a739245332
commit 8ff9f97493
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 14 deletions

View file

@ -63,10 +63,11 @@ export class InstanceSettings {
errorMessage: `Error parsing n8n-config file "${settingsFile}". It does not seem to be valid JSON.`, errorMessage: `Error parsing n8n-config file "${settingsFile}". It does not seem to be valid JSON.`,
}); });
} else { } else {
// Ensure that the `.n8n` folder exists
mkdirSync(this.n8nFolder, { recursive: true });
// If file doesn't exist, create new settings // If file doesn't exist, create new settings
const encryptionKey = process.env.N8N_ENCRYPTION_KEY ?? randomBytes(24).toString('base64'); const encryptionKey = process.env.N8N_ENCRYPTION_KEY ?? randomBytes(24).toString('base64');
settings = { encryptionKey }; settings = { encryptionKey };
mkdirSync(path.dirname(settingsFile));
this.save(settings); this.save(settings);
// console.info(`UserSettings were generated and saved to: ${settingsFile}`); // console.info(`UserSettings were generated and saved to: ${settingsFile}`);
} }

View file

@ -4,13 +4,15 @@ import { InstanceSettings } from '@/InstanceSettings';
describe('InstanceSettings', () => { describe('InstanceSettings', () => {
process.env.N8N_USER_FOLDER = '/test'; process.env.N8N_USER_FOLDER = '/test';
const existSpy = jest.spyOn(fs, 'existsSync');
beforeEach(() => jest.resetAllMocks());
describe('If the settings file exists', () => { describe('If the settings file exists', () => {
beforeEach(() => { const readSpy = jest.spyOn(fs, 'readFileSync');
jest.spyOn(fs, 'existsSync').mockReturnValue(true); beforeEach(() => existSpy.mockReturnValue(true));
});
it('should load settings from the file', () => { it('should load settings from the file', () => {
jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify({ encryptionKey: 'test_key' })); readSpy.mockReturnValue(JSON.stringify({ encryptionKey: 'test_key' }));
const settings = new InstanceSettings(); const settings = new InstanceSettings();
expect(settings.encryptionKey).toEqual('test_key'); expect(settings.encryptionKey).toEqual('test_key');
expect(settings.instanceId).toEqual( expect(settings.instanceId).toEqual(
@ -19,19 +21,24 @@ describe('InstanceSettings', () => {
}); });
it('should throw error if settings file is not valid JSON', () => { it('should throw error if settings file is not valid JSON', () => {
jest.spyOn(fs, 'readFileSync').mockReturnValue('{"encryptionKey":"test_key"'); readSpy.mockReturnValue('{"encryptionKey":"test_key"');
expect(() => new InstanceSettings()).toThrowError(); expect(() => new InstanceSettings()).toThrowError();
}); });
}); });
describe('If the settings file does not exist', () => { describe('If the settings file does not exist', () => {
const mkdirSpy = jest.spyOn(fs, 'mkdirSync');
const writeFileSpy = jest.spyOn(fs, 'writeFileSync');
beforeEach(() => {
existSpy.mockReturnValue(false);
mkdirSpy.mockReturnValue('');
writeFileSpy.mockReturnValue();
});
it('should create a new settings file', () => { it('should create a new settings file', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockReturnValue('');
const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockReturnValue();
const settings = new InstanceSettings(); const settings = new InstanceSettings();
expect(settings.encryptionKey).not.toEqual('test_key'); expect(settings.encryptionKey).not.toEqual('test_key');
expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n'); expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n', { recursive: true });
expect(writeFileSpy).toHaveBeenCalledWith( expect(writeFileSpy).toHaveBeenCalledWith(
'/test/.n8n/config', '/test/.n8n/config',
expect.stringContaining('"encryptionKey":'), expect.stringContaining('"encryptionKey":'),
@ -41,16 +48,13 @@ describe('InstanceSettings', () => {
it('should pick up the encryption key from env var N8N_ENCRYPTION_KEY', () => { it('should pick up the encryption key from env var N8N_ENCRYPTION_KEY', () => {
process.env.N8N_ENCRYPTION_KEY = 'env_key'; process.env.N8N_ENCRYPTION_KEY = 'env_key';
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockReturnValue('');
const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockReturnValue();
const settings = new InstanceSettings(); const settings = new InstanceSettings();
expect(settings.encryptionKey).toEqual('env_key'); expect(settings.encryptionKey).toEqual('env_key');
expect(settings.instanceId).toEqual( expect(settings.instanceId).toEqual(
'2c70e12b7a0646f92279f427c7b38e7334d8e5389cff167a1dc30e73f826b683', '2c70e12b7a0646f92279f427c7b38e7334d8e5389cff167a1dc30e73f826b683',
); );
expect(settings.encryptionKey).not.toEqual('test_key'); expect(settings.encryptionKey).not.toEqual('test_key');
expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n'); expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n', { recursive: true });
expect(writeFileSpy).toHaveBeenCalledWith( expect(writeFileSpy).toHaveBeenCalledWith(
'/test/.n8n/config', '/test/.n8n/config',
expect.stringContaining('"encryptionKey":'), expect.stringContaining('"encryptionKey":'),