use capy_framework::{self as capy, prelude::*};
use capy_testing::TestApp;
use std::sync::Arc;
#[derive(Serialize, ApiSchema)]
#[serde(crate = "capy::serde")]
#[schemars(crate = "capy::schema")]
struct Message {
message: String,
}
fn application() -> App {
App::new().api_routes(
Routes::new()
.get("/hello", || async {
Json(Message {
message: "hello".into(),
})
})
.operation("hello")
.response::<Message>(),
)
}
async fn context(openapi: bool, docs: bool, production: bool) -> AppContext {
let mut config = AppConfig::default();
config.database.url = "sqlite::memory:".into();
config.api.openapi = openapi;
config.api.docs = docs;
if production {
config.app.environment = "production".into();
}
AppContext {
db: capy::db::connect(&config.database).await.unwrap(),
config: Arc::new(config),
}
}
#[test]
fn export_needs_no_database_and_advanced_standard_properties_are_available() {
let mut config = AppConfig::default();
config.api.title = Some("Example API".into());
config.api.version = "2026.1".into();
let document = application()
.config(config)
.openapi_with(|document| {
document.as_value_mut()["components"]["securitySchemes"] =
json!({"bearer":{"type":"http","scheme":"bearer"}});
})
.openapi()
.unwrap();
assert_eq!(document.as_value()["info"]["title"], "Example API");
assert_eq!(document.as_value()["info"]["version"], "2026.1");
assert_eq!(
document.as_value()["paths"]["/api/hello"]["get"]["operationId"],
"hello"
);
assert_eq!(
document.as_value()["components"]["securitySchemes"]["bearer"]["scheme"],
"bearer"
);
}
#[tokio::test]
async fn documentation_is_opt_in_and_production_exposure_is_explicit() {
let app = TestApp::new(application().into_router(context(false, false, false).await));
app.get("/openapi.json")
.send()
.await
.assert_status(capy::axum::http::StatusCode::NOT_FOUND);
app.get("/docs/api")
.send()
.await
.assert_status(capy::axum::http::StatusCode::NOT_FOUND);
let ctx = context(false, true, false).await;
ctx.db.clone().close().await.unwrap();
let app = TestApp::new(application().into_router(ctx));
app.get("/openapi.json").send().await.assert_ok();
app.get("/docs/api").send().await.assert_ok();
let app = TestApp::new(application().into_router(context(false, true, true).await));
app.get("/openapi.json")
.send()
.await
.assert_status(capy::axum::http::StatusCode::NOT_FOUND);
app.get("/docs/api")
.send()
.await
.assert_status(capy::axum::http::StatusCode::NOT_FOUND);
let app = TestApp::new(application().into_router(context(true, true, true).await));
app.get("/openapi.json").send().await.assert_ok();
app.get("/docs/api")
.send()
.await
.assert_status(capy::axum::http::StatusCode::NOT_FOUND);
}