mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 05:15:49 -06:00
tests: setup basic tests, integration tests with testcontainers
This commit is contained in:
105
pacman-server/tests/common/mod.rs
Normal file
105
pacman-server/tests/common/mod.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use axum::Router;
|
||||
use pacman_server::{
|
||||
app::{create_router, AppState},
|
||||
auth::AuthRegistry,
|
||||
config::Config,
|
||||
};
|
||||
use testcontainers::{
|
||||
core::{IntoContainerPort, WaitFor},
|
||||
runners::AsyncRunner,
|
||||
ContainerAsync, GenericImage, ImageExt,
|
||||
};
|
||||
|
||||
/// Test configuration for integration tests
|
||||
pub struct TestConfig {
|
||||
pub database_url: String,
|
||||
pub _container: ContainerAsync<GenericImage>,
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl TestConfig {
|
||||
/// Create a test configuration with a test database
|
||||
pub async fn new() -> 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 config = Config {
|
||||
database_url: database_url.clone(),
|
||||
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(),
|
||||
github_client_secret: "test_github_client_secret".to_string(),
|
||||
s3_access_key: "test_s3_access_key".to_string(),
|
||||
s3_secret_access_key: "test_s3_secret_access_key".to_string(),
|
||||
s3_bucket_name: "test_bucket".to_string(),
|
||||
s3_public_base_url: "https://test.example.com".to_string(),
|
||||
port: 0, // Will be set by test server
|
||||
host: "127.0.0.1".parse().unwrap(),
|
||||
shutdown_timeout_seconds: 5,
|
||||
public_base_url: "http://localhost:3000".to_string(),
|
||||
jwt_secret: "test_jwt_secret_key_for_testing_only".to_string(),
|
||||
};
|
||||
|
||||
Self {
|
||||
database_url,
|
||||
_container: container,
|
||||
config,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Set up a test PostgreSQL database using testcontainers
|
||||
async fn setup_test_database(db: &str, user: &str, password: &str) -> (String, ContainerAsync<GenericImage>) {
|
||||
let container = GenericImage::new("postgres", "15")
|
||||
.with_exposed_port(5432.tcp())
|
||||
.with_wait_for(WaitFor::message_on_stderr("database system is ready to accept connections"))
|
||||
.with_env_var("POSTGRES_DB", db)
|
||||
.with_env_var("POSTGRES_USER", user)
|
||||
.with_env_var("POSTGRES_PASSWORD", password)
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let host = container.get_host().await.unwrap();
|
||||
let port = container.get_host_port_ipv4(5432).await.unwrap();
|
||||
|
||||
(
|
||||
format!("postgresql://{user}:{password}@{host}:{port}/{db}?sslmode=disable"),
|
||||
container,
|
||||
)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
// Run migrations
|
||||
sqlx::migrate!("./migrations")
|
||||
.run(&db)
|
||||
.await
|
||||
.expect("Failed to run database migrations");
|
||||
|
||||
// Create auth registry
|
||||
let auth = AuthRegistry::new(&test_config.config).expect("Failed to create auth registry");
|
||||
|
||||
// Create app state
|
||||
let app_state = AppState::new(test_config.config.clone(), auth, db);
|
||||
|
||||
// Set health status to true for tests (migrations and database are both working)
|
||||
{
|
||||
let mut health = app_state.health.write().await;
|
||||
health.set_migrations(true);
|
||||
health.set_database(true);
|
||||
}
|
||||
|
||||
app_state
|
||||
}
|
||||
|
||||
/// Create a test router with the given app state
|
||||
pub fn create_test_router(app_state: AppState) -> Router {
|
||||
create_router(app_state)
|
||||
}
|
||||
138
pacman-server/tests/oauth_integration.rs
Normal file
138
pacman-server/tests/oauth_integration.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
use axum_test::TestServer;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
mod common;
|
||||
use common::{create_test_app_state, 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;
|
||||
let router = create_test_router(app_state);
|
||||
TestServer::new(router).unwrap()
|
||||
}
|
||||
|
||||
/// Test basic endpoints functionality
|
||||
#[tokio::test]
|
||||
async fn test_basic_endpoints() {
|
||||
let server = setup_test_server().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
|
||||
#[tokio::test]
|
||||
async fn test_health_endpoint() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Test health endpoint - wait for health checker to complete initial run
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(250)).await;
|
||||
|
||||
let mut health_ok = false;
|
||||
let start = tokio::time::Instant::now();
|
||||
let timeout = tokio::time::Duration::from_secs(3);
|
||||
while start.elapsed() < timeout {
|
||||
let response = server.get("/health").await;
|
||||
if response.status_code() == 200 {
|
||||
let health_json: serde_json::Value = response.json();
|
||||
if health_json["ok"] == true {
|
||||
health_ok = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
|
||||
}
|
||||
|
||||
assert!(health_ok, "Health endpoint did not return ok=true within 3 seconds");
|
||||
}
|
||||
|
||||
/// Test OAuth provider listing and configuration
|
||||
#[tokio::test]
|
||||
async fn test_oauth_provider_configuration() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Test providers list endpoint
|
||||
let response = server.get("/auth/providers").await;
|
||||
assert_eq!(response.status_code(), 200);
|
||||
let providers: Vec<serde_json::Value> = response.json();
|
||||
assert_eq!(providers.len(), 2); // Should have GitHub and Discord providers
|
||||
|
||||
// Verify provider structure
|
||||
let provider_ids: Vec<&str> = providers.iter().map(|p| p["id"].as_str().unwrap()).collect();
|
||||
assert!(provider_ids.contains(&"github"));
|
||||
assert!(provider_ids.contains(&"discord"));
|
||||
|
||||
// Verify provider details
|
||||
for provider in providers {
|
||||
let id = provider["id"].as_str().unwrap();
|
||||
let name = provider["name"].as_str().unwrap();
|
||||
let active = provider["active"].as_bool().unwrap();
|
||||
|
||||
assert!(active, "Provider {} should be active", id);
|
||||
|
||||
match id {
|
||||
"github" => assert_eq!(name, "GitHub"),
|
||||
"discord" => assert_eq!(name, "Discord"),
|
||||
_ => panic!("Unknown provider: {}", id),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Test OAuth authorization flows
|
||||
#[tokio::test]
|
||||
async fn test_oauth_authorization_flows() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Test OAuth authorize endpoint (should redirect)
|
||||
let response = server.get("/auth/github").await;
|
||||
assert_eq!(response.status_code(), 303); // Redirect to GitHub OAuth
|
||||
|
||||
// Test OAuth authorize endpoint for Discord
|
||||
let response = server.get("/auth/discord").await;
|
||||
assert_eq!(response.status_code(), 303); // Redirect to Discord OAuth
|
||||
|
||||
// Test unknown provider
|
||||
let response = server.get("/auth/unknown").await;
|
||||
assert_eq!(response.status_code(), 400); // Bad request for unknown provider
|
||||
}
|
||||
|
||||
/// Test OAuth callback handling
|
||||
#[tokio::test]
|
||||
async fn test_oauth_callback_handling() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Test OAuth callback with missing parameters (should fail gracefully)
|
||||
let response = server.get("/auth/github/callback").await;
|
||||
assert_eq!(response.status_code(), 400); // Bad request for missing code/state
|
||||
}
|
||||
|
||||
/// Test session management endpoints
|
||||
#[tokio::test]
|
||||
async fn test_session_management() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Test logout endpoint (should redirect)
|
||||
let response = server.get("/logout").await;
|
||||
assert_eq!(response.status_code(), 302); // Redirect to home
|
||||
|
||||
// Test profile endpoint without session (should be unauthorized)
|
||||
let response = server.get("/profile").await;
|
||||
assert_eq!(response.status_code(), 401); // Unauthorized without session
|
||||
}
|
||||
|
||||
/// Test that verifies database operations work correctly
|
||||
#[tokio::test]
|
||||
async fn test_database_operations() {
|
||||
let server = setup_test_server().await;
|
||||
|
||||
// Act: Test health endpoint to verify database connectivity
|
||||
let response = server.get("/health").await;
|
||||
|
||||
// Assert: Health should be OK, indicating database is connected and migrations ran
|
||||
assert_eq!(response.status_code(), 200);
|
||||
let health_json: serde_json::Value = response.json();
|
||||
assert_eq!(health_json["ok"], true);
|
||||
}
|
||||
Reference in New Issue
Block a user