Files
pruefwerk/compliance-agent/tests/integration/api/dast.rs
T
sharang f5ac5321bd
CI / Check (push) Skipped
CI / Agent integration (push) Skipped
CI / Detect Changes (push) Successful in 3s
CI / Deploy Docs (push) Successful in 1m15s
CI / Deploy MCP (push) Successful in 5m14s
CI / Deploy Dashboard (push) Successful in 6m48s
CI / Deploy Agent (push) Successful in 9m2s
feat(dast): scans log in with the target's credentials (#1000)
2026-11-16 02:31:38 +00:00

124 lines
3.9 KiB
Rust

use crate::common::TestServer;
use serde_json::json;
#[tokio::test]
async fn add_and_list_dast_targets() {
let server = TestServer::start().await;
// Initially empty
let resp = server.get("/api/v1/dast/targets").await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
assert_eq!(body["data"].as_array().unwrap().len(), 0);
// Add a target
let resp = server
.post(
"/api/v1/dast/targets",
&json!({
"name": "test-app",
"base_url": "https://test-app.example.com",
"target_type": "web_app",
}),
)
.await;
assert_eq!(resp.status(), 200);
// List should return 1
let resp = server.get("/api/v1/dast/targets").await;
let body: serde_json::Value = resp.json().await.unwrap();
let targets = body["data"].as_array().unwrap();
assert_eq!(targets.len(), 1);
assert_eq!(targets[0]["name"], "test-app");
assert_eq!(targets[0]["base_url"], "https://test-app.example.com");
server.cleanup().await;
}
#[tokio::test]
async fn list_dast_findings_empty() {
let server = TestServer::start().await;
let resp = server.get("/api/v1/dast/findings").await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
assert_eq!(body["data"].as_array().unwrap().len(), 0);
server.cleanup().await;
}
#[tokio::test]
async fn a_targets_login_is_stored_sealed_and_never_returned() {
let server = TestServer::start().await;
let resp = server
.post(
"/api/v1/dast/targets",
&json!({
"name": "members",
"base_url": "https://members.example.com",
"auth": {
"method": "bearer",
"token": "tok-secret",
"headers": { "X-Api-Key": "key-secret" },
},
}),
)
.await;
assert_eq!(resp.status(), 200);
let body: serde_json::Value = resp.json().await.unwrap();
let id = body["data"]["_id"]["$oid"].as_str().expect("the new id");
let returned = body.to_string();
assert!(
!returned.contains("tok-secret") && !returned.contains("key-secret"),
"{returned}"
);
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 stored = client
.database(&server.tenant_db_name())
.collection::<mongodb::bson::Document>("dast_targets")
.find_one(mongodb::bson::doc! {
"_id": mongodb::bson::oid::ObjectId::parse_str(id).unwrap()
})
.await
.unwrap()
.expect("stored under the returned id");
let auth = stored.get_document("auth_config").unwrap();
let token = auth.get_str("token").unwrap();
let header = auth
.get_document("headers")
.unwrap()
.get_str("X-Api-Key")
.unwrap();
assert!(token != "tok-secret" && header != "key-secret", "{auth}");
server.cleanup().await;
}
#[tokio::test]
async fn a_login_the_scan_cannot_send_is_refused() {
let server = TestServer::start().await;
for auth in [
json!({ "method": "form", "login_url": "https://members.example.com/login" }),
json!({ "method": "bearer" }),
json!({ "method": "ntlm", "token": "t" }),
] {
let resp = server
.post(
"/api/v1/dast/targets",
&json!({ "name": "m", "base_url": "https://members.example.com", "auth": auth }),
)
.await;
assert_eq!(resp.status(), 400, "{auth}");
}
let resp = server.get("/api/v1/dast/targets").await;
let body: serde_json::Value = resp.json().await.unwrap();
assert!(body["data"].as_array().unwrap().is_empty());
server.cleanup().await;
}