2022-03-13 14:53:56 +00:00
|
|
|
use crate::helpers::{assert_is_redirect_to, spawn_app, ConfirmationLinks, TestApp};
|
2022-03-13 19:36:44 +00:00
|
|
|
use fake::faker::internet::en::SafeEmail;
|
|
|
|
use fake::faker::name::en::Name;
|
|
|
|
use fake::Fake;
|
|
|
|
use std::time::Duration;
|
2021-07-25 15:40:01 +00:00
|
|
|
use wiremock::matchers::{any, method, path};
|
2022-07-05 16:47:21 +00:00
|
|
|
use wiremock::{Mock, ResponseTemplate};
|
2021-07-17 18:43:47 +00:00
|
|
|
|
|
|
|
async fn create_unconfirmed_subscriber(app: &TestApp) -> ConfirmationLinks {
|
2022-03-13 19:36:44 +00:00
|
|
|
// We are working with multiple subscribers now,
|
|
|
|
// their details must be randomised to avoid conflicts!
|
|
|
|
let name: String = Name().fake();
|
|
|
|
let email: String = SafeEmail().fake();
|
|
|
|
let body = serde_urlencoded::to_string(&serde_json::json!({
|
|
|
|
"name": name,
|
|
|
|
"email": email
|
|
|
|
}))
|
|
|
|
.unwrap();
|
2021-07-17 18:43:47 +00:00
|
|
|
|
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 20:00:34 +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;
|
2022-03-13 14:53:56 +00:00
|
|
|
app.test_user.login(&app).await;
|
2021-07-17 18:43:47 +00:00
|
|
|
|
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;
|
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act - Part 1 - Submit newsletter form
|
2021-07-17 18:43:47 +00:00
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
2022-03-13 14:53:56 +00:00
|
|
|
"text_content": "Newsletter body as plain text",
|
|
|
|
"html_content": "<p>Newsletter body as HTML</p>",
|
2022-03-13 19:36:44 +00:00
|
|
|
"idempotency_key": uuid::Uuid::new_v4().to_string()
|
2021-07-17 18:43:47 +00:00
|
|
|
});
|
2022-03-13 14:53:56 +00:00
|
|
|
let response = app.post_publish_newsletter(&newsletter_request_body).await;
|
|
|
|
assert_is_redirect_to(&response, "/admin/newsletters");
|
2021-07-17 18:43:47 +00:00
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act - Part 2 - Follow the redirect
|
|
|
|
let html_page = app.get_publish_newsletter_html().await;
|
2022-03-13 19:36:44 +00:00
|
|
|
assert!(html_page.contains(
|
|
|
|
"<p><i>The newsletter issue has been accepted - \
|
|
|
|
emails will go out shortly.</i></p>"
|
|
|
|
));
|
|
|
|
app.dispatch_all_pending_emails().await;
|
2021-07-17 18:43:47 +00:00
|
|
|
// 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;
|
2022-03-13 14:53:56 +00:00
|
|
|
app.test_user.login(&app).await;
|
2021-07-17 18:43:47 +00:00
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.expect(1)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act - Part 1 - Submit newsletter form
|
2021-07-17 18:43:47 +00:00
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
2022-03-13 14:53:56 +00:00
|
|
|
"text_content": "Newsletter body as plain text",
|
|
|
|
"html_content": "<p>Newsletter body as HTML</p>",
|
2022-03-13 19:36:44 +00:00
|
|
|
"idempotency_key": uuid::Uuid::new_v4().to_string()
|
2021-07-17 18:43:47 +00:00
|
|
|
});
|
2022-03-13 14:53:56 +00:00
|
|
|
let response = app.post_publish_newsletter(&newsletter_request_body).await;
|
|
|
|
assert_is_redirect_to(&response, "/admin/newsletters");
|
2021-07-17 18:43:47 +00:00
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act - Part 2 - Follow the redirect
|
|
|
|
let html_page = app.get_publish_newsletter_html().await;
|
2022-03-13 19:36:44 +00:00
|
|
|
assert!(html_page.contains(
|
|
|
|
"<p><i>The newsletter issue has been accepted - \
|
|
|
|
emails will go out shortly.</i></p>"
|
|
|
|
));
|
|
|
|
app.dispatch_all_pending_emails().await;
|
2021-07-17 18:43:47 +00:00
|
|
|
// 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]
|
2022-03-13 14:53:56 +00:00
|
|
|
async fn you_must_be_logged_in_to_see_the_newsletter_form() {
|
2021-08-14 18:35:46 +00:00
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act
|
|
|
|
let response = app.get_publish_newsletter().await;
|
2021-08-14 18:35:46 +00:00
|
|
|
|
|
|
|
// Assert
|
2022-03-13 14:53:56 +00:00
|
|
|
assert_is_redirect_to(&response, "/login");
|
2021-08-14 21:57:03 +00:00
|
|
|
}
|
2021-08-30 15:57:35 +00:00
|
|
|
|
2021-12-26 15:53:58 +00:00
|
|
|
#[tokio::test]
|
2022-03-13 14:53:56 +00:00
|
|
|
async fn you_must_be_logged_in_to_publish_a_newsletter() {
|
2021-08-30 15:57:35 +00:00
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
|
2022-03-13 14:53:56 +00:00
|
|
|
// Act
|
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
|
|
|
"text_content": "Newsletter body as plain text",
|
|
|
|
"html_content": "<p>Newsletter body as HTML</p>",
|
2022-03-13 19:36:44 +00:00
|
|
|
"idempotency_key": uuid::Uuid::new_v4().to_string()
|
2022-03-13 14:53:56 +00:00
|
|
|
});
|
|
|
|
let response = app.post_publish_newsletter(&newsletter_request_body).await;
|
2021-08-30 15:57:35 +00:00
|
|
|
|
|
|
|
// Assert
|
2022-03-13 14:53:56 +00:00
|
|
|
assert_is_redirect_to(&response, "/login");
|
2021-08-30 15:57:35 +00:00
|
|
|
}
|
2022-03-13 19:36:44 +00:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn newsletter_creation_is_idempotent() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
create_confirmed_subscriber(&app).await;
|
|
|
|
app.test_user.login(&app).await;
|
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.expect(1)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act - Part 1 - Submit newsletter form
|
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
|
|
|
"text_content": "Newsletter body as plain text",
|
|
|
|
"html_content": "<p>Newsletter body as HTML</p>",
|
|
|
|
// We expect the idempotency key as part of the
|
|
|
|
// form data, not as an header
|
|
|
|
"idempotency_key": uuid::Uuid::new_v4().to_string()
|
|
|
|
});
|
|
|
|
let response = app.post_publish_newsletter(&newsletter_request_body).await;
|
|
|
|
assert_is_redirect_to(&response, "/admin/newsletters");
|
|
|
|
|
|
|
|
// Act - Part 2 - Follow the redirect
|
|
|
|
let html_page = app.get_publish_newsletter_html().await;
|
|
|
|
assert!(html_page.contains(
|
|
|
|
"<p><i>The newsletter issue has been accepted - \
|
|
|
|
emails will go out shortly.</i></p>"
|
|
|
|
));
|
|
|
|
|
|
|
|
// Act - Part 3 - Submit newsletter form **again**
|
|
|
|
let response = app.post_publish_newsletter(&newsletter_request_body).await;
|
|
|
|
assert_is_redirect_to(&response, "/admin/newsletters");
|
|
|
|
|
|
|
|
// Act - Part 4 - Follow the redirect
|
|
|
|
let html_page = app.get_publish_newsletter_html().await;
|
|
|
|
assert!(html_page.contains(
|
|
|
|
"<p><i>The newsletter issue has been accepted - \
|
|
|
|
emails will go out shortly.</i></p>"
|
|
|
|
));
|
|
|
|
|
|
|
|
app.dispatch_all_pending_emails().await;
|
|
|
|
// Mock verifies on Drop that we have sent the newsletter email **once**
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn concurrent_form_submission_is_handled_gracefully() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
create_confirmed_subscriber(&app).await;
|
|
|
|
app.test_user.login(&app).await;
|
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
// Setting a long delay to ensure that the second request
|
|
|
|
// arrives before the first one completes
|
|
|
|
.respond_with(ResponseTemplate::new(200).set_delay(Duration::from_secs(2)))
|
|
|
|
.expect(1)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act - Submit two newsletter forms concurrently
|
|
|
|
let newsletter_request_body = serde_json::json!({
|
|
|
|
"title": "Newsletter title",
|
|
|
|
"text_content": "Newsletter body as plain text",
|
|
|
|
"html_content": "<p>Newsletter body as HTML</p>",
|
|
|
|
"idempotency_key": uuid::Uuid::new_v4().to_string()
|
|
|
|
});
|
|
|
|
let response1 = app.post_publish_newsletter(&newsletter_request_body);
|
|
|
|
let response2 = app.post_publish_newsletter(&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;
|
|
|
|
// Mock verifies on Drop that we have sent the newsletter email **once**
|
|
|
|
}
|