#[cfg(test)]
mod hierarchy_feature_tests {
use crate::authorization_enhanced::service::{AuthorizationConfig, AuthorizationService};
use role_system::{Permission, Role};
#[tokio::test]
async fn test_role_hierarchy_features() {
println!("๐งช Testing new role-system v1.1.1 hierarchy features");
let service = AuthorizationService::with_config(AuthorizationConfig::default())
.await
.expect("Failed to create authorization service");
let guest_role = Role::new("guest").with_description("Guest user role");
let user_role = Role::new("user").with_description("Regular user role");
let admin_role = Role::new("admin").with_description("Administrator role");
service
.role_system
.register_role(guest_role.clone())
.await
.unwrap();
service
.role_system
.register_role(user_role.clone())
.await
.unwrap();
service
.role_system
.register_role(admin_role.clone())
.await
.unwrap();
service
.role_system
.add_role_inheritance("user", "guest")
.await
.unwrap();
service
.role_system
.add_role_inheritance("admin", "user")
.await
.unwrap();
println!("โ
Testing Role hierarchy methods:");
if let Ok(Some(admin)) = service.role_system.get_role("admin").await {
if let Some(parent_id) = admin.parent_role_id() {
println!(" ๐ Admin parent role: {}", parent_id);
assert_eq!(parent_id, "user");
}
let depth = admin.hierarchy_depth();
let is_root = admin.is_root_role();
let is_leaf = admin.is_leaf_role();
let children = admin.child_role_ids();
println!(" ๐ Admin role metadata:");
println!(" - Depth: {}", depth);
println!(" - Is root: {}", is_root);
println!(" - Is leaf: {}", is_leaf);
println!(" - Children: {:?}", children);
assert_eq!(depth, 2);
assert!(!is_root); assert!(is_leaf); }
println!("โ
Testing AuthorizationService hierarchy methods:");
let hierarchy = service.get_role_hierarchy("admin").await.unwrap();
println!(" ๐ Admin hierarchy: {:?}", hierarchy);
assert!(hierarchy.contains(&"admin".to_string()));
let metadata = service.get_role_metadata("admin").await.unwrap();
println!(" ๐ Admin metadata: {}", metadata);
assert!(metadata.contains("admin"));
assert!(metadata.contains("depth=2"));
println!("๐ All role-system v1.1.1 hierarchy features working correctly!");
}
#[tokio::test]
async fn test_hierarchy_feature_integration_with_api() {
println!("๐ Testing hierarchy features integration with API endpoints");
let service = AuthorizationService::new().await.unwrap();
let manager_role = Role::new("manager").with_description("Manager role");
service
.role_system
.register_role(manager_role)
.await
.unwrap();
if let Ok(Some(role)) = service.role_system.get_role("manager").await {
let parent_id = role.parent_role_id();
println!(" ๐ Manager parent role: {:?}", parent_id);
let is_root = role.is_root_role();
let depth = role.hierarchy_depth();
println!(" ๐ Manager is root: {}, depth: {}", is_root, depth);
}
println!("โ
API integration with hierarchy features confirmed!");
}
}