#!/usr/bin/env python3 """ Tests for the UniFi device SSH configuration functionality of the SSL Manager. This module contains tests for loading and using UniFi device SSH configuration values. """ import os import sys import json import tempfile import unittest from unittest.mock import patch # Add the src directory to the Python path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../src'))) from ssl_manager import load_config, SSLManager class TestUniFiSSHConfig(unittest.TestCase): """Test cases for UniFi device SSH configuration functionality.""" def setUp(self): """Set up test fixtures.""" # Create a temporary directory for test files self.temp_dir = tempfile.TemporaryDirectory() # Sample config for testing self.test_config = { "cert_dir": "~/test-certs", "default_port": 8443, "connection_timeout": 5.0, "default_validity_days": 730, "key_size": 4096, "unifi": { "host": "test.unifi.local", "username": "testuser", "password": "testpass", "site": "testsite", "ssh_port": 2222, "ssh_username": "sshuser", "ssh_password": "sshpass", "ssh_key_path": "~/test-ssh-key" } } def tearDown(self): """Tear down test fixtures.""" # Clean up the temporary directory self.temp_dir.cleanup() def test_load_config_with_ssh_params(self): """Test loading a configuration file with UniFi device SSH parameters.""" # Create a temporary config file config_path = os.path.join(self.temp_dir.name, "test_config.json") with open(config_path, 'w') as f: json.dump(self.test_config, f) # Load the config config = load_config(config_path) # Verify the UniFi device SSH parameters self.assertIn("unifi", config) self.assertEqual(config["unifi"]["ssh_port"], 2222) self.assertEqual(config["unifi"]["ssh_username"], "sshuser") self.assertEqual(config["unifi"]["ssh_password"], "sshpass") self.assertEqual(config["unifi"]["ssh_key_path"], "~/test-ssh-key") def test_ssl_manager_stores_ssh_params(self): """Test that SSLManager stores UniFi device SSH parameters.""" # Create a temporary config file config_path = os.path.join(self.temp_dir.name, "test_config.json") with open(config_path, 'w') as f: json.dump(self.test_config, f) # Create an SSLManager with the config ssl_manager = SSLManager(config_path=config_path) # Verify the manager stores the UniFi device SSH parameters self.assertEqual(ssl_manager.unifi_ssh_port, 2222) self.assertEqual(ssl_manager.unifi_ssh_username, "sshuser") self.assertEqual(ssl_manager.unifi_ssh_password, "sshpass") self.assertEqual(ssl_manager.unifi_ssh_key_path, "~/test-ssh-key") def test_get_unifi_ssh_params(self): """Test the get_unifi_ssh_params method.""" # Create a temporary config file config_path = os.path.join(self.temp_dir.name, "test_config.json") with open(config_path, 'w') as f: json.dump(self.test_config, f) # Create an SSLManager with the config ssl_manager = SSLManager(config_path=config_path) # Get the UniFi device SSH parameters params = ssl_manager.get_unifi_ssh_params() # Verify the parameters self.assertIsInstance(params, dict) self.assertEqual(params["host"], "test.unifi.local") self.assertEqual(params["port"], 2222) self.assertEqual(params["username"], "sshuser") self.assertEqual(params["password"], "sshpass") self.assertEqual(params["key_path"], "~/test-ssh-key") if __name__ == '__main__': unittest.main()