feat(ci): use environment variables for database url

This commit is contained in:
perf3ct 2025-06-20 22:33:18 +00:00
parent eb77908353
commit cf01284fbe
7 changed files with 55 additions and 22 deletions

View File

@ -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 }}

View File

@ -56,9 +56,12 @@ fn create_test_user_with_suffix(suffix: &str) -> CreateUser {
async fn create_test_app_state() -> Result<Arc<AppState>> {
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<Arc<AppState>> {
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)
);

View File

@ -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<Uuid> {
@ -60,7 +64,7 @@ fn create_test_document(user_id: Uuid, filename: &str, file_hash: Option<String>
#[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)

View File

@ -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<Self> {
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(

View File

@ -94,8 +94,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
async fn create_test_app_state() -> Result<Arc<AppState>> {
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<Arc<AppState>> {
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)
);

View File

@ -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());

View File

@ -98,8 +98,11 @@ async fn create_test_user(db: &Database, username: &str) -> Result<Uuid> {
async fn create_test_app_state() -> Result<Arc<AppState>> {
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<Arc<AppState>> {
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)
);