#[cfg(test)]
mod auth_tests {
use crate::auth::{
hash_password_secure, verify_password_secure,
InMemoryUserStore, AuthManager, AuthConfig
};
use std::collections::HashMap;
use std::sync::Arc;
use parking_lot::RwLock;
#[test]
fn test_secure_password_hashing() {
let password = "test123";
let hash = hash_password_secure(password).unwrap();
assert!(verify_password_secure(password, &hash).unwrap());
assert!(!verify_password_secure("wrong", &hash).unwrap());
let hash2 = hash_password_secure(password).unwrap();
assert_ne!(hash, hash2);
println!("✅ Secure password hashing test passed");
}
#[test]
fn test_user_store_creation() {
let store = InMemoryUserStore::new();
assert!(store.users.read().is_empty());
let store_with_admin = InMemoryUserStore::with_default_admin();
assert_eq!(store_with_admin.users.read().len(), 1);
let admin = store_with_admin.users.read().get("admin").unwrap();
assert_eq!(admin.username, "admin");
assert!(verify_password_secure("admin123", &admin.password_hash).unwrap());
println!("✅ User store creation test passed");
}
#[test]
fn test_account_lockout() {
let config = AuthConfig::default();
let auth_manager = AuthManager::new(config).unwrap();
let user_store = InMemoryUserStore::new();
let password_hash = hash_password_secure("password123").unwrap();
let mut users = user_store.users.write();
users.insert("testuser".to_string(), crate::auth::UserRecord {
id: "test_user".to_string(),
username: "testuser".to_string(),
password_hash,
email: Some("test@example.com".to_string()),
roles: vec!["user".to_string()],
tenant_id: None,
failed_login_attempts: 0,
locked_until: None,
});
drop(users);
let result = auth_manager.authenticate("testuser", "password123", "127.0.0.1", "Mozilla/5.0");
assert!(result.await.is_ok());
println!("✅ Account lockout test passed");
}
#[test]
fn test_argon2id_security() {
let password = "secure_password_123!";
let hash = hash_password_secure(password).unwrap();
assert!(hash.starts_with("$argon2id$"));
assert!(hash.len() > 50); assert!(hash.contains('$'));
println!("✅ Argon2id security test passed");
}
}