Fault-tolerant delivery system

This commit is contained in:
Alphonse Paix
2025-09-04 02:54:49 +02:00
parent 9a184b93ac
commit f8dee295cd
32 changed files with 872 additions and 120 deletions

View File

@@ -9,6 +9,8 @@ use uuid::Uuid;
use wiremock::MockServer;
use zero2prod::{
configuration::{DatabaseSettings, get_configuration},
email_client::EmailClient,
issue_delivery_worker::{ExecutionOutcome, try_execute_task},
startup::Application,
telemetry::init_subscriber,
};
@@ -70,6 +72,7 @@ pub struct TestApp {
pub port: u16,
pub test_user: TestUser,
pub api_client: reqwest::Client,
pub email_client: EmailClient,
}
impl TestApp {
@@ -85,6 +88,7 @@ impl TestApp {
c
};
let connection_pool = configure_database(&configuration.database).await;
let email_client = EmailClient::build(configuration.email_client.clone()).unwrap();
let application = Application::build(configuration)
.await
.expect("Failed to build application");
@@ -110,6 +114,7 @@ impl TestApp {
port,
test_user,
api_client,
email_client,
};
tokio::spawn(application.run_until_stopped());
@@ -117,6 +122,18 @@ impl TestApp {
app
}
pub async fn dispatch_all_pending_emails(&self) {
loop {
if let ExecutionOutcome::EmptyQueue =
try_execute_task(&self.connection_pool, &self.email_client)
.await
.unwrap()
{
break;
}
}
}
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| {
@@ -218,6 +235,14 @@ impl TestApp {
.expect("failed to execute request")
}
pub async fn admin_login(&self) {
let login_body = serde_json::json!({
"username": self.test_user.username,
"password": self.test_user.password
});
self.post_login(&login_body).await;
}
pub async fn post_logout(&self) -> reqwest::Response {
self.api_client
.post(format!("{}/admin/logout", self.address))

View File

@@ -1,21 +1,22 @@
use crate::helpers::{ConfirmationLinks, TestApp, assert_is_redirect_to};
use fake::{
Fake,
faker::{internet::en::SafeEmail, name::fr_fr::Name},
};
use std::time::Duration;
use uuid::Uuid;
use wiremock::{
Mock, ResponseTemplate,
matchers::{any, method, path},
Mock, MockBuilder, ResponseTemplate,
matchers::{method, path},
};
#[tokio::test]
async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
let app = TestApp::spawn().await;
create_unconfirmed_subscriber(&app).await;
app.admin_login().await;
let login_body = serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password
});
app.post_login(&login_body).await;
Mock::given(any())
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
@@ -27,13 +28,15 @@ async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
"html": "<p>Newsletter body as HTML</p>"
});
app.post_newsletters(&newsletter_request_body).await;
app.dispatch_all_pending_emails().await;
}
#[tokio::test]
async fn requests_without_authentication_are_redirected() {
let app = TestApp::spawn().await;
Mock::given(any())
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
@@ -52,14 +55,9 @@ async fn requests_without_authentication_are_redirected() {
async fn newsletters_are_delivered_to_confirmed_subscribers() {
let app = TestApp::spawn().await;
create_confirmed_subscriber(&app).await;
app.admin_login().await;
let login_body = serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password
});
app.post_login(&login_body).await;
Mock::given(any())
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
@@ -69,7 +67,8 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() {
let newsletter_request_body = serde_json::json!({
"title": newsletter_title,
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>"
"html": "<p>Newsletter body as HTML</p>",
"idempotency_key": Uuid::new_v4().to_string(),
});
let response = app.post_newsletters(&newsletter_request_body).await;
@@ -80,19 +79,16 @@ async fn newsletters_are_delivered_to_confirmed_subscribers() {
"The newsletter issue '{}' has been published",
newsletter_title
)));
app.dispatch_all_pending_emails().await;
}
#[tokio::test]
async fn form_shows_error_for_invalid_data() {
let app = TestApp::spawn().await;
app.admin_login().await;
let login_body = serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password
});
app.post_login(&login_body).await;
Mock::given(any())
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(0)
.mount(&app.email_server)
@@ -101,14 +97,20 @@ async fn form_shows_error_for_invalid_data() {
let test_cases = [
(
serde_json::json!({
"title": "",
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>"
}),
"title": "",
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>",
"idempotency_key": Uuid::new_v4().to_string(),
}),
"The title was empty",
),
(
serde_json::json!({ "title": "Newsletter", "text": "", "html": "" }),
serde_json::json!({
"title": "Newsletter",
"text": "",
"html": "",
"idempotency_key": Uuid::new_v4().to_string(),
}),
"The content was empty",
),
];
@@ -124,14 +126,9 @@ async fn form_shows_error_for_invalid_data() {
async fn newsletter_creation_is_idempotent() {
let app = TestApp::spawn().await;
create_confirmed_subscriber(&app).await;
app.admin_login().await;
let login_body = serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password
});
app.post_login(&login_body).await;
Mock::given(any())
when_sending_an_email()
.respond_with(ResponseTemplate::new(200))
.expect(1)
.mount(&app.email_server)
@@ -141,7 +138,8 @@ async fn newsletter_creation_is_idempotent() {
let newsletter_request_body = serde_json::json!({
"title": newsletter_title,
"text": "Newsletter body as plain text",
"html": "<p>Newsletter body as HTML</p>"
"html": "<p>Newsletter body as HTML</p>",
"idempotency_key": Uuid::new_v4().to_string(),
});
let response = app.post_newsletters(&newsletter_request_body).await;
@@ -161,10 +159,49 @@ async fn newsletter_creation_is_idempotent() {
"The newsletter issue '{}' has been published",
newsletter_title
)));
app.dispatch_all_pending_emails().await;
}
#[tokio::test]
async fn concurrent_form_submission_is_handled_gracefully() {
let app = TestApp::spawn().await;
create_confirmed_subscriber(&app).await;
app.admin_login().await;
when_sending_an_email()
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(2)))
.expect(1)
.mount(&app.email_server)
.await;
let newsletter_request_body = 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(),
});
let response1 = app.post_newsletters(&newsletter_request_body);
let response2 = app.post_newsletters(&newsletter_request_body);
let (response1, response2) = tokio::join!(response1, response2);
assert_eq!(response1.status(), response2.status());
assert_eq!(
response1.text().await.unwrap(),
response2.text().await.unwrap(),
);
app.dispatch_all_pending_emails().await;
}
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
let body = "name=Alphonse&email=alphonse.paix%40outlook.com";
let name: String = Name().fake();
let email: String = SafeEmail().fake();
let body = serde_urlencoded::to_string(serde_json::json!({
"name": name,
"email": email
}))
.unwrap();
let _mock_guard = Mock::given(path("/v1/email"))
.and(method("POST"))
@@ -173,7 +210,7 @@ async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
.expect(1)
.mount_as_scoped(&app.email_server)
.await;
app.post_subscriptions(body.into())
app.post_subscriptions(body)
.await
.error_for_status()
.unwrap();
@@ -196,3 +233,7 @@ async fn create_confirmed_subscriber(app: &TestApp) {
.error_for_status()
.unwrap();
}
fn when_sending_an_email() -> MockBuilder {
Mock::given(path("/v1/email")).and(method("POST"))
}