Files
pruefwerk/compliance-agent/tests/integration/api/releases.rs
T
sharang c8a13fecad
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Dashboard (push) Skipped
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m11s
CI / Deploy Agent (push) Successful in 6m59s
feat(releases): release a target to a connected workspace and publish versions (#1028) (#1063)
2026-11-19 12:35:08 +00:00

144 lines
4.8 KiB
Rust

use crate::common::TestServer;
use compliance_core::models::{SbomEntry, VulnRef};
use serde_json::json;
/// A target's id, created through the API.
async fn target(server: &TestServer, name: &str) -> String {
let resp = server
.post(
"/api/v1/targets",
&json!({ "name": name, "target_type": "firmware_rtos" }),
)
.await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
body["data"]["_id"]["$oid"].as_str().unwrap().to_string()
}
#[tokio::test]
async fn a_supplier_publishes_versions_of_a_stripped_sbom() {
let server = TestServer::start().await;
let id = target(&server, "DR-4 Firmware").await;
let publish = |label: &str| {
let (server, id, label) = (&server, id.clone(), label.to_string());
async move {
server
.post(
&format!("/api/v1/targets/{id}/versions"),
&json!({ "label": label }),
)
.await
}
};
// Nothing to publish before a scan found components.
let resp = publish("1.4.0").await;
assert_eq!(resp.status(), 422);
assert_eq!(
resp.json::<serde_json::Value>().await.unwrap()["error"],
"empty"
);
let mongodb_uri = std::env::var("TEST_MONGODB_URI")
.unwrap_or_else(|_| "mongodb://root:example@localhost:27017/?authSource=admin".into());
let client = mongodb::Client::with_uri_str(&mongodb_uri).await.unwrap();
let db = client.database(&server.tenant_db_name());
let mut lwip = SbomEntry::new(id.clone(), "lwip".into(), "2.1.2".into(), "generic".into());
lwip.purl = Some("pkg:generic/lwip@2.1.2".into());
lwip.known_vulnerabilities = vec![VulnRef {
id: "CVE-2026-0001".into(),
source: "osv".into(),
severity: Some("HIGH".into()),
url: None,
exploitation: None,
aliases: Vec::new(),
}];
let mbedtls = SbomEntry::new(
id.clone(),
"mbedtls".into(),
"3.6.2".into(),
"generic".into(),
);
db.collection::<SbomEntry>("sbom_entries")
.insert_many([lwip, mbedtls])
.await
.unwrap();
let resp = publish(" 1.4.0 ").await;
assert_eq!(resp.status(), 201);
let body: serde_json::Value = resp.json().await.unwrap();
assert_eq!(body["data"]["label"], "1.4.0");
assert_eq!(body["data"]["components"], 2);
let resp = publish("1.4.0").await;
assert_eq!(resp.status(), 409);
assert_eq!(
resp.json::<serde_json::Value>().await.unwrap()["error"],
"label_taken"
);
assert_eq!(publish(" ").await.status(), 422);
// The stored document is the stripped one.
let stored = db
.collection::<mongodb::bson::Document>("sbom_versions")
.find_one(mongodb::bson::doc! { "label": "1.4.0" })
.await
.unwrap()
.unwrap();
let text = std::fs::read_to_string(stored.get_str("stored_path").unwrap()).unwrap();
let doc: serde_json::Value = serde_json::from_str(&text).unwrap();
assert_eq!(doc["metadata"]["component"]["name"], "DR-4 Firmware");
assert_eq!(doc["metadata"]["component"]["version"], "1.4.0");
assert!(doc["metadata"]["component"]["supplier"]["name"].is_string());
assert!(doc.get("vulnerabilities").is_none());
assert!(!text.contains("CVE-2026-0001"), "no vulnerability crosses");
// A preview stores nothing.
let resp = server
.get(&format!(
"/api/v1/targets/{id}/versions/preview?label=1.5.0"
))
.await;
assert_eq!(resp.status(), 200);
let preview: serde_json::Value = resp.json().await.unwrap();
assert_eq!(preview["metadata"]["component"]["version"], "1.5.0");
assert_eq!(preview["components"].as_array().unwrap().len(), 2);
let body: serde_json::Value = server
.get(&format!("/api/v1/targets/{id}/releases"))
.await
.json()
.await
.unwrap();
let labels: Vec<&str> = body["data"]["versions"]
.as_array()
.unwrap()
.iter()
.map(|v| v["label"].as_str().unwrap())
.collect();
assert_eq!(labels, ["1.4.0"], "the preview was not published");
assert_eq!(body["data"]["releases"], json!([]));
// Without a registry there is no connection to release over.
let resp = server
.post(
&format!("/api/v1/targets/{id}/releases"),
&json!({ "connection_id": "c-1" }),
)
.await;
assert_eq!(resp.status(), 422);
assert_eq!(
resp.json::<serde_json::Value>().await.unwrap()["error"],
"unknown_connection"
);
assert_eq!(
server
.get("/api/v1/targets/nonsense/releases")
.await
.status(),
404
);
server.cleanup().await;
}