#!/usr/bin/env python3 """ Tests for the UniFi device configuration functionality of the SSL Manager. This module contains tests for loading and using UniFi device 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 TestUniFiConfig(unittest.TestCase): """Test cases for UniFi device 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" } } def tearDown(self): """Tear down test fixtures.""" # Clean up the temporary directory self.temp_dir.cleanup() def test_load_config_with_unifi_params(self): """Test loading a configuration file with UniFi device 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 parameters self.assertIn("unifi", config) self.assertEqual(config["unifi"]["host"], "test.unifi.local") self.assertEqual(config["unifi"]["username"], "testuser") self.assertEqual(config["unifi"]["password"], "testpass") self.assertEqual(config["unifi"]["site"], "testsite") def test_ssl_manager_stores_unifi_params(self): """Test that SSLManager stores UniFi device 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 parameters self.assertEqual(ssl_manager.unifi_host, "test.unifi.local") self.assertEqual(ssl_manager.unifi_username, "testuser") self.assertEqual(ssl_manager.unifi_password, "testpass") self.assertEqual(ssl_manager.unifi_site, "testsite") def test_get_unifi_connection_params(self): """Test the get_unifi_connection_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 connection parameters params = ssl_manager.get_unifi_connection_params() # Verify the parameters self.assertIsInstance(params, dict) self.assertEqual(params["host"], "test.unifi.local") self.assertEqual(params["username"], "testuser") self.assertEqual(params["password"], "testpass") self.assertEqual(params["site"], "testsite") if __name__ == '__main__': unittest.main()