manicanldes-backend/test/environment.spec.ts

47 lines
1.4 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' },
])('rejects invalid configuration %j', (override) => {
expect(() => parseEnvironment({ ...valid, ...override })).toThrow(
'Invalid environment',
);
});
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');
}
});
});