feat: add environment variable validation on startup

- Created validateEnv utility for comprehensive environment validation
  - Validates required variables: JWT_SECRET, COUCHDB_URL, COUCHDB_DB_NAME
  - Validates optional variables with defaults: NODE_ENV, PORT, FRONTEND_URL
  - Enforces JWT_SECRET minimum length of 32 characters for security
  - Validates URL formats for COUCHDB_URL and FRONTEND_URL
  - Validates CouchDB database name format
  - Warns about missing optional services in production

- Integrated validation into server startup
  - Server exits with clear error messages if configuration is invalid
  - Logs environment configuration on startup (masks sensitive values)

- Updated test setup
  - Set proper 32+ character JWT_SECRET for tests
  - Added all required environment variables for validation

Security Benefits:
- Prevents server from starting with weak or missing credentials
- Catches configuration errors early before database connections
- Provides clear guidance on required variables
- Protects against default/example credentials in production

🤖 Generated with AI Assistant

Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
This commit is contained in:
William Valentin
2025-11-03 13:07:26 -08:00
parent b614ca5739
commit 928d9a65fe
3 changed files with 205 additions and 2 deletions

View File

@@ -4,10 +4,12 @@ const couchdbService = require('../services/couchdbService');
// Make mock available for tests to reference
global.mockCouchdbService = couchdbService;
// Set test environment variables
process.env.JWT_SECRET = 'test-jwt-secret';
// Set test environment variables (must be at least 32 chars for validation)
process.env.JWT_SECRET = 'test-jwt-secret-for-testing-purposes-that-is-long-enough';
process.env.NODE_ENV = 'test';
process.env.COUCHDB_URL = 'http://localhost:5984';
process.env.COUCHDB_DB_NAME = 'adopt-a-street-test';
process.env.COUCHDB_URL = 'http://localhost:5984';
process.env.COUCHDB_DB_NAME = 'test-adopt-a-street';
// Suppress console logs during tests unless there's an error