ssl-managment/tests/test_config.py
Mike Geppert a78cf961ff Initial commit for SSL Management project
This commit includes:
- SSL Manager implementation for certificate operations
- Configuration file with UniFi device parameters
- Test files for various components
- Documentation for UniFi verification
- Project guidelines
2025-07-20 20:46:42 -05:00

134 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""
Tests for the configuration functionality of the SSL Manager.
This module contains tests for loading and using configuration values.
"""
import os
import sys
import json
import tempfile
import unittest
from unittest.mock import patch, mock_open
# 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 TestConfigLoading(unittest.TestCase):
"""Test cases for configuration loading 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
}
def tearDown(self):
"""Tear down test fixtures."""
# Clean up the temporary directory
self.temp_dir.cleanup()
def test_load_config_with_valid_file(self):
"""Test loading a valid configuration file."""
# 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 config values
self.assertEqual(config["cert_dir"], "~/test-certs")
self.assertEqual(config["default_port"], 8443)
self.assertEqual(config["connection_timeout"], 5.0)
self.assertEqual(config["default_validity_days"], 730)
self.assertEqual(config["key_size"], 4096)
def test_load_config_with_missing_file(self):
"""Test loading a non-existent configuration file."""
# Use a non-existent file path
config_path = os.path.join(self.temp_dir.name, "nonexistent.json")
# Load the config
config = load_config(config_path)
# Verify default values are used
self.assertEqual(config["cert_dir"], "~/.ssl-certs")
self.assertEqual(config["default_port"], 443)
self.assertEqual(config["connection_timeout"], 3.0)
self.assertEqual(config["default_validity_days"], 365)
self.assertEqual(config["key_size"], 2048)
def test_load_config_with_partial_file(self):
"""Test loading a configuration file with only some values."""
# Create a config with only some values
partial_config = {
"default_port": 8443,
"key_size": 4096
}
# Create a temporary config file
config_path = os.path.join(self.temp_dir.name, "partial_config.json")
with open(config_path, 'w') as f:
json.dump(partial_config, f)
# Load the config
config = load_config(config_path)
# Verify the specified values are used and others are defaults
self.assertEqual(config["cert_dir"], "~/.ssl-certs") # Default
self.assertEqual(config["default_port"], 8443) # From file
self.assertEqual(config["connection_timeout"], 3.0) # Default
self.assertEqual(config["default_validity_days"], 365) # Default
self.assertEqual(config["key_size"], 4096) # From file
def test_ssl_manager_uses_config_values(self):
"""Test that SSLManager uses values from the config file."""
# 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 uses the config values
self.assertEqual(ssl_manager.default_port, 8443)
self.assertEqual(ssl_manager.connection_timeout, 5.0)
self.assertEqual(ssl_manager.default_validity_days, 730)
self.assertEqual(ssl_manager.key_size, 4096)
def test_ssl_manager_cert_dir_override(self):
"""Test that cert_dir parameter overrides the config value."""
# 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 a custom cert_dir
custom_cert_dir = os.path.join(self.temp_dir.name, "custom-certs")
ssl_manager = SSLManager(cert_dir=custom_cert_dir, config_path=config_path)
# Verify the manager uses the custom cert_dir
self.assertEqual(ssl_manager.cert_dir, custom_cert_dir)
# Verify the directory was created
self.assertTrue(os.path.exists(custom_cert_dir))
self.assertTrue(os.path.isdir(custom_cert_dir))
if __name__ == '__main__':
unittest.main()