use axum::body::Body; use axum::http::{Request, StatusCode}; use http_body_util::BodyExt; use serde_json::Value; use tower::ServiceExt; #[tokio::test] async fn openapi_json_describes_all_endpoints() { let response = genenetwork4::app() .oneshot( Request::builder() .uri("/openapi.json") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let doc: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(doc["info"]["title"], "GeneNetwork4 API"); let paths = doc["paths"].as_object().unwrap(); for path in [ "/", "/version", "/species", "/species/{species}", "/groups", "/groups/{group}", "/groups/{group}/strains", "/groups/{group}/traits/{trait_id}", "/groups/{group}/traits/{trait_id}/values", "/groups/{group}/datasets", "/publications/{pubmed_id}/traits", "/mapping-methods", "/accessions/{accession}", "/datasets/{dataset}", "/datasets/{dataset}/probesets/{probeset}", "/datasets/{dataset}/probesets/{probeset}/values", ] { assert!(paths.contains_key(path), "missing path: {path}"); } } /// Every JSON success response is documented as the envelope (an /// `Envelope_*` schema), so a new endpoint cannot forget to wrap its /// data. Only plain text (`/`) is exempt; `/openapi.json` is not an /// operation of the document it serves. #[tokio::test] async fn every_json_success_response_is_an_envelope() { let response = genenetwork4::app() .oneshot( Request::builder() .uri("/openapi.json") .body(Body::empty()) .unwrap(), ) .await .unwrap(); let body = response.into_body().collect().await.unwrap().to_bytes(); let doc: Value = serde_json::from_slice(&body).unwrap(); let mut checked = 0; for (path, item) in doc["paths"].as_object().unwrap() { for (method, operation) in item.as_object().unwrap() { let Some(content) = operation["responses"]["200"]["content"].as_object() else { continue; }; for (media_type, body) in content { if media_type != "application/json" { continue; } let schema = body["schema"]["$ref"].as_str().unwrap_or_default(); assert!( schema.starts_with("#/components/schemas/Envelope_"), "{method} {path}: 200 response is {schema:?}, not an envelope" ); checked += 1; } } } assert!(checked >= 20, "checked only {checked} responses"); }