G: F1 probe documented paths through auth-off router with real methods
This commit is contained in:
1 parent
55a2401f17
commit
1506af11fc
1 file changed
+98
-12
+98
-12
@@ -9,8 +9,10 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::body::{to_bytes, Body};
|
||||
use axum::http::{Request, StatusCode};
|
||||
use axum::http::{Method, Request, StatusCode};
|
||||
use axum::Router;
|
||||
use serde_json::Value;
|
||||
use skyg_linux_tracker::auth::tokens;
|
||||
@@ -20,13 +22,38 @@ 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 skyg_linux_tracker::sources::fake::fake_state;
|
||||
use skyg_linux_tracker::state::AppState;
|
||||
use sqlx::sqlite::SqlitePoolOptions;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tower::ServiceExt;
|
||||
|
||||
const MANIFEST: &str = env!("CARGO_MANIFEST_DIR");
|
||||
|
||||
/// Auth off: these probes are about routing, not about the token layer, and
|
||||
/// the fallback's `no such route` never reached the router while auth answered
|
||||
/// every request with 401 first.
|
||||
fn open_config() -> Config {
|
||||
let mut config = Config::default();
|
||||
config.auth.enabled = false;
|
||||
config
|
||||
}
|
||||
|
||||
/// The process shutdown token, so a probe of the two follow routes ends.
|
||||
fn probe(state: AppState) -> (Router, CancellationToken) {
|
||||
let shutdown = state.shutdown.clone();
|
||||
(server::build_router(state), shutdown)
|
||||
}
|
||||
|
||||
fn cancel_soon(shutdown: CancellationToken) {
|
||||
tokio::spawn(async move {
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
shutdown.cancel();
|
||||
});
|
||||
}
|
||||
|
||||
/// The router with peer routes mounted, so every documented path is present.
|
||||
fn app() -> Router {
|
||||
fn app() -> (Router, CancellationToken) {
|
||||
let status = PeerStatus {
|
||||
name: PeerName::parse("beta").unwrap(),
|
||||
enabled: true,
|
||||
@@ -37,8 +64,8 @@ fn app() -> Router {
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
server::build_router(fake_state_with_peers(
|
||||
Config::default(),
|
||||
probe(fake_state_with_peers(
|
||||
open_config(),
|
||||
static_registry(vec![status]),
|
||||
))
|
||||
}
|
||||
@@ -55,9 +82,28 @@ fn sample(path: &str) -> String {
|
||||
.replace("{path}", "systemd/units")
|
||||
}
|
||||
|
||||
/// Status and body of one GET through the assembled router.
|
||||
async fn get(app: &Router, uri: &str) -> (StatusCode, Value) {
|
||||
let request = Request::get(uri)
|
||||
/// Operations of one documented path, by the method the document declares.
|
||||
fn documented_methods(item: &utoipa::openapi::path::PathItem) -> Vec<Method> {
|
||||
[
|
||||
(Method::GET, item.get.is_some()),
|
||||
(Method::POST, item.post.is_some()),
|
||||
(Method::PUT, item.put.is_some()),
|
||||
(Method::DELETE, item.delete.is_some()),
|
||||
(Method::PATCH, item.patch.is_some()),
|
||||
(Method::HEAD, item.head.is_some()),
|
||||
(Method::OPTIONS, item.options.is_some()),
|
||||
]
|
||||
.into_iter()
|
||||
.filter(|(_, present)| *present)
|
||||
.map(|(method, _)| method)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Status and body of one request through the assembled router.
|
||||
async fn call(app: &Router, method: Method, uri: &str) -> (StatusCode, Value) {
|
||||
let request = Request::builder()
|
||||
.method(method)
|
||||
.uri(uri)
|
||||
.header("host", "127.0.0.1")
|
||||
.body(Body::empty())
|
||||
.unwrap();
|
||||
@@ -137,11 +183,51 @@ async fn every_router_route_is_documented() {
|
||||
#[tokio::test]
|
||||
async fn every_documented_path_is_routed() {
|
||||
let doc = document(true);
|
||||
let app = app();
|
||||
for path in doc.paths.paths.keys() {
|
||||
let (status, body) = get(&app, &sample(path)).await;
|
||||
let fallback = body["error"]["message"] == "no such route";
|
||||
assert!(!fallback, "{path} is documented but not routed ({status})");
|
||||
let (app, shutdown) = app();
|
||||
cancel_soon(shutdown);
|
||||
for (path, item) in &doc.paths.paths {
|
||||
for method in documented_methods(item) {
|
||||
let (status, body) = call(&app, method.clone(), &sample(path)).await;
|
||||
let fallback = body["error"]["message"] == "no such route";
|
||||
assert!(
|
||||
!fallback,
|
||||
"{method} {path} is documented but not routed ({status})"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The other half of the probe: a peer-less instance mounts no peer paths, so
|
||||
/// those documented paths must answer with the fallback.
|
||||
#[tokio::test]
|
||||
async fn documented_peer_paths_are_absent_without_peers() {
|
||||
let doc = document(false);
|
||||
assert!(
|
||||
!doc.paths
|
||||
.paths
|
||||
.keys()
|
||||
.any(|path| path.starts_with("/api/v1/peers")),
|
||||
"document(false) still has peer paths"
|
||||
);
|
||||
let (app, shutdown) = probe(fake_state(open_config()));
|
||||
cancel_soon(shutdown);
|
||||
for (path, item) in &doc.paths.paths {
|
||||
for method in documented_methods(item) {
|
||||
let (status, body) = call(&app, method.clone(), &sample(path)).await;
|
||||
assert_ne!(
|
||||
body["error"]["message"], "no such route",
|
||||
"{method} {path} is documented but not routed ({status})"
|
||||
);
|
||||
}
|
||||
}
|
||||
for path in [
|
||||
"/api/v1/peers",
|
||||
"/api/v1/peers/beta",
|
||||
"/api/v1/peers/beta/systemd/units",
|
||||
] {
|
||||
let (status, body) = call(&app, Method::GET, path).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND, "{path}");
|
||||
assert_eq!(body["error"]["message"], "no such route", "{path}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user