mod common; use axum::body::Body; use axum::http::{Method, Request, StatusCode}; use http_body_util::BodyExt; use serde_json::{Value, json}; use serial_test::serial; use tower::ServiceExt; use common::app::TestApp; // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- async fn json_body(resp: axum::response::Response) -> Value { let body = resp.into_body().collect().await.unwrap().to_bytes(); serde_json::from_slice(&body).unwrap() } fn admin_get( uri: &str, cookie: (axum::http::HeaderName, axum::http::HeaderValue), ) -> Request
{ Request::builder() .uri(uri) .header(cookie.0, cookie.1) .body(Body::empty()) .unwrap() } fn admin_put( uri: &str, cookie: (axum::http::HeaderName, axum::http::HeaderValue), body: &Value, ) -> Request { Request::builder() .method(Method::PUT) .uri(uri) .header(cookie.0, cookie.1) .header("content-type", "application/json") .body(Body::from(serde_json::to_vec(body).unwrap())) .unwrap() } fn admin_delete( uri: &str, cookie: (axum::http::HeaderName, axum::http::HeaderValue), ) -> Request { Request::builder() .method(Method::DELETE) .uri(uri) .header(cookie.0, cookie.1) .body(Body::empty()) .unwrap() } // --------------------------------------------------------------------------- // Settings tests // --------------------------------------------------------------------------- #[tokio::test] #[serial] #[ignore] async fn settings_crud() { let app = TestApp::new().await; // PUT a setting let resp = app .router .clone() .oneshot(admin_put( "/admin/settings/app_name", app.admin_cookie(), &json!({ "value": "Test App" }), )) .await .unwrap(); assert!( resp.status().is_success(), "expected success on PUT, got {}", resp.status() ); // GET all settings and verify the entry appears with source: "database" let resp = app .router .clone() .oneshot(admin_get("/admin/settings", app.admin_cookie())) .await .unwrap(); assert_eq!(resp.status(), StatusCode::OK); let json = json_body(resp).await; let settings = json.as_array().unwrap(); let app_name_entry = settings .iter() .find(|s| s["key"] == "app_name") .expect("app_name entry not found in settings"); assert_eq!(app_name_entry["source"], "database"); // DELETE the setting let resp = app .router .clone() .oneshot(admin_delete("/admin/settings/app_name", app.admin_cookie())) .await .unwrap(); assert!( resp.status().is_success(), "expected success on DELETE, got {}", resp.status() ); // GET again and verify it's removed let resp = app .router .clone() .oneshot(admin_get("/admin/settings", app.admin_cookie())) .await .unwrap(); assert_eq!(resp.status(), StatusCode::OK); let json = json_body(resp).await; let settings = json.as_array().unwrap(); let app_name_entry = settings.iter().find(|s| s["key"] == "app_name"); assert!( app_name_entry.is_none(), "app_name entry should have been deleted" ); } #[tokio::test] #[serial] #[ignore] async fn settings_requires_auth() { let app = TestApp::new().await; let resp = app .router .clone() .oneshot( Request::builder() .uri("/admin/settings") .body(Body::empty()) .unwrap(), ) .await .unwrap(); assert_eq!(resp.status(), StatusCode::UNAUTHORIZED); } #[tokio::test] #[serial] #[ignore] async fn logo_upload_and_serve() { let app = TestApp::new().await; let boundary = "----testboundary"; // Minimal valid 1x1 PNG let png_bytes: Vec