use sqlx::{postgres::PgPoolOptions, query, Executor, Pool, Postgres}; use uuid::Uuid; use std::net::TcpListener; use zero2prod::configuration::{get_configuration, DatabaseSettings}; pub struct TestApp { pub address: String, pub connection_pool: Pool, } async fn spawn_app() -> TestApp { let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port"); let port = listener.local_addr().unwrap().port(); let address = format!("http://127.0.0.1:{}", port); let mut config = get_configuration().expect("Failed to read configuration"); config.database.database_name = Uuid::new_v4().to_string(); let connection_pool = configure_database(&config.database).await; let server = zero2prod::startup::run(listener, connection_pool.clone()).expect("Failed to bind address"); let _ = tokio::spawn(server); TestApp { address, connection_pool, } } pub async fn configure_database(config: &DatabaseSettings) -> Pool { let connection_pool = PgPoolOptions::new() .max_connections(10) .connect(&config.connection_string_without_db()) .await .expect("Failed to connect to Postgres."); connection_pool.execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_str()) .await .expect("Failed to create database."); sqlx::migrate!("./migrations") .run(&connection_pool) .await .expect("Failed to run migrations."); connection_pool } #[tokio::test] async fn health_check_works() { let app = spawn_app().await; let health_check_endpoint = format!("{}/health_check", app.address); let client = reqwest::Client::new(); let response = client .get(health_check_endpoint) .send() .await .expect("Failed to execute request."); assert!(response.status().is_success()); assert_eq!(Some(0), response.content_length()); } #[tokio::test] async fn subscribe_returns_a_200_for_valid_form_data() { let app = spawn_app().await; let client = reqwest::Client::new(); let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; let response = client .post(&format!("{}/subscriptions", &app.address)) .header("Content-Type", "application/x-www-form-urlencoded") .body(body) .send() .await .expect("Failed to execute request."); assert_eq!(200, response.status().as_u16()); let saved = query!("SELECT email, name FROM subscriptions",) .fetch_one(&app.connection_pool) .await .expect("Failed to fetch saved subscription."); assert_eq!(saved.email, "ursula_le_guin@gmail.com"); assert_eq!(saved.name, "le guin"); } #[tokio::test] async fn subscribe_returns_a_400_when_data_is_missing() { let app = spawn_app().await; let client = reqwest::Client::new(); let test_cases = vec![ ("name=le%20guin", "missing the email"), ("email=ursula_le_guin%40gmail.com", "missing the name"), ("", "missing both name and email"), ]; for (invalid_body, error_message) in test_cases { let response = client .post(&format!("{}/subscriptions", &app.address)) .header("Content-Type", "application/x-www-form-urlencoded") .body(invalid_body) .send() .await .expect("Failed to execute request."); assert_eq!( 400, response.status().as_u16(), "The API did not fail with 400 Bad Request when the payload was {}.", error_message ); } }