#!/usr/bin/env python3 """ Test production deployment with Waitress WSGI server. Starts Waitress on a free port, verifies health endpoint, then shuts down. """ import subprocess import time import signal import sys import os from typing import Optional import httpx # Set environment variables for production-like settings os.environ['DEBUG'] = 'False' os.environ['ADMIN_PASSWORD'] = 'test-production-password' os.environ['SECRET_KEY'] = 'test-secret-key-for-production-test' def wait_for_server(url: str, timeout: int = 10) -> bool: """Wait until server responds with 200 OK.""" start = time.time() while time.time() - start < timeout: try: response = httpx.get(url, timeout=1) if response.status_code == 200: return True except (httpx.ConnectError, httpx.ReadTimeout): pass time.sleep(0.5) return False def main(): port = 18081 # Use a high port unlikely to conflict host = '127.0.0.1' url = f'http://{host}:{port}' # Start Waitress server in a subprocess print(f"Starting Waitress server on {url}...") # Set PYTHONPATH to ensure wsgi module can be imported env = os.environ.copy() env['PYTHONPATH'] = '.' proc = subprocess.Popen( [ 'waitress-serve', '--host', host, '--port', str(port), '--threads', '2', 'wsgi:wsgi_app' ], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) try: # Give server a moment to start time.sleep(2) # Wait for server to be ready print("Waiting for server to be ready...") if not wait_for_server(f'{url}/health', timeout=30): print("ERROR: Server did not become ready within timeout") proc.terminate() stdout, stderr = proc.communicate(timeout=5) print("STDOUT:", stdout) print("STDERR:", stderr) sys.exit(1) # Test health endpoint print("Testing health endpoint...") response = httpx.get(f'{url}/health', timeout=5) if response.status_code == 200: print(f"SUCCESS: Health endpoint returned {response.status_code}: {response.json()}") else: print(f"ERROR: Health endpoint returned {response.status_code}: {response.text}") sys.exit(1) # Test admin login page (should be accessible) print("Testing admin login page...") response = httpx.get(f'{url}/admin/login', timeout=5) if response.status_code == 200 and 'Admin Login' in response.text: print("SUCCESS: Admin login page accessible") else: print(f"ERROR: Admin login page failed: {response.status_code}") sys.exit(1) print("\n✅ All production tests passed!") finally: # Kill the server print("Shutting down Waitress server...") proc.terminate() try: proc.wait(timeout=5) except subprocess.TimeoutExpired: proc.kill() proc.wait() print("Server stopped.") if __name__ == '__main__': main()