|
| 1 | +"""Tests for security module.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from app.security import ( |
| 9 | + configure_production_logging, |
| 10 | + generate_secure_keys, |
| 11 | + security_health_check, |
| 12 | + validate_production_config, |
| 13 | +) |
| 14 | + |
| 15 | + |
| 16 | +class TestConfigureProductionLogging: |
| 17 | + """Tests for configure_production_logging function.""" |
| 18 | + |
| 19 | + def test_configure_logging_default_level(self): |
| 20 | + """Test logging configuration with default INFO level.""" |
| 21 | + with patch.dict(os.environ, {}, clear=False): |
| 22 | + configure_production_logging() |
| 23 | + |
| 24 | + def test_configure_logging_debug_level(self): |
| 25 | + """Test logging configuration with DEBUG level.""" |
| 26 | + with patch.dict(os.environ, {"LOG_LEVEL": "DEBUG"}, clear=False): |
| 27 | + configure_production_logging() |
| 28 | + |
| 29 | + def test_configure_logging_production_environment(self): |
| 30 | + """Test logging configuration in production environment.""" |
| 31 | + with patch.dict(os.environ, {"ENVIRONMENT": "production"}, clear=False): |
| 32 | + configure_production_logging() |
| 33 | + |
| 34 | + |
| 35 | +class TestGenerateSecureKeys: |
| 36 | + """Tests for generate_secure_keys function.""" |
| 37 | + |
| 38 | + def test_generate_keys_returns_dict(self): |
| 39 | + """Test that generate_secure_keys returns a dictionary.""" |
| 40 | + keys = generate_secure_keys() |
| 41 | + assert isinstance(keys, dict) |
| 42 | + assert "api_key" in keys |
| 43 | + assert "secret_key" in keys |
| 44 | + |
| 45 | + def test_generate_keys_are_unique(self): |
| 46 | + """Test that each call generates unique keys.""" |
| 47 | + keys1 = generate_secure_keys() |
| 48 | + keys2 = generate_secure_keys() |
| 49 | + assert keys1["api_key"] != keys2["api_key"] |
| 50 | + assert keys1["secret_key"] != keys2["secret_key"] |
| 51 | + |
| 52 | + def test_generate_keys_sufficient_length(self): |
| 53 | + """Test that generated keys have sufficient length.""" |
| 54 | + keys = generate_secure_keys() |
| 55 | + assert len(keys["api_key"]) >= 32 |
| 56 | + assert len(keys["secret_key"]) >= 32 |
| 57 | + |
| 58 | + |
| 59 | +class TestValidateProductionConfig: |
| 60 | + """Tests for validate_production_config function.""" |
| 61 | + |
| 62 | + def test_valid_config(self): |
| 63 | + """Test validation with valid configuration.""" |
| 64 | + env = { |
| 65 | + "API_KEY": "test-api-key-12345678901234567890", |
| 66 | + "SECRET_KEY": "test-secret-key-12345678901234567890", |
| 67 | + "CORS_ORIGINS": "https://example.com", |
| 68 | + "ENVIRONMENT": "production", |
| 69 | + } |
| 70 | + with patch.dict(os.environ, env, clear=False): |
| 71 | + result = validate_production_config() |
| 72 | + assert result["valid"] is True |
| 73 | + assert len(result["errors"]) == 0 |
| 74 | + |
| 75 | + def test_missing_api_key(self): |
| 76 | + """Test validation with missing API_KEY.""" |
| 77 | + env = {"SECRET_KEY": "test-secret", "API_KEY": ""} |
| 78 | + with patch.dict(os.environ, env, clear=False): |
| 79 | + os.environ.pop("API_KEY", None) |
| 80 | + result = validate_production_config() |
| 81 | + assert any("API_KEY" in e for e in result["errors"]) |
| 82 | + |
| 83 | + def test_short_api_key_warning(self): |
| 84 | + """Test warning for short API key.""" |
| 85 | + env = { |
| 86 | + "API_KEY": "short", |
| 87 | + "SECRET_KEY": "test-secret", |
| 88 | + "CORS_ORIGINS": "https://example.com", |
| 89 | + } |
| 90 | + with patch.dict(os.environ, env, clear=False): |
| 91 | + result = validate_production_config() |
| 92 | + assert any("16 characters" in w for w in result["warnings"]) |
| 93 | + |
| 94 | + def test_wildcard_cors_error(self): |
| 95 | + """Test error for wildcard CORS origins.""" |
| 96 | + env = { |
| 97 | + "API_KEY": "test-api-key-12345678901234567890", |
| 98 | + "SECRET_KEY": "test-secret", |
| 99 | + "CORS_ORIGINS": "*", |
| 100 | + } |
| 101 | + with patch.dict(os.environ, env, clear=False): |
| 102 | + result = validate_production_config() |
| 103 | + assert any("wildcard" in e for e in result["errors"]) |
| 104 | + |
| 105 | + def test_non_production_environment_warning(self): |
| 106 | + """Test warning for non-production environment.""" |
| 107 | + env = { |
| 108 | + "API_KEY": "test-api-key-12345678901234567890", |
| 109 | + "SECRET_KEY": "test-secret", |
| 110 | + "CORS_ORIGINS": "https://example.com", |
| 111 | + "ENVIRONMENT": "development", |
| 112 | + } |
| 113 | + with patch.dict(os.environ, env, clear=False): |
| 114 | + result = validate_production_config() |
| 115 | + assert any("production-ready" in w for w in result["warnings"]) |
| 116 | + |
| 117 | + |
| 118 | +class TestSecurityHealthCheck: |
| 119 | + """Tests for security_health_check function.""" |
| 120 | + |
| 121 | + def test_healthy_status(self): |
| 122 | + """Test health check with valid configuration.""" |
| 123 | + env = { |
| 124 | + "API_KEY": "test-api-key-12345678901234567890", |
| 125 | + "SECRET_KEY": "test-secret-key-12345678901234567890", |
| 126 | + "CORS_ORIGINS": "https://example.com", |
| 127 | + "ENVIRONMENT": "production", |
| 128 | + } |
| 129 | + with patch.dict(os.environ, env, clear=False): |
| 130 | + result = security_health_check() |
| 131 | + assert result["security_status"] == "healthy" |
| 132 | + |
| 133 | + def test_unhealthy_status(self): |
| 134 | + """Test health check with invalid configuration.""" |
| 135 | + env = {"CORS_ORIGINS": "*", "API_KEY": "", "SECRET_KEY": ""} |
| 136 | + with patch.dict(os.environ, env, clear=False): |
| 137 | + os.environ.pop("API_KEY", None) |
| 138 | + os.environ.pop("SECRET_KEY", None) |
| 139 | + result = security_health_check() |
| 140 | + assert result["security_status"] == "unhealthy" |
0 commit comments