Readur/src/tests/settings_tests.rs

221 lines
7.4 KiB
Rust

#[cfg(test)]
mod tests {
use crate::models::UpdateSettings;
use super::super::helpers::{create_test_app, create_test_user, login_user};
use axum::http::StatusCode;
use serde_json::json;
use tower::util::ServiceExt;
#[tokio::test]
async fn test_get_settings_default() {
let (app, _container) = create_test_app().await;
let user = create_test_user(&app).await;
let token = login_user(&app, &user.username, "password123").await;
let response = app
.oneshot(
axum::http::Request::builder()
.method("GET")
.uri("/api/settings")
.header("Authorization", format!("Bearer {}", token))
.body(axum::body::Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let settings: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(settings["ocr_language"], "eng");
}
#[tokio::test]
async fn test_update_settings() {
let (app, _container) = create_test_app().await;
let user = create_test_user(&app).await;
let token = login_user(&app, &user.username, "password123").await;
let update_data = UpdateSettings {
ocr_language: Some("spa".to_string()),
concurrent_ocr_jobs: None,
ocr_timeout_seconds: None,
max_file_size_mb: None,
allowed_file_types: None,
auto_rotate_images: None,
enable_image_preprocessing: None,
search_results_per_page: None,
search_snippet_length: None,
fuzzy_search_threshold: None,
retention_days: None,
enable_auto_cleanup: None,
enable_compression: None,
memory_limit_mb: None,
cpu_priority: None,
enable_background_ocr: None,
ocr_page_segmentation_mode: None,
ocr_engine_mode: None,
ocr_min_confidence: None,
ocr_dpi: None,
ocr_enhance_contrast: None,
ocr_remove_noise: None,
ocr_detect_orientation: None,
ocr_whitelist_chars: None,
ocr_blacklist_chars: None,
};
let response = app
.clone()
.oneshot(
axum::http::Request::builder()
.method("PUT")
.uri("/api/settings")
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.body(axum::body::Body::from(serde_json::to_vec(&update_data).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
// Verify the update
let response = app
.oneshot(
axum::http::Request::builder()
.method("GET")
.uri("/api/settings")
.header("Authorization", format!("Bearer {}", token))
.body(axum::body::Body::empty())
.unwrap(),
)
.await
.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let settings: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(settings["ocr_language"], "spa");
}
#[tokio::test]
async fn test_settings_isolated_per_user() {
let (app, _container) = create_test_app().await;
// Create two users
let user1 = create_test_user(&app).await;
let token1 = login_user(&app, &user1.username, "password123").await;
let user2_data = json!({
"username": "testuser2",
"email": "test2@example.com",
"password": "password456"
});
let response = app
.clone()
.oneshot(
axum::http::Request::builder()
.method("POST")
.uri("/api/auth/register")
.header("Content-Type", "application/json")
.body(axum::body::Body::from(serde_json::to_vec(&user2_data).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let token2 = login_user(&app, "testuser2", "password456").await;
// Update user1's settings
let update_data = UpdateSettings {
ocr_language: Some("fra".to_string()),
concurrent_ocr_jobs: None,
ocr_timeout_seconds: None,
max_file_size_mb: None,
allowed_file_types: None,
auto_rotate_images: None,
enable_image_preprocessing: None,
search_results_per_page: None,
search_snippet_length: None,
fuzzy_search_threshold: None,
retention_days: None,
enable_auto_cleanup: None,
enable_compression: None,
memory_limit_mb: None,
cpu_priority: None,
enable_background_ocr: None,
ocr_page_segmentation_mode: None,
ocr_engine_mode: None,
ocr_min_confidence: None,
ocr_dpi: None,
ocr_enhance_contrast: None,
ocr_remove_noise: None,
ocr_detect_orientation: None,
ocr_whitelist_chars: None,
ocr_blacklist_chars: None,
};
let response = app
.clone()
.oneshot(
axum::http::Request::builder()
.method("PUT")
.uri("/api/settings")
.header("Authorization", format!("Bearer {}", token1))
.header("Content-Type", "application/json")
.body(axum::body::Body::from(serde_json::to_vec(&update_data).unwrap()))
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
// Check user2's settings are still default
let response = app
.oneshot(
axum::http::Request::builder()
.method("GET")
.uri("/api/settings")
.header("Authorization", format!("Bearer {}", token2))
.body(axum::body::Body::empty())
.unwrap(),
)
.await
.unwrap();
let body = axum::body::to_bytes(response.into_body(), usize::MAX)
.await
.unwrap();
let settings: serde_json::Value = serde_json::from_slice(&body).unwrap();
assert_eq!(settings["ocr_language"], "eng");
}
#[tokio::test]
async fn test_settings_requires_auth() {
let (app, _container) = create_test_app().await;
let response = app
.oneshot(
axum::http::Request::builder()
.method("GET")
.uri("/api/settings")
.body(axum::body::Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::UNAUTHORIZED);
}
}