60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
import unittest
|
|
import os
|
|
import sys
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
# Add parent directory to path so we can import the application
|
|
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
|
|
from src.app import Application
|
|
from src.protocols.protocol_interface import FileTransferProtocol
|
|
from src.utils.config import Config
|
|
|
|
class TestApplication(unittest.TestCase):
|
|
"""Test cases for the main application"""
|
|
|
|
@patch('src.utils.config.Config')
|
|
def test_application_init(self, mock_config):
|
|
"""Test application initialization"""
|
|
# Set up mock configuration
|
|
mock_config_instance = MagicMock()
|
|
mock_config.return_value = mock_config_instance
|
|
|
|
# Initialize application
|
|
app = Application()
|
|
|
|
# Verify application initialized correctly
|
|
self.assertIsNotNone(app.logger)
|
|
self.assertIsNotNone(app.monitoring)
|
|
self.assertFalse(app.running)
|
|
|
|
@patch('src.utils.config.Config')
|
|
@patch('src.protocols.protocol_factory.ProtocolFactory.create_protocol')
|
|
def test_create_protocol(self, mock_create_protocol, mock_config):
|
|
"""Test protocol creation"""
|
|
# Set up mock configuration
|
|
mock_config_instance = MagicMock()
|
|
mock_config_instance.get.side_effect = lambda section, key: {
|
|
('app', 'TRANSFER_PROTOCOL'): 'SSH',
|
|
('server', 'REMOTE_HOST'): 'testhost',
|
|
('server', 'REMOTE_USER'): 'testuser',
|
|
('server', 'REMOTE_PASS'): 'testpass',
|
|
}.get((section, key))
|
|
|
|
mock_config_instance.get_int.return_value = 22
|
|
mock_config.return_value = mock_config_instance
|
|
|
|
# Set up mock protocol
|
|
mock_protocol = MagicMock(spec=FileTransferProtocol)
|
|
mock_create_protocol.return_value = mock_protocol
|
|
|
|
# Initialize application and create protocol
|
|
app = Application()
|
|
protocol = app._create_protocol()
|
|
|
|
# Verify protocol was created correctly
|
|
self.assertEqual(protocol, mock_protocol)
|
|
mock_create_protocol.assert_called_once_with('SSH', 'testhost', 'testuser', 'testpass', 22)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |