Handler to send emails to confirmed subscribers

This commit is contained in:
Alphonse Paix
2025-08-27 12:13:58 +02:00
parent 9193f2020d
commit 8447d050d6
8 changed files with 242 additions and 23 deletions

111
tests/api/newsletters.rs Normal file
View File

@@ -0,0 +1,111 @@
use crate::helpers::{ConfirmationLinks, TestApp};
use wiremock::{
Mock, ResponseTemplate,
matchers::{any, method, path},
};
#[tokio::test]
async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
let app = TestApp::spawn().await;
create_unconfirmed_subscriber(&app).await;
Mock::given(any())
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
.await;
let newsletter_request_body = serde_json::json!({"title": "Newsletter title", "content": { "text": "Newsletter body as plain text", "html": "<p>Newsletter body as HTML</p>"}});
let response = app.post_newsletters(newsletter_request_body).await;
assert_eq!(response.status().as_u16(), 200);
}
#[tokio::test]
async fn newsletters_are_delivered_to_confirmed_subscribers() {
let app = TestApp::spawn().await;
create_confirmed_subscriber(&app).await;
Mock::given(any())
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
.await;
let newsletter_request_body = serde_json::json!({
"title": "Newsletter title",
"content": {
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>"
}
});
let response = app.post_newsletters(newsletter_request_body).await;
assert_eq!(response.status().as_u16(), 200);
}
#[tokio::test]
async fn newsletters_returns_422_for_invalid_data() {
let app = TestApp::spawn().await;
let test_cases = [
(
serde_json::json!({
"content": {
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>"
}
}),
"missing the title",
),
(
serde_json::json!({ "title": "Newsletter" }),
"missing the title",
),
];
for (invalid_body, error_message) in test_cases {
let response = app.post_newsletters(invalid_body).await;
assert_eq!(
response.status().as_u16(),
422,
"The API did not fail with 422 Unprocessable Entity when the payload was {}.",
error_message
);
}
}
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
let _mock_guard = Mock::given(path("/v1/email"))
.and(method("POST"))
.respond_with(ResponseTemplate::new(200))
.named("Create unconfirmed subscriber")
.expect(1)
.mount_as_scoped(&app.email_server)
.await;
app.post_subscriptions(body.into())
.await
.error_for_status()
.unwrap();
let email_request = &app
.email_server
.received_requests()
.await
.unwrap()
.pop()
.unwrap();
app.get_confirmation_links(email_request)
}
async fn create_confirmed_subscriber(app: &TestApp) {
let confirmation_links = create_unconfirmed_subscriber(app).await;
reqwest::get(confirmation_links.html)
.await
.unwrap()
.error_for_status()
.unwrap();
}