about summary refs log tree commit diff
path: root/tests/get_test_token_off.rs
blob: bf0fde315bc47159aa85c5eceaef2728d4f40b14 (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
//! GENENETWORK4_TEST_TOKEN switches POST /get-test-token off: when it is
//! not a clearly affirmative value the route is not registered at all, so
//! it is a 404 and absent from /openapi.json (src/config.rs `parse_switch`,
//! src/lib.rs). The setting is process-wide, so this is its own test
//! binary with one test function that changes it in sequence; the rest of
//! the suite leaves it at its default (on).

use axum::http::StatusCode;
use http_body_util::BodyExt;
use serde_json::Value;

mod common;

fn set(value: &str) {
    // SAFETY: the only test in this binary, so nothing else reads the
    // environment concurrently.
    unsafe { std::env::set_var("GENENETWORK4_TEST_TOKEN", value) };
}

async fn documented() -> bool {
    let response = common::get("GET /openapi.json", "/openapi.json").await;
    let body = response.into_body().collect().await.unwrap().to_bytes();
    let spec: Value = serde_json::from_slice(&body).unwrap();
    spec["paths"]["/get-test-token"].is_object()
}

#[tokio::test]
async fn get_test_token_can_be_switched_off() {
    // Off -- including the spellings that are not clearly "on": the setting
    // fails closed, so a typo does not leave the endpoint running. The
    // caller is local, so only the switch can explain the 404.
    for off in ["false", "False", "0", "no", "off", "", "flase", "maybe"] {
        set(off);
        let response = common::post("POST /get-test-token (switched off)", "/get-test-token").await;
        assert_eq!(response.status(), StatusCode::NOT_FOUND, "{off:?}");
        assert!(!documented().await, "{off:?}: still in /openapi.json");
    }

    // On: registered and documented, and a local caller is not refused.
    for on in ["true", "TRUE", "1", "yes", "on"] {
        set(on);
        let response = common::post("POST /get-test-token (switched on)", "/get-test-token").await;
        assert_ne!(response.status(), StatusCode::NOT_FOUND, "{on:?}");
        assert_ne!(response.status(), StatusCode::FORBIDDEN, "{on:?}");
        assert!(documented().await, "{on:?}: missing from /openapi.json");
    }
}