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 valid /// token (every endpoint needs one now -- see docs/gn-auth.md); skips /// rather than fails when either is missing, same as species.rs. #[tokio::test] async fn species_by_name_mouse_returns_species_row() { if !common::db_available() { eprintln!( "skipping species_by_name_mouse_returns_species_row: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping species_by_name_mouse_returns_species_row: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token("GET /species/mouse", "/species/mouse", &token).await; assert_eq!(response.status(), StatusCode::OK); let body = response.into_body().collect().await.unwrap().to_bytes(); let species: Value = common::data(&body); assert_eq!(species["name"], "mouse"); assert_eq!(species["full_name"], "Mus musculus"); assert_eq!(species["taxonomy_id"], 10090); } #[tokio::test] async fn species_by_name_unknown_species_returns_404() { if !common::db_available() { eprintln!( "skipping species_by_name_unknown_species_returns_404: no usable database (check SQL_URI credentials)" ); return; } let Some(token) = common::auth_token().await else { eprintln!( "skipping species_by_name_unknown_species_returns_404: no token (see docs/gn-auth.md)" ); return; }; let response = common::get_with_token( "GET /species/not-a-real-species", "/species/not-a-real-species", &token, ) .await; assert_eq!(response.status(), StatusCode::NOT_FOUND); let request_id = response .headers() .get("x-request-id") .unwrap() .to_str() .unwrap() .to_string(); 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"], "species_not_found"); assert_eq!(error["operation_id"], "species_by_name"); assert_eq!(error["spec_url"], "/openapi.json"); assert_eq!(error["next"], "/species"); assert_eq!(error["request_id"], request_id); }