Basic unsubscribe endpoint

This commit is contained in:
Alphonse Paix
2025-09-21 17:49:31 +02:00
parent 6a963a8c0d
commit 7af07ea0dd
17 changed files with 292 additions and 43 deletions

View File

@@ -189,6 +189,17 @@ impl TestApp {
ConfirmationLinks { html, text }
}
pub async fn get_unsubscribe(&self, unsubscribe_token: String) -> reqwest::Response {
self.api_client
.get(format!(
"{}/unsubscribe?token={}",
&self.address, unsubscribe_token
))
.send()
.await
.expect("Failed to execute request")
}
pub async fn get_admin_dashboard(&self) -> reqwest::Response {
self.api_client
.get(format!("{}/admin/dashboard", &self.address))

View File

@@ -7,3 +7,4 @@ mod newsletters;
mod posts;
mod subscriptions;
mod subscriptions_confirm;
mod unsubscribe;

31
tests/api/unsubscribe.rs Normal file
View File

@@ -0,0 +1,31 @@
use crate::helpers::TestApp;
#[tokio::test]
async fn unsubscribe_works_with_a_valid_token() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
let record = sqlx::query!("SELECT unsubscribe_token FROM subscriptions")
.fetch_one(&app.connection_pool)
.await
.expect("Failed to fetch saved token");
let response = app
.get_unsubscribe(
record
.unsubscribe_token
.expect("Confirmed subscriber should have a valid unsubscribe token"),
)
.await;
assert!(response.status().is_success());
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("Good bye, old friend"));
let record = sqlx::query!("SELECT email FROM subscriptions")
.fetch_optional(&app.connection_pool)
.await
.expect("Failed to fetch subscription table");
assert!(record.is_none());
}