"""Exercise https_manager.py exactly as deploy.sh does, inside a container-like env. Overrides the Caddyfile path (the only host-only difference) to prove the enable/status/disable flow and the resulting Caddyfile content. """ import os import sys import tempfile tmp = tempfile.mkdtemp() os.environ['DATABASE_URL'] = f'sqlite:///{tmp}/t.db' from app.app import create_app from app.models.https_config import HTTPSConfig from app.utils.caddy_manager import CaddyConfigGenerator app = create_app('production') with app.app_context(): from app.extensions import db db.create_all() # Redirect the Caddyfile write/read to a temp path (the only host difference). _target = f'{tmp}/Caddyfile' CaddyConfigGenerator.write_caddyfile = staticmethod( lambda content, path=_target: (open(_target, 'w').write(content), True)[1]) CaddyConfigGenerator.reload_caddy = staticmethod(lambda: True) sys.argv = ['https_manager.py', 'enable', 'digiserver', '', 'admin@example.com', '192.168.0.152', '8443'] import importlib.util spec = importlib.util.spec_from_file_location('hm', '/home/scheianu/digiserver-v2/https_manager.py') hm = importlib.util.module_from_spec(spec) spec.loader.exec_module(hm) print('=== ENABLE (empty domain -> internal CA) ===') rc = hm.main() print('exit code:', rc) print() print('=== resulting Caddyfile site blocks ===') text = open(_target).read() for ln in text.splitlines(): s = ln.strip() if s.startswith(('http://', 'https://', ':80')) or 'tls internal' in s or 'redir' in s: print(' ', s) with app.app_context(): c = HTTPSConfig.get_config() print() print('=== stored config ===') print(' https_enabled:', c.https_enabled) print(' domain :', repr(c.domain)) print(' ip_address :', c.ip_address) print(' port :', c.port) assert c.https_enabled is True assert not c.domain, 'domain must be empty for internal CA' assert 'tls internal' in text, 'internal CA directive missing' assert 'http://192.168.0.152' in text, 'HTTP fallback missing' print() print('ASSERT PASS: internal CA + HTTP fallback generated correctly') print() print('=== STATUS ===') sys.argv = ['https_manager.py', 'status'] hm.main() print() print('=== DISABLE ===') sys.argv = ['https_manager.py', 'disable'] hm.main() with app.app_context(): c = HTTPSConfig.get_config() print(' https_enabled after disable:', c.https_enabled)