refactor: allow optional database in setup, use derived default

This commit is contained in:
Ryan Walters
2025-09-18 22:58:38 -05:00
parent e2c725cb95
commit cc06cd88a1
10 changed files with 95 additions and 59 deletions

View File

@@ -44,6 +44,16 @@ pub struct AppState {
impl AppState {
pub async fn new(config: Config, auth: AuthRegistry, db: PgPool, shutdown_notify: Arc<Notify>) -> Self {
Self::new_with_database(config, auth, db, shutdown_notify, true).await
}
pub async fn new_with_database(
config: Config,
auth: AuthRegistry,
db: PgPool,
shutdown_notify: Arc<Notify>,
use_database: bool,
) -> Self {
let jwt_secret = config.jwt_secret.clone();
// Initialize image storage
@@ -67,8 +77,8 @@ impl AppState {
healthchecker_task: Arc::new(RwLock::new(None)),
};
// Start the healthchecker task
{
// Start the healthchecker task only if database is being used
if use_database {
let health_state = app_state.health.clone();
let db_pool = app_state.db.clone();
let healthchecker_task = app_state.healthchecker_task.clone();

View File

@@ -51,7 +51,7 @@ impl DiscordProvider {
Arc::new(Self {
client,
http,
pkce: PkceManager::new(),
pkce: PkceManager::default(),
})
}

View File

@@ -64,7 +64,7 @@ impl GitHubProvider {
Arc::new(Self {
client,
http,
pkce: PkceManager::new(),
pkce: PkceManager::default(),
})
}
}

View File

@@ -10,6 +10,7 @@ pub struct PkceRecord {
pub created_at: Instant,
}
#[derive(Default)]
pub struct PkceManager {
pkce: DashMap<String, PkceRecord>,
last_purge_at_secs: AtomicU32,
@@ -17,14 +18,6 @@ pub struct PkceManager {
}
impl PkceManager {
pub fn new() -> Self {
Self {
pkce: DashMap::new(),
last_purge_at_secs: AtomicU32::new(0),
pkce_additions: AtomicU32::new(0),
}
}
pub fn generate_challenge(&self) -> (PkceCodeChallenge, String) {
let (pkce_challenge, pkce_verifier) = PkceCodeChallenge::new_random_sha256();
trace!("PKCE challenge generated");

View File

@@ -3,14 +3,19 @@ use tracing::{info, warn};
pub type PgPool = Pool<Postgres>;
pub async fn create_pool(database_url: &str, max_connections: u32) -> PgPool {
info!("Connecting to PostgreSQL");
PgPoolOptions::new()
.max_connections(max_connections)
.connect(database_url)
.await
.unwrap_or_else(|e| {
pub async fn create_pool(immediate: bool, database_url: &str, max_connections: u32) -> PgPool {
info!(immediate, "Connecting to PostgreSQL");
let options = PgPoolOptions::new().max_connections(max_connections);
if immediate {
options.connect(database_url).await.unwrap_or_else(|e| {
warn!(error = %e, "Failed to connect to PostgreSQL");
panic!("database connect failed: {}", e);
})
} else {
options
.connect_lazy(database_url)
.expect("Failed to create lazy database pool")
}
}

View File

@@ -35,6 +35,7 @@ pub async fn find_user_by_email(pool: &sqlx::PgPool, email: &str) -> Result<Opti
.await
}
#[allow(clippy::too_many_arguments)]
pub async fn link_oauth_account(
pool: &sqlx::PgPool,
user_id: i64,

View File

@@ -40,7 +40,7 @@ async fn main() {
let addr = std::net::SocketAddr::new(config.host, config.port);
let shutdown_timeout = std::time::Duration::from_secs(config.shutdown_timeout_seconds as u64);
let auth = AuthRegistry::new(&config).expect("auth initializer");
let db = data::pool::create_pool(&config.database_url, 10).await;
let db = data::pool::create_pool(true, &config.database_url, 10).await;
// Run database migrations at startup
if let Err(e) = sqlx::migrate!("./migrations").run(&db).await {

View File

@@ -377,7 +377,7 @@ pub async fn health_handler(
) -> axum::response::Response {
// Force health check in debug mode
#[cfg(debug_assertions)]
if params.get("force").is_some() {
if params.contains_key("force") {
app_state.check_health().await;
}

View File

@@ -14,22 +14,34 @@ use tokio::sync::Notify;
/// Test configuration for integration tests
pub struct TestConfig {
pub database_url: String,
pub container: ContainerAsync<GenericImage>,
pub database_url: Option<String>,
pub container: Option<ContainerAsync<GenericImage>>,
pub config: Config,
}
impl TestConfig {
/// Create a test configuration with a test database
pub async fn new() -> Self {
Self::new_with_database(true).await
}
/// Create a test configuration with optional database setup
pub async fn new_with_database(use_database: bool) -> Self {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Failed to install default crypto provider");
let (database_url, container) = setup_test_database("testdb", "testuser", "testpass").await;
let (database_url, container) = if use_database {
let (url, container) = setup_test_database("testdb", "testuser", "testpass").await;
(Some(url), Some(container))
} else {
(None, None)
};
let config = Config {
database_url: database_url.clone(),
database_url: database_url
.clone()
.unwrap_or_else(|| "postgresql://dummy:dummy@localhost:5432/dummy?sslmode=disable".to_string()),
discord_client_id: "test_discord_client_id".to_string(),
discord_client_secret: "test_discord_client_secret".to_string(),
github_client_id: "test_github_client_id".to_string(),
@@ -76,27 +88,44 @@ async fn setup_test_database(db: &str, user: &str, password: &str) -> (String, C
/// Create a test app state with database and auth registry
pub async fn create_test_app_state(test_config: &TestConfig) -> AppState {
// Create database pool
let db = pacman_server::data::pool::create_pool(&test_config.database_url, 5).await;
create_test_app_state_with_database(test_config, true).await
}
// Run migrations
sqlx::migrate!("./migrations")
.run(&db)
.await
.expect("Failed to run database migrations");
/// Create a test app state with optional database setup
pub async fn create_test_app_state_with_database(test_config: &TestConfig, use_database: bool) -> AppState {
let db = if use_database {
// Create database pool
let db_url = test_config
.database_url
.as_ref()
.expect("Database URL required when use_database is true");
let db = pacman_server::data::pool::create_pool(use_database, db_url, 5).await;
// Run migrations
sqlx::migrate!("./migrations")
.run(&db)
.await
.expect("Failed to run database migrations");
db
} else {
// Create a dummy database pool that will fail gracefully
let dummy_url = "postgresql://dummy:dummy@localhost:5432/dummy?sslmode=disable";
pacman_server::data::pool::create_pool(false, dummy_url, 1).await
};
// Create auth registry
let auth = AuthRegistry::new(&test_config.config).expect("Failed to create auth registry");
// Create app state
let notify = Arc::new(Notify::new());
let app_state = AppState::new(test_config.config.clone(), auth, db, notify).await;
let app_state = AppState::new_with_database(test_config.config.clone(), auth, db, notify, use_database).await;
// Set health status to true for tests (migrations and database are both working)
// Set health status based on database usage
{
let mut health = app_state.health.write().await;
health.set_migrations(true);
health.set_database(true);
health.set_migrations(use_database);
health.set_database(use_database);
}
app_state

View File

@@ -3,13 +3,12 @@ use mockall::predicate::*;
use pretty_assertions::assert_eq;
mod common;
use common::{create_test_app_state, create_test_router, TestConfig};
// OAuth provider imports removed as they're not used in these health tests
use common::{create_test_app_state, create_test_app_state_with_database, create_test_router, TestConfig};
/// Common setup function for all tests
async fn setup_test_server() -> TestServer {
let test_config = TestConfig::new().await;
let app_state = create_test_app_state(&test_config).await;
/// Setup function with optional database
async fn setup_test_server(use_database: bool) -> TestServer {
let test_config = TestConfig::new_with_database(use_database).await;
let app_state = create_test_app_state_with_database(&test_config, use_database).await;
let router = create_test_router(app_state);
TestServer::new(router).unwrap()
}
@@ -17,12 +16,11 @@ async fn setup_test_server() -> TestServer {
/// Test basic endpoints functionality
#[tokio::test]
async fn test_basic_endpoints() {
let server = setup_test_server().await;
let server = setup_test_server(false).await;
// Test root endpoint
let response = server.get("/").await;
assert_eq!(response.status_code(), 200);
assert_eq!(response.text(), "Hello, World! Visit /auth/github to start OAuth flow.");
}
/// Test health endpoint functionality with real database connectivity
@@ -53,7 +51,7 @@ async fn test_health_endpoint() {
/// Test OAuth provider listing and configuration
#[tokio::test]
async fn test_oauth_provider_configuration() {
let server = setup_test_server().await;
let server = setup_test_server(false).await;
// Test providers list endpoint
let response = server.get("/auth/providers").await;
@@ -85,7 +83,7 @@ async fn test_oauth_provider_configuration() {
/// Test OAuth authorization flows
#[tokio::test]
async fn test_oauth_authorization_flows() {
let server = setup_test_server().await;
let server = setup_test_server(false).await;
// Test OAuth authorize endpoint (should redirect)
let response = server.get("/auth/github").await;
@@ -103,7 +101,7 @@ async fn test_oauth_authorization_flows() {
/// Test OAuth callback handling
#[tokio::test]
async fn test_oauth_callback_handling() {
let server = setup_test_server().await;
let server = setup_test_server(false).await;
// Test OAuth callback with missing parameters (should fail gracefully)
let response = server.get("/auth/github/callback").await;
@@ -113,7 +111,7 @@ async fn test_oauth_callback_handling() {
/// Test session management endpoints
#[tokio::test]
async fn test_session_management() {
let server = setup_test_server().await;
let server = setup_test_server(false).await;
// Test logout endpoint (should redirect)
let response = server.get("/logout").await;
@@ -127,7 +125,7 @@ async fn test_session_management() {
/// Test that verifies database operations work correctly
#[tokio::test]
async fn test_database_operations() {
let server = setup_test_server().await;
let server = setup_test_server(true).await;
// Act: Test health endpoint to verify database connectivity
let response = server.get("/health").await;
@@ -141,7 +139,7 @@ async fn test_database_operations() {
/// Test OAuth authorization flow
#[tokio::test]
async fn test_oauth_authorization_flow() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth authorize handler redirects to the provider's authorization page for valid providers
// TODO: Test that the OAuth authorize handler returns an error for unknown providers
@@ -151,7 +149,7 @@ async fn test_oauth_authorization_flow() {
/// Test OAuth callback validation
#[tokio::test]
async fn test_oauth_callback_validation() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler validates the provider exists before processing
// TODO: Test that the OAuth callback handler returns an error when the provider returns an OAuth error
@@ -162,7 +160,7 @@ async fn test_oauth_callback_validation() {
/// Test OAuth callback processing
#[tokio::test]
async fn test_oauth_callback_processing() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler exchanges the authorization code for user information successfully
// TODO: Test that the OAuth callback handler handles provider callback errors gracefully
@@ -174,7 +172,7 @@ async fn test_oauth_callback_processing() {
/// Test account linking flow
#[tokio::test]
async fn test_account_linking_flow() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler links a new provider to an existing user when link intent is present and session is valid
// TODO: Test that the OAuth callback handler redirects to profile after successful account linking
@@ -184,7 +182,7 @@ async fn test_account_linking_flow() {
/// Test new user registration
#[tokio::test]
async fn test_new_user_registration() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler creates a new user account when no existing user is found
// TODO: Test that the OAuth callback handler requires an email address for all sign-ins
@@ -194,7 +192,7 @@ async fn test_new_user_registration() {
/// Test existing user sign-in
#[tokio::test]
async fn test_existing_user_sign_in() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler allows sign-in when the provider is already linked to an existing user
// TODO: Test that the OAuth callback handler requires explicit linking when a user with the same email exists and has other providers linked
@@ -204,7 +202,7 @@ async fn test_existing_user_sign_in() {
/// Test avatar processing
#[tokio::test]
async fn test_avatar_processing() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the OAuth callback handler processes user avatars asynchronously without blocking the response
// TODO: Test that the OAuth callback handler handles avatar processing errors gracefully
@@ -213,7 +211,7 @@ async fn test_avatar_processing() {
/// Test profile access
#[tokio::test]
async fn test_profile_access() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the profile handler returns user information when a valid session exists
// TODO: Test that the profile handler returns an error when no session cookie is present
@@ -225,7 +223,7 @@ async fn test_profile_access() {
/// Test logout functionality
#[tokio::test]
async fn test_logout_functionality() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the logout handler clears the session if a session was there
// TODO: Test that the logout handler removes the session from memory storage
@@ -236,7 +234,7 @@ async fn test_logout_functionality() {
/// Test provider configuration
#[tokio::test]
async fn test_provider_configuration() {
let _server = setup_test_server().await;
let _server = setup_test_server(false).await;
// TODO: Test that the providers list handler returns all configured OAuth providers
// TODO: Test that the providers list handler includes provider status (active/inactive)