Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
87 lines
2.2 KiB
TypeScript
87 lines
2.2 KiB
TypeScript
import { Container } from '@n8n/di';
|
|
import fs from 'fs';
|
|
|
|
import { Config, Env } from '../src/decorators';
|
|
|
|
jest.mock('fs');
|
|
const mockFs = jest.mocked(fs);
|
|
|
|
describe('decorators', () => {
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
Container.reset();
|
|
process.env = {};
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
afterEach(() => {
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
it('should throw when explicit typing is missing', () => {
|
|
expect(() => {
|
|
@Config
|
|
class InvalidConfig {
|
|
@Env('STRING_VALUE')
|
|
value = 'string';
|
|
}
|
|
Container.get(InvalidConfig);
|
|
}).toThrowError(
|
|
'Invalid decorator metadata on key "value" on InvalidConfig\n Please use explicit typing on all config fields',
|
|
);
|
|
});
|
|
|
|
it('should read value from _FILE env variable', () => {
|
|
const filePath = '/path/to/secret';
|
|
process.env.TEST_VALUE_FILE = filePath;
|
|
mockFs.readFileSync.mockReturnValueOnce('secret-value');
|
|
|
|
@Config
|
|
class TestConfig {
|
|
@Env('TEST_VALUE')
|
|
value: string = 'default';
|
|
}
|
|
|
|
const config = Container.get(TestConfig);
|
|
expect(config.value).toBe('secret-value');
|
|
expect(mockFs.readFileSync).toHaveBeenCalledWith(filePath, 'utf8');
|
|
});
|
|
|
|
it('should warn when _FILE env variable value contains whitespace', () => {
|
|
const filePath = '/path/to/secret';
|
|
process.env.TEST_VALUE_FILE = filePath;
|
|
mockFs.readFileSync.mockReturnValueOnce('secret-value\n');
|
|
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation();
|
|
|
|
@Config
|
|
class TestConfig {
|
|
@Env('TEST_VALUE')
|
|
value: string = 'default';
|
|
}
|
|
|
|
const config = Container.get(TestConfig);
|
|
expect(config.value).toBe('secret-value');
|
|
expect(consoleWarnSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining('TEST_VALUE_FILE contains leading or trailing whitespace'),
|
|
);
|
|
consoleWarnSpy.mockRestore();
|
|
});
|
|
|
|
it('should prefer direct env variable over _FILE variant', () => {
|
|
const filePath = '/path/to/secret';
|
|
process.env.TEST_VALUE = 'direct-value';
|
|
process.env.TEST_VALUE_FILE = filePath;
|
|
|
|
@Config
|
|
class TestConfig {
|
|
@Env('TEST_VALUE')
|
|
value: string = 'default';
|
|
}
|
|
|
|
const config = Container.get(TestConfig);
|
|
expect(config.value).toBe('direct-value');
|
|
expect(mockFs.readFileSync).not.toHaveBeenCalled();
|
|
});
|
|
});
|