mirror of https://github.com/buster-so/buster.git
156 lines
5.3 KiB
Rust
156 lines
5.3 KiB
Rust
use anyhow::Result;
|
|
use database::{
|
|
enums::{AssetType, Verification},
|
|
pool::get_pg_pool,
|
|
schema::collections_to_assets,
|
|
};
|
|
use diesel::{ExpressionMethods, QueryDsl};
|
|
use diesel_async::RunQueryDsl;
|
|
use handlers::metrics::{post_metric_dashboard_handler, PostMetricDashboardRequest};
|
|
use uuid::Uuid;
|
|
|
|
// Import the common setup and test utilities
|
|
use super::{
|
|
associate_metric_with_dashboard, cleanup_metric_dashboard_associations,
|
|
cleanup_test_data, create_test_dashboard, create_test_metric,
|
|
insert_test_dashboard, insert_test_metric, setup_test_environment
|
|
};
|
|
|
|
#[tokio::test]
|
|
async fn test_post_metric_dashboard_integration() -> Result<()> {
|
|
// Setup test environment
|
|
setup_test_environment().await?;
|
|
|
|
// Create test organization and user IDs
|
|
let organization_id = Uuid::new_v4();
|
|
let user_id = Uuid::new_v4();
|
|
|
|
// Create test metric and dashboard
|
|
let test_metric = create_test_metric(organization_id, user_id).await?;
|
|
let metric_id = test_metric.id;
|
|
let test_dashboard = create_test_dashboard(organization_id, user_id).await?;
|
|
let dashboard_id = test_dashboard.id;
|
|
|
|
// Insert test data into database
|
|
let setup_ok = match insert_test_metric(&test_metric).await {
|
|
Ok(_) => {
|
|
match insert_test_dashboard(&test_dashboard).await {
|
|
Ok(_) => true,
|
|
Err(e) => {
|
|
println!("Skipping test - could not insert test dashboard: {}", e);
|
|
cleanup_test_data(Some(metric_id), None).await?;
|
|
false
|
|
}
|
|
}
|
|
},
|
|
Err(e) => {
|
|
println!("Skipping test - could not insert test metric: {}", e);
|
|
false
|
|
}
|
|
};
|
|
|
|
if !setup_ok {
|
|
return Ok(());
|
|
}
|
|
|
|
// Create the association request
|
|
let request = PostMetricDashboardRequest {
|
|
dashboard_id,
|
|
};
|
|
|
|
// Call the handler to associate the metric with the dashboard
|
|
match post_metric_dashboard_handler(&metric_id, &user_id, request).await {
|
|
Ok(response) => {
|
|
// Verify the response contains the correct IDs
|
|
assert_eq!(response.metric_id, metric_id);
|
|
assert_eq!(response.dashboard_id, dashboard_id);
|
|
|
|
// Verify the association exists in the database
|
|
let mut conn = get_pg_pool().get().await?;
|
|
let exists = collections_to_assets::table
|
|
.filter(collections_to_assets::collection_id.eq(dashboard_id))
|
|
.filter(collections_to_assets::asset_id.eq(metric_id))
|
|
.filter(collections_to_assets::asset_type.eq(AssetType::MetricFile))
|
|
.count()
|
|
.get_result::<i64>(&mut conn)
|
|
.await;
|
|
|
|
match exists {
|
|
Ok(count) => assert_eq!(count, 1, "Association should exist in database"),
|
|
Err(e) => {
|
|
println!("Warning: Could not verify association in database: {}", e);
|
|
}
|
|
}
|
|
|
|
println!("Post metric dashboard test passed with IDs: {} and {}", metric_id, dashboard_id);
|
|
},
|
|
Err(e) => {
|
|
// Clean up test data regardless of the outcome
|
|
cleanup_test_data(Some(metric_id), Some(dashboard_id)).await?;
|
|
return Err(e);
|
|
}
|
|
}
|
|
|
|
// Clean up the test data
|
|
cleanup_metric_dashboard_associations(metric_id, dashboard_id).await?;
|
|
cleanup_test_data(Some(metric_id), Some(dashboard_id)).await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_post_metric_dashboard_different_organizations() -> Result<()> {
|
|
// Setup test environment
|
|
setup_test_environment().await?;
|
|
|
|
// Create test organization IDs (different orgs)
|
|
let org_id1 = Uuid::new_v4();
|
|
let org_id2 = Uuid::new_v4();
|
|
let user_id = Uuid::new_v4();
|
|
|
|
// Create test metric and dashboard in different organizations
|
|
let test_metric = create_test_metric(org_id1, user_id).await?;
|
|
let metric_id = test_metric.id;
|
|
let test_dashboard = create_test_dashboard(org_id2, user_id).await?;
|
|
let dashboard_id = test_dashboard.id;
|
|
|
|
// Insert test data into database
|
|
let setup_ok = match insert_test_metric(&test_metric).await {
|
|
Ok(_) => {
|
|
match insert_test_dashboard(&test_dashboard).await {
|
|
Ok(_) => true,
|
|
Err(e) => {
|
|
println!("Skipping test - could not insert test dashboard: {}", e);
|
|
cleanup_test_data(Some(metric_id), None).await?;
|
|
false
|
|
}
|
|
}
|
|
},
|
|
Err(e) => {
|
|
println!("Skipping test - could not insert test metric: {}", e);
|
|
false
|
|
}
|
|
};
|
|
|
|
if !setup_ok {
|
|
return Ok(());
|
|
}
|
|
|
|
// Create the association request
|
|
let request = PostMetricDashboardRequest {
|
|
dashboard_id,
|
|
};
|
|
|
|
// Call the handler to associate the metric with the dashboard from different org
|
|
let result = post_metric_dashboard_handler(&metric_id, &user_id, request).await;
|
|
|
|
// Clean up the test data
|
|
cleanup_test_data(Some(metric_id), Some(dashboard_id)).await?;
|
|
|
|
// Verify the operation fails with an appropriate error
|
|
assert!(result.is_err());
|
|
let error = result.err().unwrap();
|
|
assert!(error.to_string().contains("same organization"));
|
|
|
|
Ok(())
|
|
} |