import os import unittest from unittest.mock import patch from dns_sync.config import AppConfig, parse_endpoint_list class EndpointParsingTests(unittest.TestCase): def test_comma_separated_addresses_use_default_port(self): endpoints = parse_endpoint_list( "npm", "192.168.1.3, 192.168.1.4:8081,192.168.1.5", 81, ) self.assertEqual( tuple(endpoint.identity for endpoint in endpoints), ( "npm://192.168.1.3:81", "npm://192.168.1.4:8081", "npm://192.168.1.5:81", ), ) def test_duplicate_is_rejected_after_normalization(self): with self.assertRaisesRegex(RuntimeError, "Duplicate NPM"): parse_endpoint_list( "npm", "192.168.1.3,192.168.1.3:81", 81, ) def test_empty_entry_is_rejected(self): with self.assertRaisesRegex(RuntimeError, "empty address"): parse_endpoint_list( "npm", "192.168.1.3,,192.168.1.5", 81, ) def test_url_scheme_is_rejected(self): with self.assertRaisesRegex(RuntimeError, "must not include"): parse_endpoint_list( "npm", "http://192.168.1.3:81", 81, ) class AppConfigPolicyTests(unittest.TestCase): def test_explicit_dns_target_is_used(self): with patch.dict( os.environ, { "DNS_TARGET_IP": "192.168.1.101", "NPM_ENDPOINTS": "192.168.1.3,192.168.1.4", }, clear=True, ): config = AppConfig.from_env() self.assertEqual( config.target_ip, "192.168.1.101", ) def test_legacy_proxy_ip_remains_target_fallback(self): with patch.dict( os.environ, { "PROXY_IP": "192.168.1.101", }, clear=True, ): config = AppConfig.from_env() self.assertEqual( config.target_ip, "192.168.1.101", ) def test_dns_target_is_required_without_legacy_fallback(self): with patch.dict( os.environ, { "NPM_ENDPOINTS": "192.168.1.3", }, clear=True, ): with self.assertRaisesRegex( RuntimeError, "DNS_TARGET_IP is required", ): AppConfig.from_env() if __name__ == "__main__": unittest.main()