From 001f835e38f3409d34eeab133b8db6fadf73a019 Mon Sep 17 00:00:00 2001 From: perf3ct Date: Sun, 22 Jun 2025 18:11:54 +0000 Subject: [PATCH] feat(tests): make sure tests that should require admin privs, use admin --- tests/ocr_queue_management_tests.rs | 24 +++++++++++++----------- tests/performance_load_tests.rs | 11 +++++++---- tests/simple_throttling_test.rs | 14 +++++++++----- tests/source_scheduler_simple_tests.rs | 6 +++++- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/tests/ocr_queue_management_tests.rs b/tests/ocr_queue_management_tests.rs index b4cc87d..e961200 100644 --- a/tests/ocr_queue_management_tests.rs +++ b/tests/ocr_queue_management_tests.rs @@ -52,12 +52,14 @@ impl OCRQueueTestClient { return Err(format!("Server not running: {}", e).into()); } - let timestamp = std::time::SystemTime::now() + // Use UUID for guaranteed uniqueness across concurrent test execution + let test_id = Uuid::new_v4().simple().to_string(); + let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() - .as_millis(); - let username = format!("ocr_queue_test_{}_{}", role.to_string(), timestamp); - let email = format!("ocr_queue_test_{}@example.com", timestamp); + .as_nanos(); + let username = format!("ocr_queue_{}_{}_{}_{}", role.to_string(), test_id, nanos, Uuid::new_v4().simple()); + let email = format!("ocr_queue_{}_{}@{}.example.com", test_id, nanos, Uuid::new_v4().simple()); let password = "testpassword123"; // Register user @@ -297,8 +299,8 @@ impl Clone for OCRQueueTestClient { async fn test_queue_stats_monitoring() { let mut client = OCRQueueTestClient::new(); - // Register and login - client.register_and_login(UserRole::User).await + // Register and login as admin (queue stats require admin access) + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); @@ -353,7 +355,7 @@ async fn test_queue_stats_monitoring() { async fn test_failed_job_requeue_functionality() { let mut client = OCRQueueTestClient::new(); - client.register_and_login(UserRole::User).await + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); @@ -403,7 +405,7 @@ async fn test_failed_job_requeue_functionality() { async fn test_concurrent_ocr_processing() { let mut client = OCRQueueTestClient::new(); - client.register_and_login(UserRole::User).await + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); @@ -521,7 +523,7 @@ async fn test_concurrent_ocr_processing() { async fn test_queue_performance_monitoring() { let mut client = OCRQueueTestClient::new(); - client.register_and_login(UserRole::User).await + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); @@ -598,7 +600,7 @@ async fn test_queue_performance_monitoring() { async fn test_queue_error_handling() { let mut client = OCRQueueTestClient::new(); - client.register_and_login(UserRole::User).await + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); @@ -641,7 +643,7 @@ async fn test_queue_error_handling() { async fn test_queue_stats_consistency() { let mut client = OCRQueueTestClient::new(); - client.register_and_login(UserRole::User).await + client.register_and_login(UserRole::Admin).await .expect("Failed to register and login"); println!("✅ User registered and logged in"); diff --git a/tests/performance_load_tests.rs b/tests/performance_load_tests.rs index 65f3c54..4003001 100644 --- a/tests/performance_load_tests.rs +++ b/tests/performance_load_tests.rs @@ -19,6 +19,7 @@ use std::time::{Duration, Instant}; use tokio::sync::Semaphore; use tokio::time::sleep; use uuid::Uuid; +use chrono; use readur::models::{CreateUser, LoginRequest, LoginResponse, UserRole, DocumentResponse}; @@ -130,12 +131,14 @@ impl LoadTestClient { /// Setup a test user for load testing async fn setup_user(&mut self, user_index: usize) -> Result> { - let timestamp = std::time::SystemTime::now() + // Use UUID for guaranteed uniqueness across concurrent test execution + let test_id = Uuid::new_v4().simple().to_string(); + let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() - .as_millis(); - let username = format!("load_test_user_{}_{}", user_index, timestamp); - let email = format!("load_test_{}@example.com", timestamp); + .as_nanos(); + let username = format!("load_test_user_{}_{}_{}_{}_{}", user_index, test_id, nanos, Uuid::new_v4().simple(), chrono::Utc::now().timestamp_millis()); + let email = format!("load_test_{}_{}@{}.example.com", user_index, test_id, nanos); let password = "loadtestpassword123"; // Register user diff --git a/tests/simple_throttling_test.rs b/tests/simple_throttling_test.rs index 95faadd..061ac82 100644 --- a/tests/simple_throttling_test.rs +++ b/tests/simple_throttling_test.rs @@ -22,7 +22,7 @@ use readur::{ fn get_test_db_url() -> String { std::env::var("DATABASE_URL") .or_else(|_| std::env::var("TEST_DATABASE_URL")) - .unwrap_or_else(|_| "postgresql://postgres:postgres@localhost:5432/readur_test".to_string()) + .unwrap_or_else(|_| "postgresql://readur:readur@localhost:5432/readur".to_string()) } struct SimpleThrottleTest { @@ -56,10 +56,14 @@ impl SimpleThrottleTest { async fn create_test_user(&self) -> Result { let user_id = Uuid::new_v4(); - let timestamp = std::time::SystemTime::now() + // Use UUID for guaranteed uniqueness across concurrent test execution + let test_id = Uuid::new_v4().simple().to_string(); + let nanos = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() - .as_millis(); + .as_nanos(); + let username = format!("throttle_test_{}_{}_{}", test_id, nanos, Uuid::new_v4().simple()); + let email = format!("throttle_{}_{}@{}.example.com", test_id, nanos, Uuid::new_v4().simple()); sqlx::query( r#" @@ -68,8 +72,8 @@ impl SimpleThrottleTest { "# ) .bind(user_id) - .bind(format!("throttle_test_{}", timestamp)) - .bind(format!("throttle_{}@example.com", timestamp)) + .bind(&username) + .bind(&email) .bind("test_hash") .execute(&self.pool) .await?; diff --git a/tests/source_scheduler_simple_tests.rs b/tests/source_scheduler_simple_tests.rs index 34594a9..565bf6f 100644 --- a/tests/source_scheduler_simple_tests.rs +++ b/tests/source_scheduler_simple_tests.rs @@ -19,8 +19,12 @@ use readur::{ /// Create a test app state async fn create_test_app_state() -> Arc { + let database_url = std::env::var("TEST_DATABASE_URL") + .or_else(|_| std::env::var("DATABASE_URL")) + .unwrap_or_else(|_| "postgresql://readur:readur@localhost:5432/readur".to_string()); + let config = Config { - database_url: "sqlite::memory:".to_string(), + database_url, server_address: "127.0.0.1:8080".to_string(), jwt_secret: "test_secret".to_string(), upload_path: "/tmp/test_uploads".to_string(),