Unsubscribe link in emails sent

This commit is contained in:
Alphonse Paix
2025-09-22 01:25:36 +02:00
parent 829f3e4e4f
commit 98611f18e3
13 changed files with 431 additions and 211 deletions

View File

@@ -3,7 +3,7 @@ use argon2::{
password_hash::{SaltString, rand_core::OsRng},
};
use fake::{Fake, faker::internet::en::SafeEmail};
use linkify::LinkFinder;
use linkify::{Link, LinkFinder};
use once_cell::sync::Lazy;
use sqlx::{Connection, Executor, PgConnection, PgPool};
use uuid::Uuid;
@@ -169,16 +169,29 @@ impl TestApp {
}
}
pub fn get_unsubscribe_links(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links = get_links(s);
assert!(!links.is_empty());
let mut confirmation_link =
reqwest::Url::parse(links.last().unwrap().as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
};
let html = get_link(body["html"].as_str().unwrap());
let text = get_link(body["text"].as_str().unwrap());
ConfirmationLinks { html, text }
}
pub fn get_confirmation_links(&self, request: &wiremock::Request) -> ConfirmationLinks {
let body: serde_json::Value = serde_json::from_slice(&request.body).unwrap();
let get_link = |s: &str| {
let links: Vec<_> = LinkFinder::new()
.links(s)
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect();
let links = get_links(s);
assert_eq!(links.len(), 1);
let raw_link = links[0].as_str();
let mut confirmation_link = reqwest::Url::parse(raw_link).unwrap();
let mut confirmation_link = reqwest::Url::parse(links[0].as_str()).unwrap();
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(self.port)).unwrap();
confirmation_link
@@ -318,3 +331,29 @@ pub fn assert_is_redirect_to(response: &reqwest::Response, location: &str) {
pub fn when_sending_an_email() -> MockBuilder {
Mock::given(path("/email")).and(method("POST"))
}
pub fn fake_newsletter_body() -> serde_json::Value {
serde_json::json!({
"title": "Newsletter title",
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>",
"idempotency_key": Uuid::new_v4().to_string(),
})
}
pub fn fake_post_body() -> serde_json::Value {
serde_json::json!({
"title": "Post title",
"content": "Post content",
"idempotency_key": Uuid::new_v4().to_string(),
})
}
pub fn get_links(s: &'_ str) -> Vec<Link<'_>> {
LinkFinder::new()
.links(s)
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect()
}

View File

@@ -1,9 +1,21 @@
use crate::helpers::TestApp;
use wiremock::ResponseTemplate;
use crate::helpers::{TestApp, fake_newsletter_body, fake_post_body, when_sending_an_email};
#[tokio::test]
async fn unsubscribe_works_with_a_valid_token() {
async fn subscriber_can_unsubscribe() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
app.admin_login().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
app.post_newsletters(&fake_newsletter_body()).await;
app.dispatch_all_pending_emails().await;
let record = sqlx::query!("SELECT unsubscribe_token FROM subscriptions")
.fetch_one(&app.connection_pool)
@@ -18,7 +30,7 @@ async fn unsubscribe_works_with_a_valid_token() {
)
.await;
assert!(response.status().is_success());
assert_eq!(response.status().as_u16(), 200);
let html_fragment = response.text().await.unwrap();
assert!(html_fragment.contains("Good bye, old friend"));
@@ -28,4 +40,90 @@ async fn unsubscribe_works_with_a_valid_token() {
.expect("Failed to fetch subscription table");
assert!(record.is_none());
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
.await;
app.post_newsletters(&fake_newsletter_body()).await;
app.dispatch_all_pending_emails().await;
}
#[tokio::test]
async fn a_valid_unsubscribe_link_is_present_in_new_post_email_notifications() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
app.admin_login().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
app.post_create_post(&fake_post_body()).await;
app.dispatch_all_pending_emails().await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let unsubscribe_links = app.get_unsubscribe_links(&email_request);
reqwest::get(unsubscribe_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
let record = sqlx::query!("SELECT email FROM subscriptions")
.fetch_optional(&app.connection_pool)
.await
.expect("Failed to fetch subscription table");
assert!(record.is_none());
}
#[tokio::test]
async fn a_valid_unsubscribe_link_is_present_in_emails_manually_sent() {
let app = TestApp::spawn().await;
app.create_confirmed_subscriber().await;
app.admin_login().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
app.post_newsletters(&fake_newsletter_body()).await;
app.dispatch_all_pending_emails().await;
let email_request = app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
let unsubscribe_links = app.get_unsubscribe_links(&email_request);
reqwest::get(unsubscribe_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
}
#[tokio::test]
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();
assert_eq!(response.status().as_u16(), 404);
}