#![allow(clippy::result_large_err)]
use pandrs::config::credentials::*;
use pandrs::config::loader::*;
use pandrs::config::validation::*;
use pandrs::config::*;
use std::env;
use tempfile::tempdir;
#[test]
fn test_default_config() {
let config = PandRSConfig::default();
assert_eq!(config.database.pool.max_connections, 10);
assert_eq!(config.cloud.global.timeout, 300);
assert!(config.performance.jit.enabled);
assert_eq!(config.logging.level, "info");
assert!(validate_config(&config).is_ok());
}
#[test]
fn test_config_serialization() {
let config = PandRSConfig::default();
let yaml = config.to_yaml().unwrap();
assert!(yaml.contains("database:"));
assert!(yaml.contains("cloud:"));
assert!(yaml.contains("performance:"));
let toml = config.to_toml().unwrap();
assert!(toml.contains("[database."));
assert!(toml.contains("[cloud."));
assert!(toml.contains("[performance."));
}
#[test]
fn test_config_validation() {
let mut config = PandRSConfig::default();
assert!(validate_config(&config).is_ok());
config.database.pool.max_connections = 0;
assert!(validate_config(&config).is_err());
config.database.pool.max_connections = 10;
assert!(validate_config(&config).is_ok());
config.database.ssl.mode = "invalid-mode".to_string();
assert!(validate_config(&config).is_err());
}
#[test]
fn test_environment_config_loading() {
env::set_var("PANDRS_DB_POOL_SIZE", "25");
env::set_var("AWS_DEFAULT_REGION", "eu-west-1");
env::set_var("PANDRS_LOG_LEVEL", "debug");
let config = load_from_env().unwrap();
assert_eq!(config.database.pool.max_connections, 25);
assert_eq!(config.cloud.aws.region, Some("eu-west-1".to_string()));
assert_eq!(config.logging.level, "debug");
env::remove_var("PANDRS_DB_POOL_SIZE");
env::remove_var("AWS_DEFAULT_REGION");
env::remove_var("PANDRS_LOG_LEVEL");
}
#[test]
fn test_config_file_operations() {
let dir = tempdir().unwrap();
let config_path = dir.path().join("test_config.yml");
let original_config = PandRSConfig::default();
save_to_file(&original_config, &config_path).unwrap();
assert!(config_path.exists());
let loaded_config = load_from_file(&config_path).unwrap();
assert_eq!(
loaded_config.database.pool.max_connections,
original_config.database.pool.max_connections
);
assert_eq!(
loaded_config.cloud.global.timeout,
original_config.cloud.global.timeout
);
assert_eq!(loaded_config.logging.level, original_config.logging.level);
}
#[test]
fn test_config_precedence() {
let dir = tempdir().unwrap();
let config_path = dir.path().join("precedence_test.yml");
let mut file_config = PandRSConfig::default();
file_config.database.pool.max_connections = 30;
file_config.cloud.aws.region = Some("us-east-1".to_string());
save_to_file(&file_config, &config_path).unwrap();
env::set_var("PANDRS_DB_POOL_SIZE", "50");
env::set_var("AWS_DEFAULT_REGION", "ap-southeast-1");
let config = load_with_precedence(Some(&config_path)).unwrap();
assert_eq!(config.database.pool.max_connections, 50);
assert_eq!(config.cloud.aws.region, Some("ap-southeast-1".to_string()));
env::remove_var("PANDRS_DB_POOL_SIZE");
env::remove_var("AWS_DEFAULT_REGION");
}
#[test]
fn test_credential_store_basic_operations() {
let mut store = CredentialStore::with_defaults();
store.init_encryption("test_password").unwrap();
let credential = CredentialBuilder::new()
.database("testuser", "testpass", "localhost", 5432, "testdb")
.with_tags(vec!["test".to_string(), "local".to_string()])
.build()
.unwrap();
store.store_credential("test_db", credential).unwrap();
assert!(store.has_credential("test_db"));
assert!(!store.has_credential("missing_db"));
let retrieved = store.get_credential("test_db").unwrap();
match retrieved {
CredentialType::Database {
username,
password,
host,
port,
database,
} => {
assert_eq!(username, "testuser");
assert_eq!(password, "testpass");
assert_eq!(host, "localhost");
assert_eq!(port, 5432);
assert_eq!(database, "testdb");
}
_ => panic!("Wrong credential type retrieved"),
}
let metadata = store.get_credential_metadata("test_db").unwrap();
assert_eq!(metadata.credential_type, "database");
assert!(metadata.active);
let creds = store.list_credentials();
assert_eq!(creds.len(), 1);
assert!(creds.contains(&"test_db".to_string()));
store.remove_credential("test_db").unwrap();
assert!(!store.has_credential("test_db"));
}
#[test]
fn test_credential_encryption() {
let mut store = CredentialStore::with_defaults();
store.init_encryption("strong_password_123").unwrap();
let credential = CredentialBuilder::new()
.cloud_aws("AKIATEST123", "secret_key_456", Some("us-west-2"))
.with_expiry("2025-12-31T23:59:59Z")
.build()
.unwrap();
store.store_credential("aws_prod", credential).unwrap();
let retrieved = store.get_credential("aws_prod").unwrap();
match retrieved {
CredentialType::Cloud {
provider,
access_key,
secret_key,
region,
..
} => {
assert_eq!(provider, "aws");
assert_eq!(access_key, "AKIATEST123");
assert_eq!(secret_key, "secret_key_456");
assert_eq!(region, Some("us-west-2".to_string()));
}
_ => panic!("Wrong credential type"),
}
}
#[test]
fn test_credential_builder_patterns() {
let db_cred = CredentialBuilder::new()
.database("admin", "secure123", "prod.db.com", 5432, "production")
.with_tags(vec!["production".to_string(), "critical".to_string()])
.build()
.unwrap();
assert!(matches!(db_cred, CredentialType::Database { .. }));
let api_cred = CredentialBuilder::new()
.api_key(
"sk-1234567890abcdef",
Some("secret"),
Some("https://api.example.com"),
)
.with_expiry("2025-06-30T00:00:00Z")
.build()
.unwrap();
assert!(matches!(api_cred, CredentialType::ApiKey { .. }));
let aws_cred = CredentialBuilder::new()
.cloud_aws("AKIA123", "secret456", Some("us-east-1"))
.build()
.unwrap();
assert!(matches!(aws_cred, CredentialType::Cloud { .. }));
}
#[test]
fn test_configuration_integration() {
let mut config = PandRSConfig::default();
config.security.encryption.enabled = true;
config.security.audit.enabled = true;
config.database.ssl.enabled = true;
assert!(validate_config(&config).is_ok());
let mut store = CredentialStore::new(CredentialStoreConfig {
encrypt_at_rest: config.security.encryption.enabled,
encryption_algorithm: config.security.encryption.algorithm.clone(),
key_derivation: config.security.encryption.key_derivation.clone(),
..Default::default()
});
store.init_encryption("config_test_password").unwrap();
let db_cred = CredentialBuilder::new()
.database(
"config_user",
"config_pass",
"config.db.com",
5432,
"config_db",
)
.build()
.unwrap();
store.store_credential("config_test", db_cred).unwrap();
assert!(store.has_credential("config_test"));
}
#[test]
fn test_config_file_discovery() {
let paths = get_config_file_paths();
assert!(paths.iter().any(|p| p.file_name().unwrap() == "pandrs.yml"));
assert!(paths
.iter()
.any(|p| p.file_name().unwrap() == "pandrs.yaml"));
assert!(paths
.iter()
.any(|p| p.file_name().unwrap() == "pandrs.toml"));
assert!(paths.len() > 3);
}
#[test]
fn test_sample_config_creation() {
let dir = tempdir().unwrap();
let sample_path = dir.path().join("sample_config.yml");
create_sample_config(&sample_path).unwrap();
assert!(sample_path.exists());
let sample_config = load_from_file(&sample_path).unwrap();
assert!(validate_config(&sample_config).is_ok());
assert!(sample_config.database.pool.max_connections > 0);
assert!(sample_config.cloud.global.timeout > 0);
}
#[test]
fn test_error_handling() {
let result = load_from_file("non_existent_file.yml".as_ref());
assert!(result.is_err());
let invalid_yaml = "invalid: yaml: content: [unclosed";
let result = load_from_yaml(invalid_yaml);
assert!(result.is_err());
let store = CredentialStore::with_defaults();
let credential = CredentialBuilder::new()
.database("user", "pass", "host", 5432, "db")
.build()
.unwrap();
let mut store_mut = store;
let _result = store_mut.store_credential("test", credential);
}