use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; /// Needs a live MariaDB connection (SQL_URI, see src/db.rs) and a live /// gn-auth (AUTH_SERVER_URL) plus GN4_TEST_AUTH_TOKEN (see /// docs/gn-auth.md); skips rather than fails when either is missing, /// same as group_by_name.rs. Probeset 100001_at (gene symbol Cd3g) in /// dataset Br_U_0803_M is a real row in the database; the dataset's /// CreateTime is 2003-08-01, so it's publicly readable regardless of /// its public/confidentiality flags (see src/auth.rs) -- a valid token /// is still required, but no gn-auth authorization call happens. #[tokio::test] async fn dataset_probeset_info_returns_gene_annotation() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_returns_gene_annotation: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping dataset_probeset_info_returns_gene_annotation: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !common::auth_server_available() { eprintln!("skipping dataset_probeset_info_returns_gene_annotation: no gn-auth reachable"); return; } let response = common::get_with_token( "GET /datasets/Br_U_0803_M/probesets/100001_at", "/datasets/Br_U_0803_M/probesets/100001_at", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let info: Value = common::data(&body); assert_eq!(info["name"], "100001_at"); let (lrs, lod) = (info["lrs"].as_f64().unwrap(), info["lod"].as_f64().unwrap()); assert!((lod - lrs / 4.604).abs() < 1e-9, "lod is lrs / 4.604"); assert_eq!(info["symbol"], "Cd3g"); assert!(info["mean"].as_f64().is_some()); } #[tokio::test] async fn dataset_probeset_info_without_a_token_returns_401() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_without_a_token_returns_401: no usable database (check SQL_URI credentials)" ); return; } let response = common::get( "GET /datasets/Br_U_0803_M/probesets/100001_at (no token)", "/datasets/Br_U_0803_M/probesets/100001_at", ) .await; assert_eq!(response.status(), StatusCode::UNAUTHORIZED); } #[tokio::test] async fn dataset_probeset_info_unknown_dataset_returns_404() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_unknown_dataset_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping dataset_probeset_info_unknown_dataset_returns_404: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !common::auth_server_available() { eprintln!( "skipping dataset_probeset_info_unknown_dataset_returns_404: no gn-auth reachable" ); return; } let response = common::get_with_token( "GET /datasets/not-a-real-dataset/probesets/100001_at", "/datasets/not-a-real-dataset/probesets/100001_at", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let body = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["status"], 404); assert_eq!(error["code"], "dataset_not_found"); assert_eq!(error["operation_id"], "dataset_probeset_info"); } #[tokio::test] async fn dataset_probeset_info_unknown_probeset_in_dataset_returns_404() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_unknown_probeset_in_dataset_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping dataset_probeset_info_unknown_probeset_in_dataset_returns_404: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !common::auth_server_available() { eprintln!( "skipping dataset_probeset_info_unknown_probeset_in_dataset_returns_404: no gn-auth reachable" ); return; } let response = common::get_with_token( "GET /datasets/Br_U_0803_M/probesets/not-a-real-probeset", "/datasets/Br_U_0803_M/probesets/not-a-real-probeset", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let body = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["status"], 404); assert_eq!(error["code"], "probeset_not_found"); assert_eq!(error["operation_id"], "dataset_probeset_info"); } /// UTHSC_BXD_Cochlea_0225 (public=1, confidentiality=1, created /// 2025-02-13) is neither publicly readable by the flags /// (confidentiality >= 1) nor old enough for the embargo-age rule, and /// has never been linked to a gn-auth resource -- so even the token's /// owner has no privilege on it, and the request must be denied rather /// than silently allowed. #[tokio::test] async fn dataset_probeset_info_non_public_unlinked_dataset_returns_403() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_non_public_unlinked_dataset_returns_403: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping dataset_probeset_info_non_public_unlinked_dataset_returns_403: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !common::auth_server_available() { eprintln!( "skipping dataset_probeset_info_non_public_unlinked_dataset_returns_403: no gn-auth reachable" ); return; } let response = common::get_with_token( "GET /datasets/UTHSC_BXD_Cochlea_0225/probesets/ENSMUSG00000028180", "/datasets/UTHSC_BXD_Cochlea_0225/probesets/ENSMUSG00000028180", &token, ) .await; assert_eq!(response.status(), StatusCode::FORBIDDEN); let body = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); } /// Same as above, but with a dataset genuinely less than a year old /// (UTHSC_BXD_Cochlea_0225 above is real but was already ~20 months /// old by the time this test was written) -- the age-embargo rule and /// the "not yet linked to any gn-auth resource" deny path both need to /// hold for freshly-added data, not just for something that merely /// hasn't aged into public yet. UTHSC_BXD_2wk_Cochlea_0926 /// (public=1, confidentiality=1, created 2026-09-01) is the most /// recently added ProbeSetFreeze in the database as of this writing. #[tokio::test] async fn dataset_probeset_info_recent_non_public_dataset_returns_403() { if !common::db_available() { eprintln!( "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)" ); return; }; if !common::auth_server_available() { eprintln!( "skipping dataset_probeset_info_recent_non_public_dataset_returns_403: no gn-auth reachable" ); return; } let response = common::get_with_token( "GET /datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180", "/datasets/UTHSC_BXD_2wk_Cochlea_0926/probesets/ENSMUSG00000028180", &token, ) .await; assert_eq!(response.status(), StatusCode::FORBIDDEN); let body = response.into_body().collect().await.unwrap().to_bytes(); let error: Value = serde_json::from_slice(&body).unwrap(); assert_eq!(error["code"], "forbidden"); }