pub mod api;
pub mod auth;
pub mod config;
pub mod server;
pub mod websocket;
pub use config::{AuthConfig, DashboardConfig};
pub use server::WebDashboard;
pub type Result<T> = std::result::Result<T, anyhow::Error>;
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[test]
fn test_dashboard_config_creation() {
let config = DashboardConfig::new()
.with_bind_address("0.0.0.0", 3000)
.with_database_url("postgresql://localhost/test")
.with_cors(true);
assert_eq!(config.bind_addr(), "0.0.0.0:3000");
assert_eq!(config.database_url, "postgresql://localhost/test");
assert!(config.enable_cors);
}
#[test]
fn test_auth_config_security_defaults() {
let auth_config = AuthConfig::default();
assert!(
auth_config.enabled,
"Authentication should be enabled by default"
);
assert_eq!(auth_config.username, "admin");
assert_eq!(auth_config.max_failed_attempts, 5);
assert!(auth_config.lockout_duration.as_secs() > 0);
}
#[tokio::test]
async fn test_dashboard_creation_with_invalid_config() {
let temp_dir = tempdir().unwrap();
let config = DashboardConfig {
database_url: "invalid://url".to_string(),
static_dir: temp_dir.path().to_path_buf(),
..Default::default()
};
let result = WebDashboard::new(config).await;
assert!(
result.is_ok(),
"Dashboard creation should succeed, connection validation happens later"
);
}
#[test]
fn test_config_builder_pattern() {
let temp_dir = tempdir().unwrap();
let config = DashboardConfig::new()
.with_bind_address("192.168.1.1", 8888)
.with_database_url("mysql://root:pass@localhost/db")
.with_static_dir(temp_dir.path().to_path_buf())
.with_auth("user", "hash")
.with_cors(false);
assert_eq!(config.bind_address, "192.168.1.1");
assert_eq!(config.port, 8888);
assert_eq!(config.database_url, "mysql://root:pass@localhost/db");
assert!(config.auth.enabled);
assert_eq!(config.auth.username, "user");
assert!(!config.enable_cors);
}
}