vuln-aggregator/tests/vulnerable_branch_images_considering_vulnerability.rs
2024-12-19 14:00:22 +03:00

89 lines
2.8 KiB
Rust

use axum::{
body::{to_bytes, Body},
http::{Method, Request, StatusCode},
};
use tower::ServiceExt;
use vuln_aggregator::router::routing::output_image::OutputImages;
#[tokio::test]
async fn returned_200_if_the_branch_and_degree_of_vulnerailities_is_supported() {
let request = Request::builder()
.method(Method::GET)
.uri("/vulnerable_branch_images_considering_vulnerability/sisyphus?degree_vulnerability=NONE")
.body(Body::empty())
.expect("build request error");
let app = vuln_aggregator::router::build::app().await;
let response = app.oneshot(request).await.expect("request execution error");
assert_eq!(response.status(), StatusCode::OK);
}
#[tokio::test]
async fn returned_400_if_the_branch_is_not_supported() {
let request = Request::builder()
.method(Method::GET)
.uri(
"/vulnerable_branch_images_considering_vulnerability/not_supported?degree_vulnerability=NONE",
)
.body(Body::empty())
.expect("build request error");
let app = vuln_aggregator::router::build::app().await;
let response = app.oneshot(request).await.expect("request execution error");
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn returned_400_if_the_degree_of_vulnerabilities_is_not_supported() {
let request = Request::builder()
.method(Method::GET)
.uri("/vulnerable_branch_images_considering_vulnerability/sisyphus?degree_vulnerability=not_supported")
.body(Body::empty())
.expect("build request error");
let app = vuln_aggregator::router::build::app().await;
let response = app.oneshot(request).await.expect("request execution error");
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
}
#[tokio::test]
async fn images_of_only_the_selected_branch_with_the_selected_vulnerability_are_returned() {
let request = Request::builder()
.method(Method::GET)
.uri("/vulnerable_branch_images_considering_vulnerability/sisyphus?degree_vulnerability=CRITICAL")
.body(Body::empty())
.expect("build request error");
let app = vuln_aggregator::router::build::app().await;
let response = app.oneshot(request).await.expect("request execution error");
let body_bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let body_str = String::from_utf8(body_bytes.to_vec()).unwrap();
let output = serde_json::from_str::<OutputImages>(&body_str).unwrap();
for image in output.images() {
if !image.image().to_owned().contains("sisyphus") {
panic!("unexpected branch")
}
if !image
.cve_list()
.as_ref()
.unwrap()
.iter()
.any(|cve| cve.severity() == "CRITICAL")
{
panic!("there is no selected vulnerability")
}
}
}