Import tests: backend/tests/integration/config-validator
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { spawnSync } from 'child_process';
|
||||
import path from 'path';
|
||||
|
||||
describe('Config Validator & Invalid Connections', () => {
|
||||
const backendDir = path.resolve(__dirname, '../../');
|
||||
|
||||
it('should fail validation when DATABASE_URL is missing', () => {
|
||||
const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], {
|
||||
cwd: backendDir,
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: '',
|
||||
JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345',
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toBe(1);
|
||||
const stderr = res.stderr.toString();
|
||||
expect(stderr).toContain('Invalid environment configuration');
|
||||
expect(stderr).toContain('DATABASE_URL');
|
||||
});
|
||||
|
||||
it('should fail validation when DATABASE_URL is not a valid URL', () => {
|
||||
const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], {
|
||||
cwd: backendDir,
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: 'invalid-url-format',
|
||||
JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345',
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toBe(1);
|
||||
const stderr = res.stderr.toString();
|
||||
expect(stderr).toContain('Invalid environment configuration');
|
||||
expect(stderr).toContain('DATABASE_URL');
|
||||
});
|
||||
|
||||
it('should fail validation when JWT_SECRET is too short', () => {
|
||||
const res = spawnSync('npx', ['tsx', 'src/config/env.ts'], {
|
||||
cwd: backendDir,
|
||||
env: {
|
||||
...process.env,
|
||||
DATABASE_URL: 'postgresql://coaxys_panel:coaxys_pass@127.0.0.1:5432/coaxys_panel?schema=public',
|
||||
JWT_SECRET: 'short',
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.status).toBe(1);
|
||||
const stderr = res.stderr.toString();
|
||||
expect(stderr).toContain('Invalid environment configuration');
|
||||
expect(stderr).toContain('JWT_SECRET');
|
||||
});
|
||||
|
||||
it('should propagate connection error when using an invalid database port', () => {
|
||||
// Attempt to start index.ts with an invalid DB port, which should cause connection to fail on startup.
|
||||
// We set PORT to a random high port so it doesn't conflict, and invalid database port with connection timeout.
|
||||
const res = spawnSync('npx', ['tsx', 'src/index.ts'], {
|
||||
cwd: backendDir,
|
||||
env: {
|
||||
...process.env,
|
||||
PORT: '14002',
|
||||
DATABASE_URL: 'postgresql://coaxys_panel:coaxys_pass@127.0.0.1:9999/coaxys_panel?schema=public&connect_timeout=2',
|
||||
JWT_SECRET: 'supersecretjwtkeyforcoaxyspanelbackendproject12345',
|
||||
},
|
||||
timeout: 10000, // Timeout after 10s if it hangs
|
||||
});
|
||||
|
||||
// It should exit with 1 because the database connection fails on startup
|
||||
expect(res.status).toBe(1);
|
||||
const stderr = res.stderr.toString() || res.stdout.toString();
|
||||
// Verify connection error details are propagated
|
||||
expect(stderr).toContain('Can\'t reach database server at');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user