tests: setup basic tests, integration tests with testcontainers

This commit is contained in:
Ryan Walters
2025-09-18 21:33:15 -05:00
parent 3ad00bdcba
commit 350f92ab21
7 changed files with 1127 additions and 56 deletions

857
Cargo.lock generated
View File

File diff suppressed because it is too large Load Diff

View File

@@ -56,3 +56,6 @@ hyper = { version = "1", features = ["server", "http1"] }
hyper-util = { version = "0.1", features = ["server", "tokio", "http1"] }
bytes = "1"
anyhow = "1"
axum-test = "18.1.0"
pretty_assertions = "1.4.1"
testcontainers = "0.25.0"

View File

@@ -1,10 +1,12 @@
use axum::{routing::get, Router};
use axum_cookie::CookieLayer;
use dashmap::DashMap;
use jsonwebtoken::{DecodingKey, EncodingKey};
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::data::pool::PgPool;
use crate::{auth::AuthRegistry, config::Config, image::ImageStorage};
use crate::{auth::AuthRegistry, config::Config, image::ImageStorage, routes};
#[derive(Debug, Clone)]
pub struct Health {
@@ -69,3 +71,34 @@ impl AppState {
}
}
}
/// Create the application router with all routes and middleware
pub fn create_router(app_state: AppState) -> Router {
Router::new()
.route("/", get(|| async { "Hello, World! Visit /auth/github to start OAuth flow." }))
.route("/health", get(routes::health_handler))
.route("/auth/providers", get(routes::list_providers_handler))
.route("/auth/{provider}", get(routes::oauth_authorize_handler))
.route("/auth/{provider}/callback", get(routes::oauth_callback_handler))
.route("/logout", get(routes::logout_handler))
.route("/profile", get(routes::profile_handler))
.with_state(app_state)
.layer(CookieLayer::default())
.layer(axum::middleware::from_fn(inject_server_header))
}
/// Inject the server header into responses
async fn inject_server_header(
req: axum::http::Request<axum::body::Body>,
next: axum::middleware::Next,
) -> Result<axum::response::Response, axum::http::StatusCode> {
let mut res = next.run(req).await;
res.headers_mut().insert(
axum::http::header::SERVER,
axum::http::HeaderValue::from_static(SERVER_HEADER_VALUE),
);
Ok(res)
}
// Constant value for the Server header: "<crate>/<version>"
const SERVER_HEADER_VALUE: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

View File

@@ -1,7 +1,7 @@
use figment::{providers::Env, value::UncasedStr, Figment};
use serde::{Deserialize, Deserializer};
#[derive(Debug, Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
// Database URL
pub database_url: String,

View File

@@ -1,6 +1,8 @@
use crate::{app::AppState, auth::AuthRegistry, config::Config};
use axum::{routing::get, Router};
use axum_cookie::CookieLayer;
use crate::{
app::{create_router, AppState},
auth::AuthRegistry,
config::Config,
};
use std::time::Instant;
use std::{sync::Arc, time::Duration};
use tracing::{info, trace, warn};
@@ -20,9 +22,6 @@ mod logging;
mod routes;
mod session;
// Constant value for the Server header: "<crate>/<version>"
const SERVER_HEADER_VALUE: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[tokio::main]
async fn main() {
// Load environment variables
@@ -55,17 +54,11 @@ async fn main() {
h.set_migrations(true);
}
let app = Router::new()
.route("/", get(|| async { "Hello, World! Visit /auth/github to start OAuth flow." }))
.route("/health", get(routes::health_handler))
.route("/auth/providers", get(routes::list_providers_handler))
.route("/auth/{provider}", get(routes::oauth_authorize_handler))
.route("/auth/{provider}/callback", get(routes::oauth_callback_handler))
.route("/logout", get(routes::logout_handler))
.route("/profile", get(routes::profile_handler))
.with_state(app_state.clone())
.layer(CookieLayer::default())
.layer(axum::middleware::from_fn(inject_server_header));
// Extract needed parts for health checker before moving app_state
let health_state = app_state.health.clone();
let db_pool = app_state.db.clone();
let app = create_router(app_state);
info!(%addr, "Starting HTTP server bind");
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
@@ -77,8 +70,8 @@ async fn main() {
// Spawn background health checker (listens for shutdown via notify)
{
let health_state = app_state.health.clone();
let db_pool = app_state.db.clone();
let health_state = health_state.clone();
let db_pool = db_pool.clone();
let notify_for_health = notify.clone();
tokio::spawn(async move {
trace!("Health checker task started");
@@ -177,15 +170,3 @@ async fn shutdown_signal() -> Instant {
_ = sigterm => { Instant::now() }
}
}
async fn inject_server_header(
req: axum::http::Request<axum::body::Body>,
next: axum::middleware::Next,
) -> Result<axum::response::Response, axum::http::StatusCode> {
let mut res = next.run(req).await;
res.headers_mut().insert(
axum::http::header::SERVER,
axum::http::HeaderValue::from_static(SERVER_HEADER_VALUE),
);
Ok(res)
}

View 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)
}

View 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);
}