Files
pruefwerk/compliance-agent/tests/live_findings.rs
T
sharang 6d78d2cd1e
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Dashboard (push) Successful in 4m31s
CI / Deploy Agent (push) Successful in 6m17s
feat(scan): findings are saved as each stage ends, and show while the scan runs (#372) (#401)
2026-09-25 19:38:57 +00:00

129 lines
4.3 KiB
Rust

//! Findings saved stage by stage (#372): a finding shows as soon as its stage
//! ends, triage later updates the same row, and a status a person set while
//! the scan ran is kept. Runs against a live Mongo (`TEST_MONGODB_URI`) and
//! skips cleanly without one.
#![allow(clippy::expect_used, clippy::unwrap_used)]
use compliance_agent::database::DatabasePool;
use compliance_agent::pipeline::finding_store::RunFindings;
use compliance_core::models::{Finding, FindingStatus, ScanRun, ScanTrigger, ScanType, Severity};
use mongodb::bson::doc;
const TENANT: &str = "live-findings";
fn finding(fingerprint: &str, title: &str) -> Finding {
Finding::new(
"target-1".into(),
fingerprint.into(),
"semgrep".into(),
ScanType::Sast,
title.into(),
"desc".into(),
Severity::High,
)
}
#[tokio::test]
async fn findings_show_per_stage_and_triage_updates_them_in_place() {
let uri = std::env::var("TEST_MONGODB_URI")
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
let prefix = format!("live_{}", &uuid::Uuid::new_v4().simple().to_string()[..10]);
let Ok(pool) = DatabasePool::connect(&uri, &prefix).await else {
eprintln!("SKIP live_findings: no MongoDB reachable at {uri}");
return;
};
let db = pool.for_tenant_id(TENANT).await.unwrap();
let run = db
.scan_runs()
.insert_one(ScanRun::new("target-1".into(), ScanTrigger::Manual))
.await
.unwrap();
let run_id = run.inserted_id.as_object_id().unwrap().to_hex();
// A finding an earlier run stored: never saved twice.
db.findings()
.insert_one(finding("fp-old", "seen before"))
.await
.unwrap();
let mut store = RunFindings::new(&run_id);
let mut all = vec![
finding("fp-sast", "strcpy"),
finding("fp-old", "seen before"),
];
// Act 1: SAST ends. Its new finding is visible at once, and counted.
assert_eq!(store.save_new(&db, &mut all).await.unwrap(), 1);
let saved = db
.findings()
.find_one(doc! { "fingerprint": "fp-sast" })
.await
.unwrap()
.expect("visible while the scan runs");
assert_eq!(saved.scan_run_id.as_deref(), Some(run_id.as_str()));
let counted = db
.scan_runs()
.find_one(doc! { "_id": run.inserted_id.clone() })
.await
.unwrap()
.unwrap();
assert_eq!(counted.new_findings_count, 1);
// A person marks it while the scan still runs; the same stage output
// again (a later stage) saves nothing twice.
db.findings()
.update_one(
doc! { "fingerprint": "fp-sast" },
doc! { "$set": { "status": "accepted_risk" } },
)
.await
.unwrap();
assert_eq!(store.save_new(&db, &mut all).await.unwrap(), 0);
// Act 2: triage refutes it and adds a rationale; a grounded check adds one.
all[0].status = FindingStatus::NeedsReview;
all[0].triage_rationale = Some("vendor-style bounded copy".into());
all[0].control_refs = vec!["cra-ai-20".into()];
let mut grounded = finding("fp-grounded", "no update signature");
grounded.status = FindingStatus::NeedsReview;
all.push(grounded);
let new_findings = store.write_back(&db, &mut all).await.unwrap();
// Assert: triage landed on the row, the person's status stayed.
let row = db
.findings()
.find_one(doc! { "fingerprint": "fp-sast" })
.await
.unwrap()
.unwrap();
assert_eq!(row.status, FindingStatus::AcceptedRisk);
assert_eq!(
row.triage_rationale.as_deref(),
Some("vendor-style bounded copy")
);
assert_eq!(row.control_refs, ["cra-ai-20"]);
// Untouched by a person, triage's status lands.
let grounded = db
.findings()
.find_one(doc! { "fingerprint": "fp-grounded" })
.await
.unwrap()
.unwrap();
assert_eq!(grounded.status, FindingStatus::NeedsReview);
assert_eq!(store.new_count(), 2);
assert_eq!(
new_findings.len(),
2,
"sast + grounded, not the earlier one"
);
assert_eq!(
db.findings()
.count_documents(doc! { "fingerprint": "fp-old" })
.await
.unwrap(),
1
);
pool.drop_tenant(TENANT).await.ok();
}