added live redoc and slight refactors to tokens

This commit is contained in:
ari committed 2026-10-20 22:53:09 -04:00
1 parent 435a54a02d
commit b38c66bcc8
19 files changed
+836 -157

No files matched your search

+46 -1
View File
@@ -332,7 +332,7 @@ async fn every_router_route_is_documented() {
let mut routes = api_routes();
routes.extend(server_routes());
// A scan that stops matching would make the checks below vacuous.
assert_eq!(routes.len(), 15, "route scan found: {routes:?}");
assert_eq!(routes.len(), 17, "route scan found: {routes:?}");
for route in routes {
let path = openapi_path(&route.path);
let item =
@@ -590,6 +590,51 @@ async fn openapi_route_is_authenticated_and_serves_the_document() {
assert!(served["paths"].get("/api/v1/peers").is_some());
}
/// The Redoc viewer is static and public so a browser can load it, while the
/// document it renders stays behind the token: the page cannot leak anything
/// the spec route would not.
#[tokio::test]
async fn docs_viewer_is_public_and_the_spec_it_loads_is_not() {
let mut config = Config::default();
config.auth.enabled = true;
let state = fake_state_with_peers(config, static_registry(Vec::new()));
let app = server::build_router(state);
let get = |uri: &str| {
Request::get(uri)
.header("host", "127.0.0.1")
.body(Body::empty())
.unwrap()
};
let page = app.clone().oneshot(get("/docs")).await.unwrap();
assert_eq!(page.status(), StatusCode::OK);
assert_eq!(page.headers()["content-type"], "text/html; charset=utf-8");
assert!(page.headers().contains_key("content-security-policy"));
let html = to_bytes(page.into_body(), usize::MAX).await.unwrap();
let html = String::from_utf8(html.to_vec()).unwrap();
assert!(html.contains("/docs/redoc.standalone.js"));
assert!(html.contains("/api/v1/openapi.json"));
let script = app
.clone()
.oneshot(get("/docs/redoc.standalone.js"))
.await
.unwrap();
assert_eq!(script.status(), StatusCode::OK);
assert_eq!(
script.headers()["content-type"],
"text/javascript; charset=utf-8"
);
let spec = app
.clone()
.oneshot(get("/api/v1/openapi.json"))
.await
.unwrap();
assert_eq!(spec.status(), StatusCode::UNAUTHORIZED);
}
/// `slinuxm openapi` prints the same document the route serves, without a
/// config file: the server is never started and no token is read.
#[test]