use sqlx::{Connection, Executor, PgConnection, PgPool}; use tokio::net::TcpListener; use uuid::Uuid; use zero2prod::configuration::{DatabaseSettings, get_configuration}; pub struct TestApp { pub address: String, pub connection_pool: PgPool, } #[tokio::test] async fn health_check_works() { let app = spawn_app().await; let client = reqwest::Client::new(); let response = client .get(format!("http://{}/health_check", app.address)) .send() .await .unwrap(); 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 configuration = get_configuration().expect("Failed to read configuration"); let connection_string = configuration.database.connection_string(); let mut connection = PgConnection::connect(&connection_string) .await .expect("Failed to connect to Postgres"); let client = reqwest::Client::new(); let body = "name=alphonse&email=alphonse.paix%40outlook.com"; let response = client .post(format!("http://{}/subscriptions", app.address)) .header("Content-Type", "application/x-www-form-urlencoded") .body(body) .send() .await .unwrap(); assert_eq!(200, response.status().as_u16()); let saved = sqlx::query!("SELECT email, name FROM subscriptions") .fetch_one(&mut connection) .await .expect("Failed to fetch saved subscription"); assert_eq!(saved.email, "alphonse.paix@outlook.com"); assert_eq!(saved.name, "alphonse"); } #[tokio::test] async fn subscribe_returns_a_422_when_data_is_missing() { let app = spawn_app().await; let client = reqwest::Client::new(); let test_cases = [ ("name=Alphonse", "missing the email"), ("email=alphonse.paix%40outlook.com", "missing the name"), ("", "missing both name and email"), ]; for (invalid_body, error_message) in test_cases { let response = client .post(format!("http://{}/subscriptions", app.address)) .header("Content-Type", "application/x-www-form-urlencoded") .body(invalid_body) .send() .await .unwrap(); assert_eq!( 422, response.status().as_u16(), "the API did not fail with 422 Unprocessable Entity when the payload was {}.", error_message ); } } async fn spawn_app() -> TestApp { let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); let address = listener.local_addr().unwrap().to_string(); let mut configuration = get_configuration().expect("Failed to read configuration"); configuration.database.database_name = Uuid::new_v4().to_string(); let connection_pool = configure_database(&configuration.database).await; let app = TestApp { address, connection_pool: connection_pool.clone(), }; tokio::spawn(async move { zero2prod::startup::run(listener, connection_pool).await; }); app } async fn configure_database(config: &DatabaseSettings) -> PgPool { let connection = PgPool::connect(&config.connection_string_without_db()) .await .expect("Failed to connect to Postgres"); connection .execute(format!(r#"CREATE DATABASE "{}";"#, config.database_name).as_ref()) .await .expect("Failed to create the database"); let connection_pool = PgPool::connect(&config.connection_string()) .await .expect("Failed to connect to Postgres"); sqlx::migrate!("./migrations") .run(&connection_pool) .await .expect("Failed to migrate the database"); connection_pool }