diff --git a/tests/test_core.py b/tests/test_core.py index 2391a84..d21a7a2 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -197,62 +197,60 @@ class MockNetboxVM: """Mock save method.""" -class TestNetboxv2TokenHandling(unittest.TestCase): - """Test that sync properly handles NetBox v2 token authentication.""" +class TestNetboxTokenHandling(unittest.TestCase): + """Test that sync properly handles NetBox token authentication.""" - @patch("netbox_zabbix_sync.modules.core.ZabbixAPI") - @patch("netbox_zabbix_sync.modules.core.nbapi") - def test_sync_uses_token_for_netbox_v2(self, mock_api, mock_zabbix_api): - """Test that sync uses token authentication for NetBox v2.""" - # Setup NetBox mock - mock_netbox = MagicMock() - mock_api.return_value = mock_netbox - mock_netbox.version = "4.5" + def test_v1_token_with_netbox_45(self): + """Test that v1 token with NetBox 4.5+ logs warning but returns True.""" + syncer = Sync() - # Setup Zabbix mock - mock_zabbix = MagicMock() - mock_zabbix_api.return_value = mock_zabbix - mock_zabbix.check_auth.return_value = True - - # Test v1 token (should log warning) with self.assertLogs("NetBox-Zabbix-sync", level="WARNING") as log_context: - syncer_with_v1_token = Sync() - result_v1_token = syncer_with_v1_token.connect( - nb_host="http://netbox.local", - nb_token="token123", # v1 token (doesn't start with "nbt_") - zbx_host="http://zabbix.local", - zbx_user="user", - zbx_pass="pass", - zbx_token=None, - ) + result = syncer._validate_netbox_token("token123", "4.5") - # Verify v1 token connection succeeded and warning was logged - self.assertTrue(result_v1_token) + self.assertTrue(result) self.assertTrue( any("v1 token format" in record.message for record in log_context.records) ) - # Test v2 token (should not log warning) - # Reset the log capture context - syncer_with_v2_token = Sync() - with self.assertLogs("NetBox-Zabbix-sync", level="DEBUG") as log_context_v2: - result_v2_token = syncer_with_v2_token.connect( - nb_host="http://netbox.local", - nb_token="nbt_token123", # v2 token (starts with "nbt_") - zbx_host="http://zabbix.local", - zbx_user="user", - zbx_pass="pass", - zbx_token=None, - ) + def test_v2_token_with_netbox_35(self): + """Test that v2 token with NetBox < 4.5 logs error and returns False.""" + syncer = Sync() - # Verify v2 token connection succeeded and NO warning was logged - self.assertTrue(result_v2_token) - self.assertFalse( + with self.assertLogs("NetBox-Zabbix-sync", level="ERROR") as log_context: + result = syncer._validate_netbox_token("nbt_token123", "3.5") + + self.assertFalse(result) + self.assertTrue( any( - "v1 token format" in record.message for record in log_context_v2.records + "v2 token format with Netbox version lower than 4.5" in record.message + for record in log_context.records ) ) + def test_v2_token_with_netbox_45(self): + """Test that v2 token with NetBox 4.5+ logs debug and returns True.""" + syncer = Sync() + + with self.assertLogs("NetBox-Zabbix-sync", level="DEBUG") as log_context: + result = syncer._validate_netbox_token("nbt_token123", "4.5") + + self.assertTrue(result) + self.assertTrue( + any("v2 token format" in record.message for record in log_context.records) + ) + + def test_v1_token_with_netbox_35(self): + """Test that v1 token with NetBox < 4.5 logs debug and returns True.""" + syncer = Sync() + + with self.assertLogs("NetBox-Zabbix-sync", level="DEBUG") as log_context: + result = syncer._validate_netbox_token("token123", "3.5") + + self.assertTrue(result) + self.assertTrue( + any("v1 token format" in record.message for record in log_context.records) + ) + class TestSyncNetboxConnection(unittest.TestCase): """Test NetBox connection handling in sync function."""