Unsubscribe option available on website

This commit is contained in:
Alphonse Paix
2025-09-22 15:44:02 +02:00
parent a37123a32d
commit f1ce77a762
21 changed files with 400 additions and 91 deletions

View File

@@ -202,10 +202,10 @@ impl TestApp {
ConfirmationLinks { html, text }
}
pub async fn get_unsubscribe(&self, unsubscribe_token: String) -> reqwest::Response {
pub async fn get_unsubscribe_confirm(&self, unsubscribe_token: &str) -> reqwest::Response {
self.api_client
.get(format!(
"{}/unsubscribe?token={}",
"{}/unsubscribe/confirm?token={}",
&self.address, unsubscribe_token
))
.send()
@@ -298,6 +298,18 @@ impl TestApp {
.await
.expect("Failed to execute request")
}
pub async fn post_unsubscribe<Body>(&self, body: &Body) -> reqwest::Response
where
Body: serde::Serialize,
{
self.api_client
.post(format!("{}/unsubscribe", self.address))
.form(body)
.send()
.await
.expect("Failed to execute request")
}
}
async fn configure_database(config: &DatabaseSettings) -> PgPool {

View File

@@ -8,3 +8,4 @@ mod posts;
mod subscriptions;
mod subscriptions_confirm;
mod unsubscribe;
mod unsubscribe_confirm;

View File

@@ -16,7 +16,7 @@ async fn subscribe_displays_a_confirmation_message_for_valid_form_data() {
assert!(response.status().is_success());
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("A confirmation email has been sent"));
assert!(html_fragment.contains("You&#39;ll receive a confirmation email shortly"));
}
#[tokio::test]
@@ -33,8 +33,8 @@ async fn subscribe_persists_the_new_subscriber() {
let response = app.post_subscriptions(body).await;
assert!(response.status().is_success());
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("A confirmation email has been sent"));
let html_fragment = dbg!(response.text().await.unwrap());
assert!(html_fragment.contains("You&#39;ll receive a confirmation email shortly"));
let saved = sqlx::query!("SELECT email, status FROM subscriptions")
.fetch_one(&app.connection_pool)

View File

@@ -22,16 +22,12 @@ async fn subscriber_can_unsubscribe() {
.expect("Failed to fetch saved token");
let response = app
.get_unsubscribe(
record
.unsubscribe_token
.expect("Confirmed subscriber should have a valid unsubscribe token"),
)
.get_unsubscribe_confirm(&record.unsubscribe_token.unwrap())
.await;
assert_eq!(response.status().as_u16(), 200);
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("Good bye, old friend"));
assert!(html_fragment.contains("Good bye, friend"));
let record = sqlx::query!("SELECT email FROM subscriptions")
.fetch_optional(&app.connection_pool)
@@ -120,9 +116,11 @@ async fn an_invalid_unsubscribe_token_is_rejected() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
let response = reqwest::get(format!("{}/unsubscribe?token=invalid", app.address))
.await
.unwrap();
let response = app.get_unsubscribe_confirm("invalid-token").await;
// let response = reqwest::get(format!("{}/unsubscribe?token=invalid", app.address))
// .await
// .unwrap();
assert_eq!(response.status().as_u16(), 404);
}
@@ -139,16 +137,12 @@ async fn subscription_works_after_unsubscribe() {
let email = record.email;
let response = app
.get_unsubscribe(
record
.unsubscribe_token
.expect("Confirmed subscriber should have a valid unsubscribe token"),
)
.get_unsubscribe_confirm(&record.unsubscribe_token.unwrap())
.await;
assert_eq!(response.status().as_u16(), 200);
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("Good bye, old friend"));
assert!(html_fragment.contains("Good bye, friend"));
let record = sqlx::query!("SELECT email, unsubscribe_token FROM subscriptions")
.fetch_optional(&app.connection_pool)

View File

@@ -0,0 +1,67 @@
use crate::helpers::{TestApp, when_sending_an_email};
use wiremock::ResponseTemplate;
#[tokio::test]
async fn unsubscribe_form_sends_a_valid_link_if_email_is_in_database() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
let record = sqlx::query!("SELECT email, unsubscribe_token FROM subscriptions")
.fetch_one(&app.connection_pool)
.await
.expect("Failed to fetch saved email and token");
let body = serde_json::json!({
"email": record.email
});
app.post_unsubscribe(&body).await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let unsubscribe_links = app.get_unsubscribe_links(&email_request);
assert!(
unsubscribe_links
.html
.as_str()
.contains(&record.unsubscribe_token.unwrap())
);
let response = reqwest::get(unsubscribe_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("Good bye, friend"));
}
#[tokio::test]
async fn an_invalid_email_is_ignored() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
.await;
let body = serde_json::json!({
"email": "invalid.email@example.com"
});
app.post_unsubscribe(&body).await;
}