Handler to send emails to confirmed subscribers
This commit is contained in:
@@ -94,6 +94,15 @@ impl TestApp {
|
||||
.await
|
||||
.expect("Failed to execute request")
|
||||
}
|
||||
|
||||
pub async fn post_newsletters(&self, body: serde_json::Value) -> reqwest::Response {
|
||||
reqwest::Client::new()
|
||||
.post(format!("{}/newsletters", self.address))
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.expect("Failed to execute request")
|
||||
}
|
||||
}
|
||||
|
||||
async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod health_check;
|
||||
mod helpers;
|
||||
mod newsletters;
|
||||
mod subscriptions;
|
||||
mod subscriptions_confirm;
|
||||
|
||||
111
tests/api/newsletters.rs
Normal file
111
tests/api/newsletters.rs
Normal 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();
|
||||
}
|
||||
@@ -81,7 +81,7 @@ async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
||||
assert_eq!(
|
||||
400,
|
||||
response.status().as_u16(),
|
||||
"the API did not return a 400 Bad Request when the payload had an {}.",
|
||||
"the API did not fail with 400 Bad Request when the payload had an {}.",
|
||||
description
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user