63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { parseEnvironment } from '../src/config/environment';
|
|
|
|
const valid = { DATABASE_URL: 'postgresql://user:secret@localhost:5432/mani' };
|
|
|
|
describe('environment configuration', () => {
|
|
it('applies defaults and parses explicit configuration', () => {
|
|
expect(parseEnvironment(valid)).toMatchObject({
|
|
PORT: 3000,
|
|
CORS_ORIGINS: [],
|
|
});
|
|
expect(
|
|
parseEnvironment({
|
|
...valid,
|
|
PORT: '4000',
|
|
CORS_ORIGINS: ' https://shop.example.com ',
|
|
}),
|
|
).toMatchObject({ PORT: 4000, CORS_ORIGINS: ['https://shop.example.com'] });
|
|
});
|
|
|
|
it.each([
|
|
{ DATABASE_URL: undefined },
|
|
{ DATABASE_URL: 'https://example.com' },
|
|
{ PORT: '0' },
|
|
{ PORT: '65536' },
|
|
{ PORT: '1.5' },
|
|
{ PORT: 'invalid' },
|
|
{ NODE_ENV: 'staging' },
|
|
{ CORS_ORIGINS: '*' },
|
|
{ CORS_ORIGINS: 'https://shop.example.com/path' },
|
|
{ TRUST_PROXY: 'true' },
|
|
{ TRUST_PROXY: '*' },
|
|
{ TRUST_PROXY: '2' },
|
|
{ TRUST_PROXY: '10.0.0.0/33' },
|
|
{ TRUST_PROXY: '10.0.0.1/8/1' },
|
|
{ TRUST_PROXY: 'proxy.example.com' },
|
|
])('rejects invalid configuration %j', (override) => {
|
|
expect(() => parseEnvironment({ ...valid, ...override })).toThrow(
|
|
'Invalid environment',
|
|
);
|
|
});
|
|
|
|
it('parses trusted proxy addresses, subnets and named ranges', () => {
|
|
expect(parseEnvironment(valid).TRUST_PROXY).toEqual([]);
|
|
expect(
|
|
parseEnvironment({
|
|
...valid,
|
|
TRUST_PROXY: ' loopback, 172.17.0.0/16 ,203.0.113.7,::1/128',
|
|
}).TRUST_PROXY,
|
|
).toEqual(['loopback', '172.17.0.0/16', '203.0.113.7', '::1/128']);
|
|
});
|
|
|
|
it('does not leak credentials in validation errors', () => {
|
|
expect(() => parseEnvironment({ DATABASE_URL: 'secret-password' })).toThrow(
|
|
'DATABASE_URL',
|
|
);
|
|
try {
|
|
parseEnvironment({ DATABASE_URL: 'secret-password' });
|
|
} catch (error) {
|
|
expect(String(error)).not.toContain('secret-password');
|
|
}
|
|
});
|
|
});
|