2021-07-17 18:43:47 +00:00
|
|
|
use crate::helpers::{spawn_app, ConfirmationLinks, TestApp};
|
2021-07-25 15:40:01 +00:00
|
|
|
use wiremock::matchers::{any, method, path};
|
2021-07-17 18:43:47 +00:00
|
|
|
use wiremock::{Mock, ResponseTemplate};
|
|
|
|
|
|
|
|
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
2021-07-21 20:41:13 +00:00
|
|
|
let _mock_guard = Mock::given(path("/email"))
|
2021-07-17 18:43:47 +00:00
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
2021-07-21 21:21:23 +00:00
|
|
|
.named("Create unconfirmed subscriber")
|
2021-07-21 20:59:32 +00:00
|
|
|
.expect(1)
|
2021-07-21 20:41:13 +00:00
|
|
|
.mount_as_scoped(&app.email_server)
|
2021-07-17 18:43:47 +00:00
|
|
|
.await;
|
|
|
|
app.post_subscriptions(body.into())
|
|
|
|
.await
|
|
|
|
.error_for_status()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let email_request = &app
|
|
|
|
.email_server
|
|
|
|
.received_requests()
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.pop()
|
|
|
|
.unwrap();
|
2021-09-11 19:59:18 +00:00
|
|
|
app.get_confirmation_links(email_request)
|
2021-07-17 18:43:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn create_confirmed_subscriber(app: &TestApp) {
|
|
|
|
let confirmation_link = create_unconfirmed_subscriber(app).await.html;
|
|
|
|
reqwest::get(confirmation_link)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.error_for_status()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:53:58 +00:00
|
|
|
#[tokio::test]
|
2021-07-17 18:43:47 +00:00
|
|
|
async fn newsletters_are_not_delivered_to_unconfirmed_subscribers() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
create_unconfirmed_subscriber(&app).await;
|
|
|
|
|
2021-07-22 07:31:57 +00:00
|
|
|
Mock::given(any())
|
2021-07-17 18:43:47 +00:00
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.expect(0)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
2021-07-25 15:40:01 +00:00
|
|
|
"content": {
|
2021-07-17 18:43:47 +00:00
|
|
|
"text": "Newsletter body as plain text",
|
|
|
|
"html": "<p>Newsletter body as HTML</p>",
|
|
|
|
}
|
|
|
|
});
|
2021-07-25 16:37:00 +00:00
|
|
|
let response = app.post_newsletters(newsletter_request_body).await;
|
2021-07-17 18:43:47 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(response.status().as_u16(), 200);
|
|
|
|
// Mock verifies on Drop that we haven't sent the newsletter email
|
|
|
|
}
|
|
|
|
|
2021-12-26 15:53:58 +00:00
|
|
|
#[tokio::test]
|
2021-07-17 18:43:47 +00:00
|
|
|
async fn newsletters_are_delivered_to_confirmed_subscribers() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
create_confirmed_subscriber(&app).await;
|
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.expect(1)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
2021-07-25 15:40:01 +00:00
|
|
|
"content": {
|
2021-07-17 18:43:47 +00:00
|
|
|
"text": "Newsletter body as plain text",
|
|
|
|
"html": "<p>Newsletter body as HTML</p>",
|
|
|
|
}
|
|
|
|
});
|
2021-07-25 16:37:00 +00:00
|
|
|
let response = app.post_newsletters(newsletter_request_body).await;
|
2021-07-17 18:43:47 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(response.status().as_u16(), 200);
|
|
|
|
// Mock verifies on Drop that we have sent the newsletter email
|
|
|
|
}
|
2021-07-25 16:37:00 +00:00
|
|
|
|
2021-12-26 15:53:58 +00:00
|
|
|
#[tokio::test]
|
2021-07-25 16:37:00 +00:00
|
|
|
async fn newsletters_returns_400_for_invalid_data() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
let test_cases = vec![
|
|
|
|
(
|
|
|
|
serde_json::json!({
|
|
|
|
"content": {
|
|
|
|
"text": "Newsletter body as plain text",
|
|
|
|
"html": "<p>Newsletter body as HTML</p>",
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
"missing title",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
serde_json::json!({
|
|
|
|
"title": "Newsletter!"
|
|
|
|
}),
|
|
|
|
"missing content",
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (invalid_body, error_message) in test_cases {
|
|
|
|
let response = app.post_newsletters(invalid_body).await;
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(
|
|
|
|
400,
|
|
|
|
response.status().as_u16(),
|
|
|
|
// Additional customised error message on test failure
|
|
|
|
"The API did not fail with 400 Bad Request when the payload was {}.",
|
|
|
|
error_message
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|