CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy MCP (push) Skipped
CI / Deploy Docs (push) Successful in 1m10s
CI / Deploy Dashboard (push) Successful in 3m53s
CI / Deploy Agent (push) Successful in 7m21s
168 lines
6.3 KiB
Rust
168 lines
6.3 KiB
Rust
use crate::common::TestServer;
|
|
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()
|
|
}
|
|
|
|
fn alert(target: &str, cve: &str, severity: &str) -> compliance_core::models::CveAlert {
|
|
serde_json::from_value(json!({
|
|
"cve_id": cve, "repo_id": target, "affected_package": "lodash",
|
|
"affected_version": "1.0", "source": "osv", "severity": severity,
|
|
"cvss_score": null, "summary": null, "llm_impact_summary": null,
|
|
"references": [], "created_at": "2026-11-16T08:00:00Z",
|
|
}))
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_supply_chain_links_suppliers_components_and_products() {
|
|
let server = TestServer::start().await;
|
|
let drive = target(
|
|
&server,
|
|
"Antrieb",
|
|
Some(json!({ "name": "Example Drives GmbH", "version": "4.2.1" })),
|
|
)
|
|
.await;
|
|
let plc = target(&server, "Steuerung", None).await;
|
|
let retired = target(&server, "Alte Steuerung", 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; without a registry they are named by id.
|
|
let targets = db.collection::<mongodb::bson::Document>("onboarded_targets");
|
|
for (id, projects) in [
|
|
(&drive, vec!["m1"]),
|
|
(&plc, vec!["m1", "m2"]),
|
|
(&retired, vec!["m2"]),
|
|
] {
|
|
let oid = mongodb::bson::oid::ObjectId::parse_str(id).unwrap();
|
|
let mut set = mongodb::bson::doc! { "project_ids": projects };
|
|
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::<compliance_core::models::CveAlert>("cve_alerts")
|
|
.insert_many([
|
|
alert(&drive, "CVE-2026-90001", "critical"),
|
|
alert(&plc, "CVE-2021-23337", "high"),
|
|
])
|
|
.await
|
|
.unwrap();
|
|
|
|
let resp = server.get("/api/v1/supply-chain").await;
|
|
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
let nodes = body["data"]["nodes"].as_array().unwrap();
|
|
let edges = body["data"]["edges"].as_array().unwrap();
|
|
let keys: Vec<&str> = nodes.iter().map(|n| n["key"].as_str().unwrap()).collect();
|
|
assert_eq!(
|
|
keys,
|
|
[
|
|
"supplier:Example Drives GmbH".to_string(),
|
|
format!("target:{drive}"),
|
|
format!("target:{plc}"),
|
|
"project:m1".to_string(),
|
|
"project:m2".to_string(),
|
|
],
|
|
"the archived target is left out"
|
|
);
|
|
let node = |key: &str| nodes.iter().find(|n| n["key"] == key).unwrap();
|
|
assert_eq!(node(&format!("target:{drive}"))["bought"], true);
|
|
assert_eq!(node(&format!("target:{drive}"))["open"]["critical"], 1);
|
|
assert!(node(&format!("target:{plc}")).get("bought").is_none());
|
|
assert_eq!(node("project:m1")["open"]["critical"], 1);
|
|
assert_eq!(node("project:m1")["open"]["high"], 1);
|
|
assert_eq!(node("project:m2")["open"]["high"], 1, "the shared PLC");
|
|
assert_eq!(node("project:m2")["level"], "product");
|
|
let supplies = edges.iter().find(|e| e["kind"] == "supplies").unwrap();
|
|
assert_eq!(supplies["to"], format!("target:{drive}"));
|
|
assert_eq!(supplies["delivery"]["how"], "missing");
|
|
assert_eq!(supplies["delivery"]["version"], "4.2.1");
|
|
let part_of = edges.iter().filter(|e| e["kind"] == "part_of").count();
|
|
assert_eq!(part_of, 3, "drive to m1, PLC to m1 and m2");
|
|
|
|
// Without a registry a product's box has no documents to show.
|
|
let body: serde_json::Value = server
|
|
.get("/api/v1/supply-chain/projects/m1/documents")
|
|
.await
|
|
.json()
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(body["data"]["documents"], json!([]));
|
|
assert_eq!(body["data"]["store_path"], "");
|
|
|
|
server.cleanup().await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn the_arrangement_is_kept_until_it_is_reset() {
|
|
let server = TestServer::start().await;
|
|
let layout = "/api/v1/supply-chain/layout";
|
|
|
|
let resp = server.get(layout).await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = resp.json().await.unwrap();
|
|
assert_eq!(body["data"]["positions"], json!([]));
|
|
assert!(body["data"]["updated_at"].is_null());
|
|
|
|
let saved = json!({ "positions": [
|
|
{ "key": "supplier:Beispiel GmbH & Co. KG", "x": 40, "y": 60 },
|
|
{ "key": "target:a", "x": 400, "y": 60 },
|
|
{ "key": "target:a", "x": 420, "y": 80 },
|
|
] });
|
|
let resp = server.put(layout, &saved).await;
|
|
assert_eq!(resp.status(), 200);
|
|
|
|
let body: serde_json::Value = server.get(layout).await.json().await.unwrap();
|
|
assert_eq!(
|
|
body["data"]["positions"],
|
|
json!([
|
|
{ "key": "supplier:Beispiel GmbH & Co. KG", "x": 40, "y": 60 },
|
|
{ "key": "target:a", "x": 420, "y": 80 },
|
|
]),
|
|
"a name with a dot is kept, a key once with its last position"
|
|
);
|
|
assert!(body["data"]["updated_at"].is_string());
|
|
|
|
let resp = server
|
|
.put(
|
|
layout,
|
|
&json!({ "positions": [{ "key": " ", "x": 0, "y": 0 }] }),
|
|
)
|
|
.await;
|
|
assert_eq!(resp.status(), 422);
|
|
let body: serde_json::Value = server.get(layout).await.json().await.unwrap();
|
|
assert_eq!(
|
|
body["data"]["positions"].as_array().unwrap().len(),
|
|
2,
|
|
"a refused save changes nothing"
|
|
);
|
|
|
|
let resp = server.delete(layout).await;
|
|
assert_eq!(resp.status(), 200);
|
|
let body: serde_json::Value = server.get(layout).await.json().await.unwrap();
|
|
assert_eq!(body["data"]["positions"], json!([]));
|
|
|
|
server.cleanup().await;
|
|
}
|