swap to nextest

This commit is contained in:
2025-07-24 17:32:50 +01:00
parent 392dc779ff
commit 9328451a25
2 changed files with 34 additions and 4 deletions

View File

@ -11,4 +11,4 @@ curl -s -X POST localhost:7001/admin/shutdown 2>&1 > /dev/null
cargo run --features shutdown -- --port 7001 2>&1 > logs/nuchat.log & cargo run --features shutdown -- --port 7001 2>&1 > logs/nuchat.log &
# run tests # run tests
cargo test | tee logs/test-output.log cargo nextest run 2>&1 | tee logs/test-output.log

View File

@ -12,7 +12,7 @@ use http::StatusCode;
use tower::ServiceBuilder; use tower::ServiceBuilder;
use tower_http::timeout::TimeoutLayer; use tower_http::timeout::TimeoutLayer;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing::Level; use tracing::{Level, warn};
use uuid::Uuid; use uuid::Uuid;
pub fn app() -> (Router, mpsc::Receiver<bool>) { pub fn app() -> (Router, mpsc::Receiver<bool>) {
@ -42,15 +42,22 @@ pub fn app() -> (Router, mpsc::Receiver<bool>) {
} }
fn admin(tx: mpsc::Sender<bool>) -> Router { fn admin(tx: mpsc::Sender<bool>) -> Router {
let r = Router::new().route("/test", get(async || StatusCode::OK)); let r = Router::new().route("/", get(async || StatusCode::OK));
let r = add_shutdown_endpoint(r, tx); let r = add_shutdown_endpoint(r, tx);
r.layer(from_fn(async |req: Request, next: Next| { r.layer(from_fn(async |req: Request, next: Next| {
if let Ok(secret) = std::env::var("ADMIN_SECRET") { if let Ok(secret) = std::env::var("ADMIN_SECRET") {
println!("ADMIN_SECRET: {secret}");
match req.headers().get("Authorization") { match req.headers().get("Authorization") {
Some(key) if secret == *key => (), Some(key) if secret == *key => (),
Some(key) => {
warn!("Unauthorized request with key: {key:?}");
return Response::builder()
.status(StatusCode::UNAUTHORIZED)
.body(Body::empty())
.unwrap();
}
_ => { _ => {
warn!("Unauthorized request no key given");
return Response::builder() return Response::builder()
.status(StatusCode::UNAUTHORIZED) .status(StatusCode::UNAUTHORIZED)
.body(Body::empty()) .body(Body::empty())
@ -82,3 +89,26 @@ fn add_shutdown_endpoint(r: Router, tx: mpsc::Sender<bool>) -> Router {
fn add_shutdown_endpoint(r: Router, _: mpsc::Sender<bool>) -> Router { fn add_shutdown_endpoint(r: Router, _: mpsc::Sender<bool>) -> Router {
r r
} }
#[cfg(test)]
mod tests {
use super::*;
use tower::{self, ServiceExt};
#[tokio::test]
async fn test_authorization_disables_when_no_env_var_set() {
let (app, _) = app();
let resp = app
.oneshot(
axum::http::Request::builder()
.uri("/admin")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
}
}