about summary refs log tree commit diff
path: root/tests/openapi.rs
blob: 5341d0a27f0366d253d79fbae38273a0d15649de (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use axum::body::Body;
use axum::http::{Request, StatusCode};
use http_body_util::BodyExt;
use serde_json::Value;
use tower::ServiceExt;

#[tokio::test]
async fn openapi_json_describes_all_endpoints() {
    let response = genenetwork4::app()
        .oneshot(
            Request::builder()
                .uri("/openapi.json")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();

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

    let body = response.into_body().collect().await.unwrap().to_bytes();
    let doc: Value = serde_json::from_slice(&body).unwrap();

    assert_eq!(doc["info"]["title"], "GeneNetwork4 API");

    let paths = doc["paths"].as_object().unwrap();
    for path in [
        "/",
        "/version",
        "/species",
        "/species/{species}",
        "/groups",
        "/groups/{group}",
        "/groups/{group}/strains",
        "/groups/{group}/traits/{trait_id}",
        "/groups/{group}/traits/{trait_id}/values",
        "/groups/{group}/datasets",
        "/publications/{pubmed_id}/traits",
        "/mapping-methods",
        "/accessions/{accession}",
        "/datasets/{dataset}",
        "/datasets/{dataset}/probesets/{probeset}",
        "/datasets/{dataset}/probesets/{probeset}/values",
    ] {
        assert!(paths.contains_key(path), "missing path: {path}");
    }
}

/// Every JSON success response is documented as the envelope (an
/// `Envelope_*` schema), so a new endpoint cannot forget to wrap its
/// data. Only plain text (`/`) is exempt; `/openapi.json` is not an
/// operation of the document it serves.
#[tokio::test]
async fn every_json_success_response_is_an_envelope() {
    let response = genenetwork4::app()
        .oneshot(
            Request::builder()
                .uri("/openapi.json")
                .body(Body::empty())
                .unwrap(),
        )
        .await
        .unwrap();
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let doc: Value = serde_json::from_slice(&body).unwrap();

    let mut checked = 0;
    for (path, item) in doc["paths"].as_object().unwrap() {
        for (method, operation) in item.as_object().unwrap() {
            let Some(content) = operation["responses"]["200"]["content"].as_object() else {
                continue;
            };
            for (media_type, body) in content {
                if media_type != "application/json" {
                    continue;
                }
                let schema = body["schema"]["$ref"].as_str().unwrap_or_default();
                assert!(
                    schema.starts_with("#/components/schemas/Envelope_"),
                    "{method} {path}: 200 response is {schema:?}, not an envelope"
                );
                checked += 1;
            }
        }
    }
    assert!(checked >= 20, "checked only {checked} responses");
}