//! The evidence pack of a target, served as a ZIP (#285). //! //! Drives the real API against a live Mongo (`TEST_MONGODB_URI`). #![allow(clippy::expect_used, clippy::unwrap_used)] mod common; use std::io::Read; use common::TestServer; #[tokio::test] async fn a_target_s_evidence_pack_holds_its_findings_and_hashes() { let server = TestServer::start().await; // Arrange: a target with one finding from a penetration test. let created: serde_json::Value = server .post( "/api/v1/targets", &serde_json::json!({ "name": "gateway", "target_type": "firmware_rtos" }), ) .await .json() .await .unwrap(); let id = created["data"]["_id"]["$oid"].as_str().unwrap().to_string(); let added = server .post( &format!("/api/v1/targets/{id}/findings"), &serde_json::json!({ "kind": "pentest", "title": "Telnet with a default password", "severity": "critical", "cwe": "798", "location": "tcp://192.0.2.10:23", }), ) .await; assert_eq!(added.status().as_u16(), 201); // Act let resp = server .get(&format!("/api/v1/targets/{id}/evidence-pack")) .await; let missing = server .get("/api/v1/targets/000000000000000000000000/evidence-pack") .await; // Assert assert_eq!(resp.status().as_u16(), 200); assert_eq!( resp.headers()["content-type"].to_str().unwrap(), "application/zip" ); let bytes = resp.bytes().await.unwrap(); let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes.to_vec())).unwrap(); let mut read = |name: &str| { let mut text = String::new(); zip.by_name(name) .unwrap_or_else(|_| panic!("{name} in the pack")) .read_to_string(&mut text) .unwrap(); text }; let manifest: serde_json::Value = serde_json::from_str(&read("manifest.json")).unwrap(); assert_eq!(manifest["target"]["name"], "gateway"); assert_eq!(manifest["counts"]["findings"], 1); let oscal = read("oscal-assessment-results.json"); assert!( oscal.contains("cra-ai-8"), "the finding's CRA control is assessed" ); assert!(read("findings.csv").contains("tcp://192.0.2.10:23")); assert_eq!(missing.status().as_u16(), 404); // The compliance view: every control listed; the credential finding // violates cra-ai-8; never scanned, so nothing else counts as passed. let view: serde_json::Value = server .get(&format!("/api/v1/targets/{id}/compliance")) .await .json() .await .unwrap(); let controls = view["data"]["controls"].as_array().unwrap(); assert_eq!(controls.len(), 40); let cra8 = controls .iter() .find(|c| c["control"] == "cra-ai-8") .unwrap(); assert_eq!(cra8["state"], "not_satisfied"); assert_eq!(cra8["findings"][0]["location"], "tcp://192.0.2.10:23"); assert_eq!(view["data"]["counts"]["no_violation_found"], 0); assert_eq!(view["data"]["counts"]["not_code_checkable"], 19); // The overview's summary: the same counts, for every target at once. let summary: serde_json::Value = server .get("/api/v1/compliance/summary") .await .json() .await .unwrap(); let rows = summary["data"].as_array().unwrap(); assert_eq!(rows.len(), 1); assert_eq!(rows[0]["target_id"], id.as_str()); assert_eq!(rows[0]["counts"], view["data"]["counts"]); server.cleanup().await; }