Files
pruefwerk/compliance-agent/tests/integration/api/machine.rs
T
Sharang ParnerkarandClaude Opus 5.5 87d8386277
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 5m9s
CI / Check (pull_request) Successful in 21m17s
feat(sbom): a machine's SBOM, its parts with their suppliers (#1026)
A machine is a registry project of kind `product`, made of its targets:
in-house ones and components bought in (#1025). GET
/api/v1/projects/{id}/sbom returns its CycloneDX 1.6 SBOM: the machine as
a device, each part with its supplier (the workspace for one made
in-house) and the supplier's version, the parts' components once each,
and a graph of the machine depending on its parts and each part on its
own components. Part refs are built from target ids, so names never
collide. The supplier's product entry is the part, not a component of
it; archived targets are left out.

The vulnerability listing of the target SBOM moves into its own helper,
shared by both; the CycloneDX schema test helpers move to tests/common.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Q9inRcu428A2iNjDHPEAT9
2026-11-17 10:48:09 +01:00

106 lines
4.1 KiB
Rust

use crate::common::TestServer;
use compliance_core::models::SbomEntry;
use serde_json::json;
/// Create a target named `name`, with `supplier` when it is bought in, and
/// return its id.
async fn target(server: &TestServer, name: &str, supplier: Option<serde_json::Value>) -> String {
let resp = server
.post(
"/api/v1/targets",
&json!({ "name": name, "target_type": "firmware_rtos", "supplier": supplier }),
)
.await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
body["data"]["_id"]["$oid"].as_str().unwrap().to_string()
}
/// An SBOM entry of target `target`.
fn entry(target: &str, name: &str, manager: &str, purl: Option<&str>) -> SbomEntry {
let mut e = SbomEntry::new(target.into(), name.into(), "1.0".into(), manager.into());
e.purl = purl.map(String::from);
e
}
#[tokio::test]
async fn a_machines_sbom_lists_its_parts_with_their_suppliers_and_components() {
let server = TestServer::start().await;
let controller = target(&server, "Steuerung", None).await;
let drive = target(
&server,
"Antrieb",
Some(json!({ "name": "Example Drives GmbH", "product": "Drive Control", "version": "4.2.1" })),
)
.await;
let retired = target(&server, "Alte Steuerung", None).await;
let elsewhere = target(&server, "Andere Maschine", None).await;
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());
// Projects are registry-owned, so assign the targets directly.
let targets = db.collection::<mongodb::bson::Document>("onboarded_targets");
for id in [&controller, &drive, &retired] {
let oid = mongodb::bson::oid::ObjectId::parse_str(id).unwrap();
let mut set = mongodb::bson::doc! { "project_ids": ["m1"] };
if id == &retired {
set.insert("archived_at", mongodb::bson::DateTime::now());
}
targets
.update_one(
mongodb::bson::doc! { "_id": oid },
mongodb::bson::doc! { "$set": set },
)
.await
.unwrap();
}
db.collection::<SbomEntry>("sbom_entries")
.insert_many([
entry(&controller, "lodash", "npm", Some("pkg:npm/lodash@1.0")),
entry(&drive, "lodash", "npm", Some("pkg:npm/lodash@1.0")),
entry(&drive, "vendor-blob", "supplier", None),
// The bought-in product itself, as a scan stores it.
entry(&drive, "Drive Control", "supplier-product", None),
entry(
&retired,
"openssl",
"generic",
Some("pkg:generic/openssl@1.0"),
),
entry(&elsewhere, "zlib", "generic", Some("pkg:generic/zlib@1.0")),
])
.await
.unwrap();
let resp = server.get("/api/v1/projects/m1/sbom").await;
assert_eq!(resp.status(), 200);
let doc: serde_json::Value = resp.json().await.unwrap();
assert_eq!(doc["metadata"]["component"]["type"], "device");
let components = doc["components"].as_array().unwrap();
let names: Vec<&str> = components
.iter()
.map(|c| c["name"].as_str().unwrap())
.collect();
// The parts by name, then their components once each; nothing of the
// archived target or another machine, and the product is the part.
assert_eq!(names, ["Antrieb", "Steuerung", "lodash", "vendor-blob"]);
assert_eq!(components[0]["supplier"]["name"], "Example Drives GmbH");
assert_eq!(components[0]["version"], "4.2.1");
// The dev tenant's workspace makes the controller.
assert_eq!(components[1]["supplier"]["name"], "dev");
server.cleanup().await;
}
#[tokio::test]
async fn a_machine_without_parts_has_no_sbom() {
let server = TestServer::start().await;
let resp = server.get("/api/v1/projects/empty/sbom").await;
assert_eq!(resp.status(), 422);
server.cleanup().await;
}