Files
adopt-a-street/backend/__tests__/jest.setup.js
William Valentin 928d9a65fe 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>
2025-11-03 13:07:26 -08:00

23 lines
836 B
JavaScript

// Get reference to the mocked couchdbService for test usage
const couchdbService = require('../services/couchdbService');
// Make mock available for tests to reference
global.mockCouchdbService = couchdbService;
// 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
global.console = {
...console,
log: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: console.error, // Keep error logging
};