import importlib.util import sys from pathlib import Path import pytest import yaml from cryptography import x509 from dotenv import dotenv_values ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT)) spec = importlib.util.spec_from_file_location('deployment_initialize', ROOT/'scripts/initialize.py') module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) def configured(tmp_path): (tmp_path/'settings.env').write_text( 'HN_PUBLIC_URL=https://192.0.2.10\nHN_FEISHU_API_BASE=https://work.example.test\n' 'HN_FEISHU_AUTHORIZE_URL=https://work.example.test/open-apis/authen/v1/authorize\n' 'HN_FEISHU_APP_ID=cli_test\nHN_FEISHU_APP_SECRET=fake-secret\n', encoding='utf-8') return tmp_path def test_initialize_ip_certificate_and_separate_keys(tmp_path): module.initialize(configured(tmp_path), test_cert=True) values = dotenv_values(tmp_path/'.env') assert values['HN_ADMIN_SECRET'] != values['HN_INTERNAL_SECRET'] assert min(len(values[k]) for k in ['HN_ADMIN_SECRET','HN_INTERNAL_SECRET','HN_REDIS_PASSWORD']) >= 32 cert=x509.load_pem_x509_certificate((tmp_path/'runtime/tls/server.pem').read_bytes()) san=cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value assert str(san.get_values_for_type(x509.IPAddress)[0]) == '192.0.2.10' module.validate_tls(tmp_path/'runtime/tls/server.pem',tmp_path/'runtime/tls/server.key','192.0.2.10') assert (tmp_path/'runtime/issuer/ca.key').read_bytes() != (tmp_path/'runtime/connector/connector.key').read_bytes() def test_refuses_existing_environment_without_changing_keys(tmp_path): module.initialize(configured(tmp_path), test_cert=True) previous=(tmp_path/'runtime/issuer/ca.key').read_bytes() with pytest.raises(ValueError, match='Existing deployment'): module.initialize(tmp_path, test_cert=True) assert (tmp_path/'runtime/issuer/ca.key').read_bytes() == previous def test_missing_tls_does_not_generate_device_identity(tmp_path): configured(tmp_path) with pytest.raises((FileNotFoundError, OSError)): module.initialize(tmp_path) assert not (tmp_path/'.env').exists() assert not (tmp_path/'runtime/issuer/ca.key').exists() def test_tls_hostname_mismatch_rejected(tmp_path): module.initialize(configured(tmp_path), test_cert=True) with pytest.raises(Exception, match='match'): module.validate_tls(tmp_path/'runtime/tls/server.pem',tmp_path/'runtime/tls/server.key','other.example.test') def test_placeholder_rejected_before_identity_generation(tmp_path): text=(ROOT/'.env.example').read_text(encoding='utf-8') (tmp_path/'settings.env').write_text(text,encoding='utf-8') with pytest.raises(ValueError, match='Configure HN_PUBLIC_URL'): module.initialize(tmp_path, test_cert=True) assert not (tmp_path/'runtime').exists() def test_compose_has_complete_build_and_internal_issuer(): text=(ROOT/'compose.yaml').read_text(encoding='utf-8') assert '' not in text services=yaml.safe_load(text)['services'] assert len(services)==7 assert 'profiles' not in services['longconn'] assert 'ports' not in services['enrollment-signer'] for service in ['connector','router','longconn']: assert all('/issuer' not in v for v in services[service].get('volumes', [])) for file in ['Dockerfile','Dockerfile.offline','requirements.lock','pyproject.toml','relay/cli.py']: assert (ROOT/file).is_file()