|
| 1 | +import fs from 'fs'; |
| 2 | +import { InstanceSettings } from '@/InstanceSettings'; |
| 3 | + |
| 4 | +describe('InstanceSettings', () => { |
| 5 | + process.env.N8N_USER_FOLDER = '/test'; |
| 6 | + |
| 7 | + describe('If the settings file exists', () => { |
| 8 | + beforeEach(() => { |
| 9 | + jest.spyOn(fs, 'existsSync').mockReturnValue(true); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should load settings from the file', () => { |
| 13 | + jest.spyOn(fs, 'readFileSync').mockReturnValue(JSON.stringify({ encryptionKey: 'test_key' })); |
| 14 | + const settings = new InstanceSettings(); |
| 15 | + expect(settings.encryptionKey).toEqual('test_key'); |
| 16 | + expect(settings.instanceId).toEqual( |
| 17 | + '6ce26c63596f0cc4323563c529acfca0cccb0e57f6533d79a60a42c9ff862ae7', |
| 18 | + ); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should throw error if settings file is not valid JSON', () => { |
| 22 | + jest.spyOn(fs, 'readFileSync').mockReturnValue('{"encryptionKey":"test_key"'); |
| 23 | + expect(() => new InstanceSettings()).toThrowError(); |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + describe('If the settings file does not exist', () => { |
| 28 | + it('should create a new settings file', () => { |
| 29 | + jest.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 30 | + const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockReturnValue(''); |
| 31 | + const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockReturnValue(); |
| 32 | + const settings = new InstanceSettings(); |
| 33 | + expect(settings.encryptionKey).not.toEqual('test_key'); |
| 34 | + expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n'); |
| 35 | + expect(writeFileSpy).toHaveBeenCalledWith( |
| 36 | + '/test/.n8n/config', |
| 37 | + expect.stringContaining('"encryptionKey":'), |
| 38 | + 'utf-8', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should pick up the encryption key from env var N8N_ENCRYPTION_KEY', () => { |
| 43 | + process.env.N8N_ENCRYPTION_KEY = 'env_key'; |
| 44 | + jest.spyOn(fs, 'existsSync').mockReturnValue(false); |
| 45 | + const mkdirSpy = jest.spyOn(fs, 'mkdirSync').mockReturnValue(''); |
| 46 | + const writeFileSpy = jest.spyOn(fs, 'writeFileSync').mockReturnValue(); |
| 47 | + const settings = new InstanceSettings(); |
| 48 | + expect(settings.encryptionKey).toEqual('env_key'); |
| 49 | + expect(settings.instanceId).toEqual( |
| 50 | + '2c70e12b7a0646f92279f427c7b38e7334d8e5389cff167a1dc30e73f826b683', |
| 51 | + ); |
| 52 | + expect(settings.encryptionKey).not.toEqual('test_key'); |
| 53 | + expect(mkdirSpy).toHaveBeenCalledWith('/test/.n8n'); |
| 54 | + expect(writeFileSpy).toHaveBeenCalledWith( |
| 55 | + '/test/.n8n/config', |
| 56 | + expect.stringContaining('"encryptionKey":'), |
| 57 | + 'utf-8', |
| 58 | + ); |
| 59 | + }); |
| 60 | + }); |
| 61 | +}); |
0 commit comments