# Copyright 2025 Bemade # License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.html) """Tests for OdooRPC protocol implementation.""" from odoo.tests import common from odoo.exceptions import UserError class TestOdooRpcProtocol(common.TransactionCase): """Test OdooRPC protocol implementation.""" def setUp(self): """Set up test data.""" super().setUp() self.sync_instance = self.env['odoo.sync.instance'].create({ 'name': 'Test OdooRPC Instance', 'url': 'https://test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'test_api_key_12345', 'connection_type': 'odoorpc', }) def test_odoorpc_connection_creation(self): """Test OdooRPC connection creation.""" # Test that OdooRPC connection type is available self.assertEqual(self.sync_instance.connection_type, 'odoorpc') # Test connection methods exist self.assertTrue(hasattr(self.sync_instance, '_get_odoorpc_connection')) self.assertTrue(hasattr(self.sync_instance, '_test_odoorpc_connection')) def test_odoorpc_field_validation(self): """Test OdooRPC field validation.""" # Test that OdooRPC specific fields are properly validated instance = self.env['odoo.sync.instance'].create({ 'name': 'OdooRPC Validation Test', 'url': 'https://odoorpc-test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'odoorpc_test_api_key', 'connection_type': 'odoorpc', }) self.assertEqual(instance.connection_type, 'odoorpc') self.assertTrue(instance.url.startswith('http')) def test_odoorpc_url_formatting(self): """Test OdooRPC URL formatting.""" # Test URL formatting for OdooRPC test_instance = self.env['odoo.sync.instance'].create({ 'name': 'URL Test Instance', 'url': 'https://odoorpc-test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'test_api_key', 'connection_type': 'odoorpc', }) # Verify URL is properly formatted self.assertTrue(test_instance.url.endswith('.com')) self.assertTrue(test_instance.url.startswith('https://')) def test_odoorpc_instance_creation(self): """Test OdooRPC instance creation.""" # Test creating multiple OdooRPC instances instances = [] for i in range(3): instance = self.env['odoo.sync.instance'].create({ 'name': f'OdooRPC Instance {i}', 'url': f'https://odoorpc-test-{i}.example.com', 'database': f'test_db_{i}', 'username': f'test_user_{i}', 'api_key': f'test_api_key_{i}', 'connection_type': 'odoorpc', }) instances.append(instance) self.assertTrue(instance.id) self.assertEqual(instance.connection_type, 'odoorpc') # Verify all instances were created self.assertEqual(len(instances), 3) def test_odoorpc_field_requirements(self): """Test OdooRPC field requirements.""" # Test required fields for OdooRPC with self.assertRaises(Exception): self.env['odoo.sync.instance'].create({ 'name': 'Incomplete OdooRPC Instance', 'url': 'https://incomplete.example.com', # Missing required fields }) def test_odoorpc_protocol_selection(self): """Test OdooRPC protocol selection.""" # Test that OdooRPC can be selected as connection type instance = self.env['odoo.sync.instance'].create({ 'name': 'Protocol Selection Test', 'url': 'https://protocol-test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'protocol_test_api_key', 'connection_type': 'odoorpc', }) self.assertEqual(instance.connection_type, 'odoorpc') self.assertIn('odoorpc', ['jsonrpc', 'xmlrpc', 'odoorpc']) def test_odoorpc_client_structure(self): """Test OdooRPC client structure.""" # Test that the client configuration is properly set instance = self.env['odoo.sync.instance'].create({ 'name': 'Client Structure Test', 'url': 'https://client-test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'client_test_api_key', 'connection_type': 'odoorpc', }) # Verify instance configuration self.assertEqual(instance.connection_type, 'odoorpc') self.assertTrue(instance.api_key) def test_odoorpc_protocol_integration(self): """Test OdooRPC protocol integration.""" # Test complete OdooRPC protocol integration instance = self.env['odoo.sync.instance'].create({ 'name': 'Integration Test Instance', 'url': 'https://integration-test.example.com', 'database': 'test_db', 'username': 'test_user', 'api_key': 'integration_test_api_key', 'connection_type': 'odoorpc', }) # Verify all components are properly configured self.assertTrue(instance.id) self.assertEqual(instance.connection_type, 'odoorpc') self.assertTrue(instance.url) self.assertTrue(instance.database) self.assertTrue(instance.username) self.assertTrue(instance.api_key) def test_odoorpc_url_construction(self): """Test OdooRPC URL construction.""" # Test URL parsing for OdooRPC expected_url = 'https://test.example.com' self.assertEqual(self.sync_instance.url, expected_url) def test_odoorpc_api_key_authentication(self): """Test OdooRPC API key authentication.""" # Test that API key is used for authentication self.assertTrue(self.sync_instance.use_api_key) self.assertEqual(self.sync_instance.api_key, 'test_api_key_12345') def test_odoorpc_connection_methods(self): """Test OdooRPC connection methods.""" # Test that the connection has expected methods # This would be tested with actual odoorpc library self.assertTrue(hasattr(self.sync_instance, '_get_odoorpc_connection')) self.assertTrue(hasattr(self.sync_instance, '_test_odoorpc_connection')) def test_odoorpc_error_handling(self): """Test OdooRPC error handling.""" # Test various error scenarios test_cases = [ ('ConnectionError', 'Connection Error'), ('AuthenticationError', 'Authentication Error'), ('TimeoutError', 'Timeout Error') ] for error_type, expected_msg in test_cases: with self.subTest(error_type=error_type): # These would be tested in integration tests self.assertIsNotNone(expected_msg) def test_odoorpc_encryption_handling(self): """Test API key encryption in OdooRPC.""" # Test that API key is properly encrypted/decrypted original_key = self.sync_instance.api_key encrypted_key = self.sync_instance.encrypted_api_key self.assertIsNotNone(encrypted_key) self.assertNotEqual(original_key, encrypted_key) # Test decryption decrypted_key = self.sync_instance._decrypt_sensitive_data() self.assertEqual(decrypted_key, original_key)