Files
zero2prod/tests/api/subscriptions_confirm.rs
Alphonse Paix 33281132c6
Some checks failed
Rust / Test (push) Has been cancelled
Rust / Rustfmt (push) Has been cancelled
Rust / Clippy (push) Has been cancelled
Rust / Code coverage (push) Has been cancelled
Update test suite to drop database automatically when test is successfull
2025-09-24 02:55:18 +02:00

74 lines
2.2 KiB
Rust

use crate::helpers::{TestApp, when_sending_an_email};
use sqlx::PgPool;
use wiremock::ResponseTemplate;
#[sqlx::test]
async fn confirmation_links_without_token_are_rejected_with_a_400(connection_pool: PgPool) {
let app = TestApp::spawn(connection_pool).await;
let response = reqwest::get(&format!("{}/subscriptions/confirm", &app.address))
.await
.unwrap();
assert_eq!(400, response.status().as_u16());
}
#[sqlx::test]
async fn clicking_on_the_link_shows_a_confirmation_message(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);
let response = reqwest::get(confirmation_links.html).await.unwrap();
assert_eq!(response.status().as_u16(), 200);
assert!(
response
.text()
.await
.unwrap()
.contains("Subscription confirmed")
);
}
#[sqlx::test]
async fn clicking_on_the_confirmation_link_confirms_a_subscriber(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);
reqwest::get(confirmation_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
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, "confirmed");
}