This repository was archived by the owner on Sep 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(chaper-9): Add newsletter publication endpoint #19
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,12 @@ | ||
| use crate::Result; | ||
| use crate::model::ModelManager; | ||
| use crate::{AppState, Result}; | ||
| use axum::extract::State; | ||
| use reqwest::StatusCode; | ||
| use std::sync::Arc; | ||
|
|
||
| #[axum::debug_handler(state = AppState)] | ||
| #[tracing::instrument(skip(mm))] | ||
| pub async fn health(State(mm): State<Arc<ModelManager>>) -> Result<StatusCode> { | ||
| mm.db().await?.health().await.map_err(Box::new)?; | ||
| mm.db().await?.health().await?; | ||
|
|
||
| Ok(StatusCode::OK) | ||
| } | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| mod health_check; | ||
| mod newsletter; | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| mod subscription; | ||
|
|
||
| pub use health_check::*; | ||
| pub use newsletter::*; | ||
| pub use subscription::*; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::{Result, email_client::EmailClient, model::ModelManager}; | ||
| use axum::{Json, extract::State, response::IntoResponse}; | ||
| use reqwest::StatusCode; | ||
| use serde::Deserialize; | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| pub struct BodyData { | ||
| title: String, | ||
| content: Content, | ||
| } | ||
|
|
||
| #[derive(Debug, Deserialize)] | ||
| struct Content { | ||
| html: String, | ||
| text: String, | ||
| } | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #[tracing::instrument(skip(mm, email_client))] | ||
| pub async fn publish_newsletter( | ||
| State(mm): State<Arc<ModelManager>>, | ||
| State(email_client): State<Arc<EmailClient>>, | ||
| Json(body): Json<BodyData>, | ||
| ) -> Result<impl IntoResponse> { | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let subscribers = mm.get_confirmed_subscribers().await?; | ||
|
|
||
| for subscriber in subscribers { | ||
| email_client | ||
| .send_email( | ||
| &subscriber.email.try_into()?, | ||
| &body.title, | ||
| &body.content.html, | ||
| &body.content.text, | ||
| ) | ||
| .await?; | ||
| } | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Ok(StatusCode::OK) | ||
| } | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| mod health_check; | ||
| mod helpers; | ||
| mod newsletter; | ||
| mod subscriptions; | ||
| mod subscriptions_confirm; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| use reqwest::{Method, StatusCode}; | ||
| use serde_json::json; | ||
| use wiremock::{ | ||
| Mock, ResponseTemplate, | ||
| matchers::{any, method}, | ||
| }; | ||
|
|
||
| use crate::helpers::{ConfirmationLinks, TestApp}; | ||
|
|
||
| async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks { | ||
| let body = [("name", "let guin"), ("email", "ursula_le_guin@gmail.com")]; | ||
|
|
||
| let _mock_guard = Mock::given(any()) | ||
| .respond_with(ResponseTemplate::new(StatusCode::OK)) | ||
| .named("Create unconfirmed subscriber") | ||
| .expect(1) | ||
| .mount_as_scoped(&app.email_server) | ||
| .await; | ||
|
|
||
| app.server.post("/subscriptions").form(&body).await; | ||
|
|
||
| let email_request = app | ||
| .email_server | ||
| .received_requests() | ||
| .await | ||
| .unwrap() | ||
| .pop() | ||
| .unwrap(); | ||
| app.get_conformation_links(&email_request) | ||
| } | ||
|
|
||
| async fn create_confirmed_subscriber(app: &TestApp) { | ||
| let confirmation_links = create_unconfirmed_subscriber(app).await; | ||
|
|
||
| app.server | ||
| .get(&format!( | ||
| "{}?{}", | ||
| confirmation_links.html.path(), | ||
| confirmation_links.html.query().unwrap() | ||
| )) | ||
| .await | ||
| .assert_status_success(); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn newsletter_are_not_delivered_to_unconfirmed_subscribers() { | ||
| // Arrange | ||
| let app = TestApp::new() | ||
| .await | ||
| .expect("Expected the app to be inisilized!"); | ||
| create_unconfirmed_subscriber(&app).await; | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Act | ||
| let newsletter = serde_json::json!({ | ||
| "title": "Newsletter title", | ||
| "content": { | ||
| "text": "Newsletter body as plain text", | ||
| "html": "<p>Newsletter body as html</p>", | ||
| }, | ||
| }); | ||
| let response = app.server.post("/newsletter").json(&newsletter).await; | ||
|
|
||
| // Assert | ||
| assert_eq!(response.status_code(), StatusCode::OK); | ||
| } | ||
|
|
||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #[tokio::test] | ||
| async fn newsletter_are_delivered_to_confirmed_subscribers() { | ||
| // Arrange | ||
| let app = TestApp::new() | ||
| .await | ||
| .expect("Expected the app to be inisilized!"); | ||
| create_confirmed_subscriber(&app).await; | ||
|
|
||
| Mock::given(any()) | ||
| .and(method(Method::POST)) | ||
| .respond_with(ResponseTemplate::new(StatusCode::OK)) | ||
| .expect(1) | ||
| .mount(&app.email_server) | ||
| .await; | ||
|
|
||
| // Act | ||
| let newsletter = serde_json::json!({ | ||
| "title": "Newsletter title", | ||
| "content": { | ||
| "text": "Newsletter body as plain text", | ||
| "html": "<p>Newsletter body as html</p>", | ||
| }, | ||
| }); | ||
| let response = app.server.post("/newsletter").json(&newsletter).await; | ||
|
|
||
| // Assert | ||
| assert_eq!(response.status_code(), StatusCode::OK); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn newsletter_return_400_for_invalid_data() { | ||
| // Arrange | ||
| let app = TestApp::new() | ||
| .await | ||
| .expect("Expected the app to be inisilized!"); | ||
| let test_cases = [ | ||
| ( | ||
| json!({ | ||
| "content": { | ||
| "text": "Newsletter body as plain text", | ||
| "html": "<p>Newsletter body as html</p>", | ||
| }, | ||
| }), | ||
| "messing title", | ||
| ), | ||
| ( | ||
| json!({ | ||
| "title": "Newsletter title", | ||
| }), | ||
| "messing content", | ||
| ), | ||
| ]; | ||
|
|
||
| for (invalid_body, error_message) in test_cases { | ||
| // Act | ||
| let response = app.server.post("/newsletter").json(&invalid_body).await; | ||
|
|
||
| // Assert | ||
| assert_eq!( | ||
| StatusCode::UNPROCESSABLE_ENTITY, | ||
| response.status_code(), | ||
| "The API did not fail with 400 Bad Request when the payload was {}.", | ||
| error_message | ||
| ); | ||
TN19N marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.