import os import unittest from unittest.mock import patch from dns_sync.adapters import Endpoint from dns_sync.config import AppConfig from dns_sync.npm_client import NpmClient class NpmClientEndpointTests(unittest.TestCase): def config(self): return AppConfig.from_env() def test_default_client_uses_first_configured_endpoint(self): with patch.dict( os.environ, { "NPM_ENDPOINTS": "192.168.1.3,192.168.1.4:8081", "DNS_TARGET_IP": "192.168.1.101", "NPM_IDENTITY": "test@example.com", "NPM_SECRET": "not-a-real-secret", }, clear=True, ): config = self.config() with patch( "dns_sync.npm_client.requests.Session.request" ) as request: client = NpmClient(config) request.assert_not_called() self.assertEqual(client.api_url, "http://192.168.1.3:81/api") def test_client_can_select_specific_configured_endpoint(self): with patch.dict( os.environ, { "NPM_ENDPOINTS": "192.168.1.3,192.168.1.4:8081", "DNS_TARGET_IP": "192.168.1.101", "NPM_IDENTITY": "test@example.com", "NPM_SECRET": "not-a-real-secret", }, clear=True, ): config = self.config() client = NpmClient(config, config.npm_endpoints[1]) self.assertEqual( client.api_url, "http://192.168.1.4:8081/api", ) self.assertEqual( client.endpoint.identity, "npm://192.168.1.4:8081", ) def test_client_rejects_non_npm_endpoint(self): with patch.dict( os.environ, { "PROXY_IP": "192.168.1.101", "NPM_IDENTITY": "test@example.com", "NPM_SECRET": "not-a-real-secret", }, clear=True, ): config = self.config() endpoint = Endpoint("verizon", "192.168.1.1") with self.assertRaisesRegex(RuntimeError, "npm endpoint"): NpmClient(config, endpoint) if __name__ == "__main__": unittest.main()