G: G3 Serving and CLI serve the spec behind auth and print it offline

This commit is contained in:
ari committed 2026-10-20 15:54:13 -04:00
1 parent 7078ed15b1
commit 869b6818e9
7 files changed
+192 -12

No files matched your search

+75
View File
@@ -13,12 +13,14 @@ use axum::body::{to_bytes, Body};
use axum::http::{Request, StatusCode};
use axum::Router;
use serde_json::Value;
use skyg_linux_tracker::auth::tokens;
use skyg_linux_tracker::config::Config;
use skyg_linux_tracker::mcp::TrackerService;
use skyg_linux_tracker::openapi::{self, document};
use skyg_linux_tracker::peers::testing::{fake_state_with_peers, static_registry};
use skyg_linux_tracker::peers::types::{ApiStatus, AuthState, PeerName, PeerStatus, ServerStatus};
use skyg_linux_tracker::server;
use sqlx::sqlite::SqlitePoolOptions;
use tower::ServiceExt;
const MANIFEST: &str = env!("CARGO_MANIFEST_DIR");
@@ -214,3 +216,76 @@ fn the_spec_is_valid_json_with_the_expected_shape() {
assert!(off["paths"].get("/api/v1/peers").is_none());
assert!(off["paths"].get("/api/v1/systemd/units").is_some());
}
/// The served document is the generated one, and only a token opens it.
#[tokio::test]
async fn openapi_route_is_authenticated_and_serves_the_document() {
let mut config = Config::default();
config.auth.enabled = true;
let mut state = fake_state_with_peers(config, static_registry(Vec::new()));
let pool = SqlitePoolOptions::new()
.max_connections(1)
.connect("sqlite::memory:")
.await
.unwrap();
skyg_linux_tracker::db::migrate(&pool).await.unwrap();
let token = tokens::create(&pool, "openapi-test").await.unwrap();
state.db = Some(pool);
let app = server::build_router(state);
let anonymous = Request::get("/api/v1/openapi.json")
.header("host", "127.0.0.1")
.body(Body::empty())
.unwrap();
let anonymous = app.clone().oneshot(anonymous).await.unwrap();
assert_eq!(anonymous.status(), StatusCode::UNAUTHORIZED);
let authorized = Request::get("/api/v1/openapi.json")
.header("host", "127.0.0.1")
.header("authorization", format!("Bearer {token}"))
.body(Body::empty())
.unwrap();
let response = app.clone().oneshot(authorized).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response
.headers()
.get("content-type")
.and_then(|v| v.to_str().ok()),
Some("application/json")
);
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
let served: Value = serde_json::from_slice(&body).unwrap();
// This router has a registry, so the served document is the peer one.
assert_eq!(served, serde_json::to_value(&*document(true)).unwrap());
assert!(served["paths"].get("/api/v1/peers").is_some());
}
/// `slinuxm openapi` prints the same document the route serves, without a
/// config file: the server is never started and no token is read.
#[test]
fn cli_prints_the_same_document_offline() {
let run = |args: &[&str]| -> Value {
let output = std::process::Command::new(env!("CARGO_BIN_EXE_slinuxm"))
.args(args)
.arg("--config")
.arg("/nonexistent/slinuxm-openapi.toml")
.output()
.expect("the slinuxm binary runs");
assert!(
output.status.success(),
"slinuxm {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
);
serde_json::from_slice(&output.stdout).expect("the output is JSON")
};
for (peers, extra) in [(false, None), (true, Some("--peers"))] {
let args: Vec<&str> = ["openapi"].into_iter().chain(extra).collect();
let printed = run(&args);
assert_eq!(
printed,
serde_json::to_value(&*document(peers)).unwrap(),
"the CLI and the served route must share one generation"
);
}
}