Merge branch 'devel' of git.kundeng.us:phoenix/icarus_auth into register_endpoint-init
All checks were successful
Rust Build / Check (pull_request) Successful in 41s
Rust Build / Test Suite (pull_request) Successful in 52s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / Clippy (pull_request) Successful in 47s
Rust Build / build (pull_request) Successful in 1m12s
All checks were successful
Rust Build / Check (pull_request) Successful in 41s
Rust Build / Test Suite (pull_request) Successful in 52s
Rust Build / Rustfmt (pull_request) Successful in 26s
Rust Build / Clippy (pull_request) Successful in 47s
Rust Build / build (pull_request) Successful in 1m12s
This commit is contained in:
@@ -1,2 +1 @@
|
|||||||
DATABASE_URL=postgres://username:password@localhost/database_name
|
DATABASE_URL=postgres://username:password@localhost/database_name
|
||||||
TEST_DATABASE_URL=postgres://icarus_op_test:password@localhost/icarus_auth_test
|
|
||||||
|
@@ -27,6 +27,7 @@ jobs:
|
|||||||
|
|
||||||
eval $(ssh-agent -s)
|
eval $(ssh-agent -s)
|
||||||
ssh-add -v ~/.ssh/icarus_models_deploy_key
|
ssh-add -v ~/.ssh/icarus_models_deploy_key
|
||||||
|
|
||||||
cargo check
|
cargo check
|
||||||
|
|
||||||
test:
|
test:
|
||||||
@@ -67,21 +68,10 @@ jobs:
|
|||||||
echo "Docker environment check complete."
|
echo "Docker environment check complete."
|
||||||
# NOTE: Do NOT use continue-on-error here.
|
# NOTE: Do NOT use continue-on-error here.
|
||||||
# If Docker isn't working as expected, the job SHOULD fail 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
|
- name: Run tests
|
||||||
env:
|
env:
|
||||||
# Define TEST_DATABASE_URL for tests to use
|
# Define 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' }}
|
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
|
RUST_LOG: info # Optional: configure test log level
|
||||||
# Make SSH agent available if tests fetch private dependencies
|
# Make SSH agent available if tests fetch private dependencies
|
||||||
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
SSH_AUTH_SOCK: ${{ env.SSH_AUTH_SOCK }}
|
||||||
@@ -93,6 +83,7 @@ jobs:
|
|||||||
|
|
||||||
eval $(ssh-agent -s)
|
eval $(ssh-agent -s)
|
||||||
ssh-add -v ~/.ssh/icarus_models_deploy_key
|
ssh-add -v ~/.ssh/icarus_models_deploy_key
|
||||||
|
|
||||||
cargo test
|
cargo test
|
||||||
|
|
||||||
fmt:
|
fmt:
|
||||||
|
@@ -20,5 +20,6 @@ icarus_models = { git = "ssh://git@git.kundeng.us/phoenix/icarus_models.git", ta
|
|||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
http-body-util = { version = "0.1.3" }
|
http-body-util = { version = "0.1.3" }
|
||||||
|
url = { version = "2.5" }
|
||||||
reqwest = { version = "0.12.5", features = ["json"] } # For making HTTP requests in tests
|
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
|
once_cell = { version = "1.19" } # Useful for lazy initialization in tests/app setup
|
||||||
|
13
src/lib.rs
13
src/lib.rs
@@ -4,19 +4,12 @@ pub mod hashing;
|
|||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod repo;
|
pub mod repo;
|
||||||
|
|
||||||
mod keys {
|
pub mod keys {
|
||||||
pub const DBURL: &str = "DATABASE_URL";
|
pub const DBURL: &str = "DATABASE_URL";
|
||||||
|
|
||||||
pub mod error {
|
pub mod error {
|
||||||
pub const ERROR: &str = "DATABASE_URL must be set in .env";
|
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 {
|
mod connection_settings {
|
||||||
@@ -44,10 +37,6 @@ pub mod db_pool {
|
|||||||
#[cfg(debug_assertions)] // Example: Only load .env in debug builds
|
#[cfg(debug_assertions)] // Example: Only load .env in debug builds
|
||||||
dotenvy::dotenv().ok();
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
95
src/main.rs
95
src/main.rs
@@ -59,6 +59,7 @@ mod init {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
body::Body,
|
body::Body,
|
||||||
http::{Request, StatusCode},
|
http::{Request, StatusCode},
|
||||||
@@ -74,6 +75,79 @@ mod tests {
|
|||||||
pub data: icarus_auth::models::common::User,
|
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<sqlx::PgPool, sqlx::Error> {
|
||||||
|
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<sqlx::PgPool, sqlx::Error> {
|
||||||
|
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<String, Box<dyn std::error::Error>> {
|
||||||
|
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]
|
#[tokio::test]
|
||||||
async fn test_hello_world() {
|
async fn test_hello_world() {
|
||||||
let app = init::app().await;
|
let app = init::app().await;
|
||||||
@@ -98,12 +172,23 @@ mod tests {
|
|||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_register_user() {
|
async fn test_register_user() {
|
||||||
let pool = icarus_auth::db_pool::create_pool()
|
let tm_pool = db_mgr::get_pool().await.unwrap();
|
||||||
.await
|
|
||||||
.expect("Failed to create pool");
|
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;
|
db::migrations(&pool).await;
|
||||||
|
|
||||||
let tx = pool.begin().await.unwrap();
|
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
let app = init::routes().await.layer(axum::Extension(pool));
|
||||||
|
|
||||||
let usr = icarus_auth::models::common::CreateUser {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user