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 { impl AppState {
pub async fn new(config: Config, auth: AuthRegistry, db: PgPool, shutdown_notify: Arc<Notify>) -> Self { 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(); let jwt_secret = config.jwt_secret.clone();
// Initialize image storage // Initialize image storage
@@ -67,8 +77,8 @@ impl AppState {
healthchecker_task: Arc::new(RwLock::new(None)), 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 health_state = app_state.health.clone();
let db_pool = app_state.db.clone(); let db_pool = app_state.db.clone();
let healthchecker_task = app_state.healthchecker_task.clone(); let healthchecker_task = app_state.healthchecker_task.clone();

View File

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

View File

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

View File

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

View File

@@ -3,14 +3,19 @@ use tracing::{info, warn};
pub type PgPool = Pool<Postgres>; pub type PgPool = Pool<Postgres>;
pub async fn create_pool(database_url: &str, max_connections: u32) -> PgPool { pub async fn create_pool(immediate: bool, database_url: &str, max_connections: u32) -> PgPool {
info!("Connecting to PostgreSQL"); info!(immediate, "Connecting to PostgreSQL");
PgPoolOptions::new()
.max_connections(max_connections) let options = PgPoolOptions::new().max_connections(max_connections);
.connect(database_url)
.await if immediate {
.unwrap_or_else(|e| { options.connect(database_url).await.unwrap_or_else(|e| {
warn!(error = %e, "Failed to connect to PostgreSQL"); warn!(error = %e, "Failed to connect to PostgreSQL");
panic!("database connect failed: {}", e); 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 .await
} }
#[allow(clippy::too_many_arguments)]
pub async fn link_oauth_account( pub async fn link_oauth_account(
pool: &sqlx::PgPool, pool: &sqlx::PgPool,
user_id: i64, user_id: i64,

View File

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

View File

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

View File

@@ -14,22 +14,34 @@ use tokio::sync::Notify;
/// Test configuration for integration tests /// Test configuration for integration tests
pub struct TestConfig { pub struct TestConfig {
pub database_url: String, pub database_url: Option<String>,
pub container: ContainerAsync<GenericImage>, pub container: Option<ContainerAsync<GenericImage>>,
pub config: Config, pub config: Config,
} }
impl TestConfig { impl TestConfig {
/// Create a test configuration with a test database /// Create a test configuration with a test database
pub async fn new() -> Self { 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() rustls::crypto::ring::default_provider()
.install_default() .install_default()
.expect("Failed to install default crypto provider"); .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 { 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_id: "test_discord_client_id".to_string(),
discord_client_secret: "test_discord_client_secret".to_string(), discord_client_secret: "test_discord_client_secret".to_string(),
github_client_id: "test_github_client_id".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 /// Create a test app state with database and auth registry
pub async fn create_test_app_state(test_config: &TestConfig) -> AppState { pub async fn create_test_app_state(test_config: &TestConfig) -> AppState {
// Create database pool create_test_app_state_with_database(test_config, true).await
let db = pacman_server::data::pool::create_pool(&test_config.database_url, 5).await; }
// Run migrations /// Create a test app state with optional database setup
sqlx::migrate!("./migrations") pub async fn create_test_app_state_with_database(test_config: &TestConfig, use_database: bool) -> AppState {
.run(&db) let db = if use_database {
.await // Create database pool
.expect("Failed to run database migrations"); 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 // Create auth registry
let auth = AuthRegistry::new(&test_config.config).expect("Failed to create auth registry"); let auth = AuthRegistry::new(&test_config.config).expect("Failed to create auth registry");
// Create app state // Create app state
let notify = Arc::new(Notify::new()); 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; let mut health = app_state.health.write().await;
health.set_migrations(true); health.set_migrations(use_database);
health.set_database(true); health.set_database(use_database);
} }
app_state app_state

View File

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