From bdcfa77d21533b4693682f4a4565cd04625d5c7a Mon Sep 17 00:00:00 2001 From: perf3ct Date: Fri, 20 Jun 2025 22:33:18 +0000 Subject: [PATCH] feat(ci): use environment variables for database url --- .github/workflows/test-integration.yml | 15 +++++++++++++-- tests/document_upload_hash_duplicate_tests.rs | 9 ++++++--- tests/hash_duplicate_detection_tests.rs | 18 +++++++++++------- tests/simple_throttling_test.rs | 11 ++++++++--- tests/source_sync_hash_duplicate_tests.rs | 7 +++++-- tests/throttled_high_concurrency_test.rs | 10 +++++++--- tests/webdav_hash_duplicate_tests.rs | 7 +++++-- 7 files changed, 55 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test-integration.yml b/.github/workflows/test-integration.yml index a462271..3b580ac 100644 --- a/.github/workflows/test-integration.yml +++ b/.github/workflows/test-integration.yml @@ -37,6 +37,13 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Remove local env files to prevent conflicts + run: | + # Remove or rename env files so they don't override CI environment variables + [ -f .env ] && mv .env .env.backup || true + [ -f .env.test ] && mv .env.test .env.test.backup || true + echo "Removed local env files to ensure CI env vars take precedence" + - name: Install system dependencies run: | sudo apt-get update @@ -68,6 +75,7 @@ jobs: - name: Start readur server run: | + echo "Starting server with DATABASE_URL: $DATABASE_URL" ./target/release/readur > server.log 2>&1 & echo $! > readur.pid sleep 2 @@ -75,9 +83,9 @@ jobs: env: DATABASE_URL: ${{ env.DATABASE_URL }} JWT_SECRET: test-secret-key - PORT: 8000 + SERVER_ADDRESS: 0.0.0.0:8000 UPLOAD_PATH: ./uploads - WATCH_PATH: ./watch + WATCH_FOLDER: ./watch - name: Wait for server to be ready run: | @@ -116,6 +124,9 @@ jobs: - name: Run integration tests run: | + echo "Running tests with DATABASE_URL: $DATABASE_URL" + echo "Environment check:" + env | grep -E "(DATABASE_URL|JWT_SECRET|API_URL)" | sort cargo test --test '*' -- --test-threads=1 env: DATABASE_URL: ${{ env.DATABASE_URL }} diff --git a/tests/document_upload_hash_duplicate_tests.rs b/tests/document_upload_hash_duplicate_tests.rs index 6fe8d9d..6730dce 100644 --- a/tests/document_upload_hash_duplicate_tests.rs +++ b/tests/document_upload_hash_duplicate_tests.rs @@ -56,9 +56,12 @@ fn create_test_user_with_suffix(suffix: &str) -> CreateUser { async fn create_test_app_state() -> Result> { let config = Config::from_env().unwrap_or_else(|_| { - // Create a test config if env fails + // Create a test config if env fails - use DATABASE_URL env var or fallback + let database_url = 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()); Config { - database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(), + database_url, server_address: "127.0.0.1:8000".to_string(), jwt_secret: "test-secret".to_string(), upload_path: "./test-uploads".to_string(), @@ -75,7 +78,7 @@ async fn create_test_app_state() -> Result> { cpu_priority: "normal".to_string(), } }); - let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?; + let db = Database::new(&config.database_url).await?; let queue_service = std::sync::Arc::new( readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1) ); diff --git a/tests/hash_duplicate_detection_tests.rs b/tests/hash_duplicate_detection_tests.rs index c43f1ff..2037519 100644 --- a/tests/hash_duplicate_detection_tests.rs +++ b/tests/hash_duplicate_detection_tests.rs @@ -10,7 +10,11 @@ use readur::{ models::{Document, CreateUser, UserRole}, }; -const TEST_DB_URL: &str = "postgresql://readur:readur@localhost:5432/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()) +} // Helper function to create a test user with unique identifier async fn create_test_user(db: &Database, username: &str) -> Result { @@ -60,7 +64,7 @@ fn create_test_document(user_id: Uuid, filename: &str, file_hash: Option #[tokio::test] async fn test_get_document_by_user_and_hash_found() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user_id = create_test_user(&db, "testuser1").await?; let file_hash = "abcd1234567890"; @@ -82,7 +86,7 @@ async fn test_get_document_by_user_and_hash_found() -> Result<()> { #[tokio::test] async fn test_get_document_by_user_and_hash_not_found() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user_id = Uuid::new_v4(); let non_existent_hash = "nonexistent1234567890"; @@ -96,7 +100,7 @@ async fn test_get_document_by_user_and_hash_not_found() -> Result<()> { #[tokio::test] async fn test_get_document_by_user_and_hash_different_user() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user1_id = create_test_user(&db, "testuser2").await?; let user2_id = create_test_user(&db, "testuser3").await?; let file_hash = "shared_hash_1234567890"; @@ -115,7 +119,7 @@ async fn test_get_document_by_user_and_hash_different_user() -> Result<()> { #[tokio::test] async fn test_duplicate_hash_prevention_same_user() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user_id = create_test_user(&db, "testuser4").await?; let file_hash = "duplicate_hash_1234567890"; @@ -136,7 +140,7 @@ async fn test_duplicate_hash_prevention_same_user() -> Result<()> { #[tokio::test] async fn test_same_hash_different_users_allowed() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user1_id = create_test_user(&db, "testuser5").await?; let user2_id = create_test_user(&db, "testuser6").await?; let file_hash = "shared_content_hash_1234567890"; @@ -164,7 +168,7 @@ async fn test_same_hash_different_users_allowed() -> Result<()> { #[tokio::test] async fn test_null_hash_allowed_multiple() -> Result<()> { - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; let user_id = create_test_user(&db, "testuser7").await?; // Create multiple documents with null hash (should be allowed) diff --git a/tests/simple_throttling_test.rs b/tests/simple_throttling_test.rs index 90823dc..95faadd 100644 --- a/tests/simple_throttling_test.rs +++ b/tests/simple_throttling_test.rs @@ -19,7 +19,11 @@ use readur::{ }; // Use the same database URL as the running server -const DB_URL: &str = "postgresql://readur:readur@localhost:5432/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()) +} struct SimpleThrottleTest { pool: PgPool, @@ -28,13 +32,14 @@ struct SimpleThrottleTest { impl SimpleThrottleTest { async fn new() -> Result { + let db_url = get_test_db_url(); let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(20) .acquire_timeout(Duration::from_secs(10)) - .connect(DB_URL) + .connect(&db_url) .await?; - let db = Database::new(DB_URL).await?; + let db = Database::new(&db_url).await?; // Create queue service with throttling (max 15 concurrent jobs) let queue_service = Arc::new(OcrQueueService::new( diff --git a/tests/source_sync_hash_duplicate_tests.rs b/tests/source_sync_hash_duplicate_tests.rs index 23af08c..baaa25d 100644 --- a/tests/source_sync_hash_duplicate_tests.rs +++ b/tests/source_sync_hash_duplicate_tests.rs @@ -94,8 +94,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result { async fn create_test_app_state() -> Result> { let config = Config::from_env().unwrap_or_else(|_| { + let database_url = 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()); Config { - database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(), + database_url, server_address: "127.0.0.1:8000".to_string(), jwt_secret: "test-secret".to_string(), upload_path: "./test-uploads".to_string(), @@ -112,7 +115,7 @@ async fn create_test_app_state() -> Result> { cpu_priority: "normal".to_string(), } }); - let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?; + let db = Database::new(&config.database_url).await?; let queue_service = std::sync::Arc::new( readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1) ); diff --git a/tests/throttled_high_concurrency_test.rs b/tests/throttled_high_concurrency_test.rs index d7f9155..3854d66 100644 --- a/tests/throttled_high_concurrency_test.rs +++ b/tests/throttled_high_concurrency_test.rs @@ -24,7 +24,11 @@ use readur::{ request_throttler::RequestThrottler, }; -const TEST_DB_URL: &str = "postgresql://readur:readur@localhost:5432/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()) +} struct ThrottledTestHarness { db: Database, @@ -40,10 +44,10 @@ impl ThrottledTestHarness { let pool = sqlx::postgres::PgPoolOptions::new() .max_connections(30) // Higher limit for stress testing .acquire_timeout(std::time::Duration::from_secs(15)) - .connect(TEST_DB_URL) + .connect(&get_test_db_url()) .await?; - let db = Database::new(TEST_DB_URL).await?; + let db = Database::new(&get_test_db_url()).await?; // Initialize services let file_service = FileService::new("./test_uploads".to_string()); diff --git a/tests/webdav_hash_duplicate_tests.rs b/tests/webdav_hash_duplicate_tests.rs index 4af34e4..9fa6eb7 100644 --- a/tests/webdav_hash_duplicate_tests.rs +++ b/tests/webdav_hash_duplicate_tests.rs @@ -98,8 +98,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result { async fn create_test_app_state() -> Result> { let config = Config::from_env().unwrap_or_else(|_| { + let database_url = 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()); Config { - database_url: "postgresql://readur:readur@localhost:5432/readur".to_string(), + database_url, server_address: "127.0.0.1:8000".to_string(), jwt_secret: "test-secret".to_string(), upload_path: "./test-uploads".to_string(), @@ -116,7 +119,7 @@ async fn create_test_app_state() -> Result> { cpu_priority: "normal".to_string(), } }); - let db = Database::new("postgresql://readur:readur@localhost:5432/readur").await?; + let db = Database::new(&config.database_url).await?; let queue_service = std::sync::Arc::new( readur::ocr_queue::OcrQueueService::new(db.clone(), db.get_pool().clone(), 1) );