about summary refs log tree commit diff
path: root/tests/species.rs
blob: 334b8cad4ca6939bc2d3042c70b699268f56c835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;

mod common;

/// Unlike hello_world/version, /species needs a live MariaDB connection
/// (SQL_URI, see src/db.rs) and a valid token (every endpoint does now
/// -- see docs/gn-auth.md). There is no database reachable inside the
/// Guix build sandbox, so this test checks connectivity first and skips
/// (passes trivially, with a message) rather than failing the build when
/// none is available; same for a token (common::auth_token() tries
/// POST /get-test-token first, then GN4_TEST_AUTH_TOKEN).
#[tokio::test]
async fn species_returns_ok_with_species_list() {
    if !common::db_available() {
        eprintln!(
            "skipping species_returns_ok_with_species_list: no usable database (check SQL_URI credentials)"
        );
        return;
    }
    let Some(token) = common::auth_token().await else {
        eprintln!("skipping species_returns_ok_with_species_list: no token (see docs/gn-auth.md)");
        return;
    };

    let response = common::get_with_token("GET /species", "/species", &token).await;

    assert_eq!(response.status(), StatusCode::OK);

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let species: Vec<Value> = common::data(&body);

    assert!(!species.is_empty());
    for record in &species {
        assert!(record["name"].as_str().is_some());
        assert!(record["family"].as_str().is_some());
    }

    // mouse and rat are always present in the database.
    let names: Vec<&str> = species
        .iter()
        .map(|record| record["name"].as_str().unwrap())
        .collect();
    assert!(names.contains(&"mouse"));
    assert!(names.contains(&"rat"));
}