use crate::helpers::{TestApp, when_sending_an_email}; use sqlx::PgPool; use wiremock::ResponseTemplate; #[sqlx::test] async fn subscribe_displays_a_confirmation_message_for_valid_form_data(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; when_sending_an_email() .respond_with(ResponseTemplate::new(200)) .mount(&app.email_server) .await; let email = "alphonse.paix@outlook.com"; let body = format!("email={email}"); let response = app.post_subscriptions(body).await; assert!(response.status().is_success()); let html_fragment = response.text().await.unwrap(); assert!(html_fragment.contains("You'll receive a confirmation email shortly")); } #[sqlx::test] async fn subscribe_persists_the_new_subscriber(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; when_sending_an_email() .respond_with(ResponseTemplate::new(200)) .mount(&app.email_server) .await; let email = "alphonse.paix@outlook.com"; let body = format!("email={email}"); let response = app.post_subscriptions(body).await; assert!(response.status().is_success()); let html_fragment = response.text().await.unwrap(); assert!(html_fragment.contains("You'll receive a confirmation email shortly")); let saved = sqlx::query!("SELECT email, status FROM subscriptions") .fetch_one(&app.connection_pool) .await .expect("Failed to fetch saved subscription"); assert_eq!(saved.email, "alphonse.paix@outlook.com"); assert_eq!(saved.status, "pending_confirmation"); } #[sqlx::test] async fn subscribe_returns_a_422_when_data_is_missing(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; let response = app.post_subscriptions(String::new()).await; assert_eq!( 422, response.status().as_u16(), "the API did not fail with 422 Unprocessable Entity when the payload was missing the email" ); } #[sqlx::test] async fn subscribe_sends_a_confirmation_email_for_valid_data(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; let email = "alphonse.paix@outlook.com"; let body = format!("email={email}"); when_sending_an_email() .respond_with(ResponseTemplate::new(200)) .expect(1) .mount(&app.email_server) .await; app.post_subscriptions(body).await; } #[sqlx::test] async fn subscribe_sends_a_confirmation_email_with_a_link(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; let email = "alphonse.paix@outlook.com"; let body = format!("email={email}"); when_sending_an_email() .respond_with(ResponseTemplate::new(200)) .expect(1) .mount(&app.email_server) .await; app.post_subscriptions(body).await; let email_request = &app.email_server.received_requests().await.unwrap()[0]; let confirmation_links = app.get_confirmation_links(email_request); assert_eq!(confirmation_links.html, confirmation_links.text); } #[sqlx::test] async fn subscribe_fails_if_there_is_a_fatal_database_error(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; let email = "alphonse.paix@outlook.com"; let body = format!("name=Alphonse&email={}", email); sqlx::query!("ALTER TABLE subscriptions DROP COLUMN email") .execute(&app.connection_pool) .await .unwrap(); let response = app.post_subscriptions(body).await; assert!( response .text() .await .unwrap() .contains("An internal server error occured") ); }