//! A response is `partial`, not an error, when something it could have //! included is unavailable. This file is its own test binary (so its //! own process) because it points SQL_URI at a dead port before the //! first database use; the connection pool is cached per process. //! //! No network, database or gn-auth: gn3 and the JWKS are local stubs, //! as in tests/search.rs. use axum::Router; use axum::body::Body; use axum::http::{Request, StatusCode}; use axum::routing::get; use http_body_util::BodyExt; use jsonwebtoken::{Algorithm, EncodingKey, Header, encode}; use serde::Serialize; use serde_json::Value; use tower::ServiceExt; const FIXTURE: &str = include_str!("../test/data/search_alcohol_phenotype.json"); const TEST_JWKS: &str = include_str!("../test/data/test_jwks.json"); const TEST_PRIVATE_KEY: &str = include_str!("../test/data/test_rsa_private.pem"); async fn spawn(app: Router) -> String { let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let port = listener.local_addr().unwrap().port(); tokio::spawn(async move { axum::serve(listener, app).await.unwrap(); }); format!("http://127.0.0.1:{port}") } fn sign_test_token(issuer: &str) -> String { #[derive(Serialize)] struct Claims { sub: String, scope: String, iss: String, iat: u64, exp: u64, } let now = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() .as_secs(); let claims = Claims { sub: "envelope-test-user".to_string(), scope: "profile".to_string(), iss: issuer.to_string(), iat: now, exp: now + 3600, }; let key = EncodingKey::from_rsa_pem(TEST_PRIVATE_KEY.as_bytes()).unwrap(); encode(&Header::new(Algorithm::RS256), &claims, &key).unwrap() } #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn search_is_partial_when_the_database_is_unreachable() { let gn3 = spawn(Router::new().route("/api/search/", get(|| async { FIXTURE }))).await; let gn_auth = spawn(Router::new().route("/auth/public-jwks", get(|| async { TEST_JWKS }))).await; // SAFETY: the only test in this binary, so nothing else reads the // environment concurrently. unsafe { std::env::set_var("GN3_URL", format!("{gn3}/api/")); std::env::set_var("AUTH_SERVER_URL", &gn_auth); std::env::set_var("SQL_URI", "mysql://nobody:nobody@127.0.0.1:1/db_webqtl"); } let token = sign_test_token(&gn_auth); let request = Request::builder() .uri("/search?q=alcohol&type=phenotype&limit=5") .header("Authorization", format!("Bearer {token}")) .body(Body::empty()) .unwrap(); let response = genenetwork4::app().oneshot(request).await.unwrap(); assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let body: Value = serde_json::from_slice(&body).unwrap(); // The hits are all there; only what the database adds is missing. assert_eq!(body["meta"]["state"], "partial"); assert_eq!(body["data"].as_array().unwrap().len(), 5); assert_eq!(body["data"][0]["name"], "10199"); assert!(body["data"][0]["locus"].is_null()); assert!(body["data"][0]["abbreviation"].is_null()); let notes: Vec<&str> = body["notes"] .as_array() .unwrap() .iter() .map(|n| n.as_str().unwrap()) .collect(); assert!( notes .iter() .any(|n| n.contains("locus and abbreviation could not be looked up")) ); }