//! /traits/... spans both ProbeSet and Publish data (see //! src/routes/traits.rs and docs/gn-auth.md); these tests exercise //! both categories through the same URL shape. Needs a live MariaDB //! connection (SQL_URI) 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 dataset_probeset_info.rs. use axum::http::StatusCode; use http_body_util::BodyExt; use serde_json::Value; mod common; async fn fixtures() -> Option { if !common::db_available() { eprintln!("skipping: no usable database (check SQL_URI credentials)"); return None; } let Some(token) = common::auth_token().await else { eprintln!("skipping: set GN4_TEST_AUTH_TOKEN (see docs/gn-auth.md)"); return None; }; if !common::auth_server_available() { eprintln!("skipping: no gn-auth reachable"); return None; } Some(token) } /// Dataset Br_U_0803_M's CreateTime is 2003-08-01, so it's publicly /// readable regardless of its public/confidentiality flags -- a valid /// token is still required, but no gn-auth authorization call happens. #[tokio::test] async fn trait_info_probeset_category() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/Br_U_0803_M/100001_at", "/traits/Br_U_0803_M/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["category"], "probeset"); assert_eq!(info["name"], "100001_at"); assert_eq!(info["symbol"], "Cd3g"); } /// BXD's PublishFreeze (BXDPublish) is addressable both by its own /// name and by the owning group's name, matching GeneNetwork2's /// wqflask.api.router.trait_sample_data's Publish branch. #[tokio::test] async fn trait_info_publish_category_by_group_name() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token("GET /traits/BXD/10001", "/traits/BXD/10001", &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["category"], "publish"); assert_eq!(info["id"], 10001); } #[tokio::test] async fn trait_info_publish_category_by_publish_freeze_name() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/BXDPublish/10001", "/traits/BXDPublish/10001", &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["category"], "publish"); assert_eq!(info["id"], 10001); } #[tokio::test] async fn trait_values_probeset_category() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/Br_U_0803_M/100001_at/values", "/traits/Br_U_0803_M/100001_at/values", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let values: Vec = common::data(&body); assert!(!values.is_empty()); assert!(values.iter().any(|v| v["name"] == "BXD1")); } #[tokio::test] async fn trait_values_publish_category() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/BXD/10001/values", "/traits/BXD/10001/values", &token, ) .await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let values: Vec = common::data(&body); assert!(!values.is_empty()); assert!(values.iter().any(|v| v["name"] == "BXD1")); } #[tokio::test] async fn traits_list_probeset_category() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token("GET /traits/Br_U_0803_M", "/traits/Br_U_0803_M", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let traits: Vec = common::data(&body); assert!(!traits.is_empty()); assert!(traits.iter().all(|t| t["category"] == "probeset")); assert!(traits.iter().any(|t| t["name"] == "100001_at")); } #[tokio::test] async fn traits_list_publish_category() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token("GET /traits/BXD", "/traits/BXD", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let traits: Vec = common::data(&body); assert!(!traits.is_empty()); assert!(traits.iter().all(|t| t["category"] == "publish")); assert!(traits.iter().any(|t| t["id"] == 10001)); } #[tokio::test] async fn trait_info_without_a_token_returns_401() { if !common::db_available() { eprintln!( "skipping trait_info_without_a_token_returns_401: no usable database (check SQL_URI credentials)" ); return; } let response = common::get("GET /traits/BXD/10001 (no token)", "/traits/BXD/10001").await; assert_eq!(response.status(), StatusCode::UNAUTHORIZED); } #[tokio::test] async fn trait_info_unknown_dataset_returns_404() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/not-a-real-dataset/123", "/traits/not-a-real-dataset/123", &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["code"], "dataset_not_found"); assert_eq!(error["operation_id"], "trait_info"); } #[tokio::test] async fn trait_info_unknown_trait_in_probeset_dataset_returns_404() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/Br_U_0803_M/not-a-real-probeset", "/traits/Br_U_0803_M/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["code"], "trait_not_found"); } #[tokio::test] async fn trait_info_unknown_trait_in_publish_dataset_returns_404() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token("GET /traits/BXD/99999999", "/traits/BXD/99999999", &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["code"], "trait_not_found"); } /// A non-numeric trait segment against a Publish dataset can never /// match a PublishXRef.Id -- same 404 as a numeric id that just /// doesn't exist, not a 500 from a failed parse. #[tokio::test] async fn trait_info_non_numeric_trait_in_publish_dataset_returns_404() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/BXD/not-a-number", "/traits/BXD/not-a-number", &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["code"], "trait_not_found"); } /// UTHSC_BXD_Cochlea_0225 (public=1, confidentiality=1, created /// 2025-02-13) is neither publicly readable by the flags nor old /// enough for the embargo-age rule, and has never been linked to a /// gn-auth resource -- same fixture as dataset_probeset_info.rs, /// exercised here through the unified endpoint instead. #[tokio::test] async fn trait_info_non_public_unlinked_probeset_dataset_returns_403() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/UTHSC_BXD_Cochlea_0225/ENSMUSG00000028180", "/traits/UTHSC_BXD_Cochlea_0225/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 trait_info_recent_non_public_probeset_dataset_returns_403() { let Some(token) = fixtures().await else { return; }; let response = common::get_with_token( "GET /traits/UTHSC_BXD_2wk_Cochlea_0926/ENSMUSG00000028180", "/traits/UTHSC_BXD_2wk_Cochlea_0926/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"); }