mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-07 18:07:42 -06:00
refactor: allow optional database in setup, use derived default
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user