Files
projectmycelium/tests/tests_archive/ssh_key_integration_test.rs
2025-09-01 21:37:01 -04:00

135 lines
6.2 KiB
Rust

use std::fs;
/// SSH Key Management Integration Test
///
/// Tests the actual SSH key functionality using the persistent data system
/// This test validates:
/// - SSH key creation ✅ (should work)
/// - SSH key service validation ✅ (should work)
/// - Button functionality issues ❌ (JavaScript/frontend issues we need to fix)
#[tokio::test]
async fn test_ssh_key_service_functionality() {
println!("🔧 Testing Real SSH Key Service Functionality");
// Initialize logger
env_logger::builder()
.filter_level(log::LevelFilter::Info)
.is_test(true)
.try_init()
.ok();
// Test user
let test_user_email = "ssh_test@example.com";
// Clean up any existing test data
cleanup_test_user_data(test_user_email);
// Test 1: ✅ SSH Key Service Creation (Should Work)
println!("🔧 Testing SSH Key Service Creation (Expected: ✅ SUCCESS)");
let ssh_service_result = threefold_marketplace::services::ssh_key_service::SSHKeyService::builder().build();
assert!(ssh_service_result.is_ok(), "SSH Key Service should build successfully");
let ssh_service = ssh_service_result.unwrap();
println!("✅ SSH Key Service: Created successfully");
// Test 2: ✅ Valid SSH Key Addition (Should Work)
println!("🔧 Testing Valid SSH Key Addition (Expected: ✅ SUCCESS)");
let valid_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKQ4Iz1Pj5PjRrxeL5LfFnGe3w9vNNjc+FW7gX6H5sAB test@example.com";
let add_result = ssh_service.add_ssh_key(test_user_email, "Test Key", valid_key, false);
assert!(add_result.is_ok(), "Valid SSH key should be added successfully");
let ssh_key = add_result.unwrap();
println!("✅ SSH Key Addition: SUCCESS (ID: {})", ssh_key.id);
// Test 3: ✅ SSH Key Listing (Should Work)
println!("🔧 Testing SSH Key Listing (Expected: ✅ SUCCESS)");
let keys_list = ssh_service.get_user_ssh_keys(test_user_email);
assert_eq!(keys_list.len(), 1, "Should have 1 SSH key");
println!("✅ SSH Key Listing: SUCCESS (Found {} keys)", keys_list.len());
// Test 4: ✅ SSH Key Validation (Should Work)
println!("🔧 Testing SSH Key Validation (Expected: ✅ SUCCESS)");
let invalid_key = "invalid-ssh-key-format";
let invalid_result = ssh_service.add_ssh_key(test_user_email, "Invalid Key", invalid_key, false);
assert!(invalid_result.is_err(), "Invalid SSH key should be rejected");
println!("✅ SSH Key Validation: SUCCESS (Invalid key rejected)");
// Test 5: ✅ Set Default Functionality (Backend Should Work)
println!("🔧 Testing Set Default Backend Functionality (Expected: ✅ SUCCESS)");
let set_default_result = ssh_service.set_default_ssh_key(test_user_email, &ssh_key.id);
assert!(set_default_result.is_ok(), "Set default should work in backend");
println!("✅ Set Default Backend: SUCCESS");
// Test 6: ✅ Update SSH Key Functionality (Backend Should Work)
println!("🔧 Testing Update SSH Key Backend Functionality (Expected: ✅ SUCCESS)");
let update_result = ssh_service.update_ssh_key(test_user_email, &ssh_key.id, Some("Updated Test Key"), Some(true));
assert!(update_result.is_ok(), "Update should work in backend");
println!("✅ Update SSH Key Backend: SUCCESS");
// Test 7: ✅ Delete SSH Key Functionality (Backend Should Work)
println!("🔧 Testing Delete SSH Key Backend Functionality (Expected: ✅ SUCCESS)");
let delete_result = ssh_service.delete_ssh_key(test_user_email, &ssh_key.id);
assert!(delete_result.is_ok(), "Delete should work in backend");
println!("✅ Delete SSH Key Backend: SUCCESS");
// Verify deletion
let keys_after_delete = ssh_service.get_user_ssh_keys(test_user_email);
assert_eq!(keys_after_delete.len(), 0, "Should have 0 SSH keys after deletion");
println!("✅ Delete Verification: SUCCESS (0 keys remaining)");
// Clean up test data
cleanup_test_user_data(test_user_email);
println!("🎯 BACKEND FUNCTIONALITY TEST SUMMARY:");
println!("✅ SSH Key Service Creation: WORKING");
println!("✅ SSH Key Addition: WORKING");
println!("✅ SSH Key Listing: WORKING");
println!("✅ SSH Key Validation: WORKING");
println!("✅ Set Default (Backend): WORKING");
println!("✅ Update SSH Key (Backend): WORKING");
println!("✅ Delete SSH Key (Backend): WORKING");
println!("");
println!("❌ FRONTEND ISSUES TO FIX:");
println!("❌ Set Default Button: NOT WORKING (JavaScript/UI issue)");
println!("❌ Edit Button: NOT WORKING (JavaScript/UI issue)");
println!("❌ Delete Button: NOT WORKING (JavaScript/UI issue)");
}
#[tokio::test]
async fn test_ssh_key_persistence() {
println!("🔧 Testing SSH Key Persistence");
let test_user_email = "persistence_test@example.com";
cleanup_test_user_data(test_user_email);
let ssh_service = threefold_marketplace::services::ssh_key_service::SSHKeyService::builder()
.build()
.expect("Should build SSH service");
// Add a key
let key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKQ4Iz1Pj5PjRrxeL5LfFnGe3w9vNNjc+FW7gX6H5sAB persistence@test.com";
let result = ssh_service.add_ssh_key(test_user_email, "Persistence Test", key, true);
assert!(result.is_ok(), "Should add SSH key successfully");
// Verify the key is persisted by creating a new service instance
let new_ssh_service = threefold_marketplace::services::ssh_key_service::SSHKeyService::builder()
.build()
.expect("Should build new SSH service");
let keys = new_ssh_service.get_user_ssh_keys(test_user_email);
assert_eq!(keys.len(), 1, "Should persist SSH key");
assert_eq!(keys[0].name, "Persistence Test", "Should persist SSH key name");
assert!(keys[0].is_default, "Should persist default status");
println!("✅ SSH Key Persistence: WORKING");
cleanup_test_user_data(test_user_email);
}
fn cleanup_test_user_data(user_email: &str) {
let encoded_email = user_email.replace("@", "_at_").replace(".", "_");
let user_data_path = format!("user_data/{}.json", encoded_email);
if std::path::Path::new(&user_data_path).exists() {
let _ = fs::remove_file(&user_data_path);
println!("🧹 Cleaned up test user data: {}", user_data_path);
}
}