n8n/packages/workflow/test/WorkflowDataProxyEnvProvider.test.ts
Tomi Turtiainen e94cda3837
Some checks are pending
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Waiting to run
feat: Add support for $env in the js task runner (no-changelog) (#11177)
2024-10-09 17:31:45 +03:00

88 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { ExpressionError } from '../src/errors/expression.error';
import { createEnvProvider, createEnvProviderState } from '../src/WorkflowDataProxyEnvProvider';
describe('createEnvProviderState', () => {
afterEach(() => {
delete process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE;
});
it('should return the state with process available and env access allowed', () => {
expect(createEnvProviderState()).toEqual({
isProcessAvailable: true,
isEnvAccessBlocked: false,
env: process.env,
});
});
it('should block env access when N8N_BLOCK_ENV_ACCESS_IN_NODE is set to "true"', () => {
process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE = 'true';
expect(createEnvProviderState()).toEqual({
isProcessAvailable: true,
isEnvAccessBlocked: true,
env: {},
});
});
it('should handle process not being available', () => {
const originalProcess = global.process;
try {
// @ts-expect-error process is read-only
global.process = undefined;
expect(createEnvProviderState()).toEqual({
isProcessAvailable: false,
isEnvAccessBlocked: false,
env: {},
});
} finally {
global.process = originalProcess;
}
});
});
describe('createEnvProvider', () => {
it('should return true when checking for a property using "has"', () => {
const proxy = createEnvProvider(0, 0, createEnvProviderState());
expect('someProperty' in proxy).toBe(true);
});
it('should return the value from process.env if access is allowed', () => {
process.env.TEST_ENV_VAR = 'test_value';
const proxy = createEnvProvider(0, 0, createEnvProviderState());
expect(proxy.TEST_ENV_VAR).toBe('test_value');
});
it('should throw ExpressionError when process is unavailable', () => {
const originalProcess = global.process;
// @ts-expect-error process is read-only
global.process = undefined;
try {
const proxy = createEnvProvider(1, 1, createEnvProviderState());
expect(() => proxy.someEnvVar).toThrowError(
new ExpressionError('not accessible via UI, please run node', {
runIndex: 1,
itemIndex: 1,
}),
);
} finally {
global.process = originalProcess;
}
});
it('should throw ExpressionError when env access is blocked', () => {
process.env.N8N_BLOCK_ENV_ACCESS_IN_NODE = 'true';
const proxy = createEnvProvider(1, 1, createEnvProviderState());
expect(() => proxy.someEnvVar).toThrowError(
new ExpressionError('access to env vars denied', {
causeDetailed:
'If you need access please contact the administrator to remove the environment variable N8N_BLOCK_ENV_ACCESS_IN_NODE',
runIndex: 1,
itemIndex: 1,
}),
);
});
});