diff --git a/.env.sample b/.env.sample index 5cad34d..135f9aa 100644 --- a/.env.sample +++ b/.env.sample @@ -1,2 +1 @@ DATABASE_URL=postgres://username:password@localhost/database_name -TEST_DATABASE_URL=postgres://icarus_op_test:password@localhost/icarus_auth_test diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index 992cc5e..2303b81 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -27,6 +27,7 @@ jobs: eval $(ssh-agent -s) ssh-add -v ~/.ssh/icarus_models_deploy_key + cargo check test: @@ -67,21 +68,10 @@ jobs: echo "Docker environment check complete." # NOTE: Do NOT use continue-on-error here. # If Docker isn't working as expected, the job SHOULD fail here. - # --- Optional but Recommended: Database Migrations Step --- - - name: Run Database Migrations - env: - # Define TEST_DATABASE_URL using service details and secrets - TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} - # Make SSH agent available if migrations fetch private dependencies - SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} - run: | - echo "Running database migrations..." - cargo install sqlx-cli --no-default-features --features native-tls,postgres - sqlx database setup --database-url $TEST_DATABASE_URL - name: Run tests env: - # Define TEST_DATABASE_URL for tests to use - TEST_DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} + # Define DATABASE_URL for tests to use + DATABASE_URL: postgresql://${{ secrets.DB_TEST_USER || 'testuser' }}:${{ secrets.DB_TEST_PASSWORD || 'testpassword' }}@postgres:5432/${{ secrets.DB_TEST_NAME || 'testdb' }} RUST_LOG: info # Optional: configure test log level # Make SSH agent available if tests fetch private dependencies SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }} @@ -93,6 +83,7 @@ jobs: eval $(ssh-agent -s) ssh-add -v ~/.ssh/icarus_models_deploy_key + cargo test fmt: diff --git a/Cargo.toml b/Cargo.toml index c014972..7cbd4f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,6 @@ icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", ta [dev-dependencies] http-body-util = { version = "0.1.3" } +url = { version = "2.5" } reqwest = { version = "0.12.5", features = ["json"] } # For making HTTP requests in tests once_cell = { version = "1.19" } # Useful for lazy initialization in tests/app setup diff --git a/src/lib.rs b/src/lib.rs index ab8bf15..891aac4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,19 +4,12 @@ pub mod hashing; pub mod models; pub mod repo; -mod keys { +pub mod keys { pub const DBURL: &str = "DATABASE_URL"; pub mod error { pub const ERROR: &str = "DATABASE_URL must be set in .env"; } - - pub mod test { - pub const DBURL: &str = "TEST_DATABASE_URL"; - pub mod error { - pub const ERROR: &str = "TEST_DATABASE_URL must be set in .env"; - } - } } mod connection_settings { @@ -44,10 +37,6 @@ pub mod db_pool { #[cfg(debug_assertions)] // Example: Only load .env in debug builds dotenvy::dotenv().ok(); - if cfg!(debug_assertions) { - env::var(keys::test::DBURL).expect(keys::test::error::ERROR) - } else { - env::var(keys::DBURL).expect(keys::error::ERROR) - } + env::var(keys::DBURL).expect(keys::error::ERROR) } } diff --git a/src/main.rs b/src/main.rs index f4d0849..dfe28f5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,6 +59,7 @@ mod init { #[cfg(test)] mod tests { use super::*; + use axum::{ body::Body, http::{Request, StatusCode}, @@ -74,6 +75,79 @@ mod tests { pub data: icarus_auth::models::common::User, } + mod db_mgr { + use std::str::FromStr; + + use icarus_auth::keys; + + pub const LIMIT: usize = 6; + + pub async fn get_pool() -> Result { + let tm_db_url = std::env::var(keys::DBURL).expect("DATABASE_URL must be present"); + let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap(); + sqlx::PgPool::connect_with(tm_options).await + } + + pub async fn generate_db_name() -> String { + let db_name = + get_database_name().unwrap() + &"_" + &uuid::Uuid::new_v4().to_string()[..LIMIT]; + db_name + } + + pub async fn connect_to_db(db_name: &str) -> Result { + let db_url = std::env::var(keys::DBURL).expect("DATABASE_URL must be set for tests"); + let options = sqlx::postgres::PgConnectOptions::from_str(&db_url)?.database(db_name); + sqlx::PgPool::connect_with(options).await + } + + pub async fn create_database( + template_pool: &sqlx::PgPool, + db_name: &str, + ) -> Result<(), sqlx::Error> { + let create_query = format!("CREATE DATABASE {}", db_name); + match sqlx::query(&create_query).execute(template_pool).await { + Ok(_) => Ok(()), + Err(e) => Err(e), + } + } + + // Function to drop a database + pub async fn drop_database( + template_pool: &sqlx::PgPool, + db_name: &str, + ) -> Result<(), sqlx::Error> { + let drop_query = format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", db_name); + sqlx::query(&drop_query).execute(template_pool).await?; + Ok(()) + } + + pub fn get_database_name() -> Result> { + dotenvy::dotenv().ok(); // Load .env file if it exists + + match std::env::var(keys::DBURL) { + Ok(database_url) => { + let parsed_url = url::Url::parse(&database_url)?; + if parsed_url.scheme() == "postgres" || parsed_url.scheme() == "postgresql" { + match parsed_url + .path_segments() + .and_then(|segments| segments.last().map(|s| s.to_string())) + { + Some(sss) => Ok(sss), + None => Err("Error parsing".into()), + } + } else { + // Handle other database types if needed + Err("Error parsing".into()) + } + } + Err(_) => { + // DATABASE_URL environment variable not found + Err("Error parsing".into()) + } + } + } + } + #[tokio::test] async fn test_hello_world() { let app = init::app().await; @@ -98,12 +172,23 @@ mod tests { #[tokio::test] async fn test_register_user() { - let pool = icarus_auth::db_pool::create_pool() - .await - .expect("Failed to create pool"); + let tm_pool = db_mgr::get_pool().await.unwrap(); + + let db_name = db_mgr::generate_db_name().await; + + match db_mgr::create_database(&tm_pool, &db_name).await { + Ok(_) => { + println!("Success"); + } + Err(e) => { + assert!(false, "Error: {:?}", e.to_string()); + } + } + + let pool = db_mgr::connect_to_db(&db_name).await.unwrap(); + db::migrations(&pool).await; - let tx = pool.begin().await.unwrap(); let app = init::routes().await.layer(axum::Extension(pool)); let usr = icarus_auth::models::common::CreateUser { @@ -145,6 +230,6 @@ mod tests { } }; - tx.rollback().await.unwrap(); + let _ = db_mgr::drop_database(&tm_pool, &db_name).await; } }