2021-02-14 15:26:43 +00:00
|
|
|
use crate::helpers::spawn_app;
|
2021-03-08 21:10:59 +00:00
|
|
|
use wiremock::matchers::{method, path};
|
|
|
|
use wiremock::{Mock, ResponseTemplate};
|
2021-02-14 12:24:50 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_returns_a_200_for_valid_form_data() {
|
|
|
|
// Arrange
|
2021-02-14 15:26:43 +00:00
|
|
|
let app = spawn_app().await;
|
2021-02-14 12:24:50 +00:00
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
2021-05-01 15:45:19 +00:00
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
2021-02-14 12:24:50 +00:00
|
|
|
// Act
|
2021-02-14 17:07:31 +00:00
|
|
|
let response = app.post_subscriptions(body.into()).await;
|
2021-02-14 12:24:50 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(200, response.status().as_u16());
|
|
|
|
}
|
|
|
|
|
2021-03-08 21:10:59 +00:00
|
|
|
#[actix_rt::test]
|
2021-03-08 23:02:45 +00:00
|
|
|
async fn subscribe_persists_the_new_subscriber() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
|
|
|
// Act
|
2021-03-09 21:12:58 +00:00
|
|
|
app.post_subscriptions(body.into()).await;
|
2021-03-08 23:02:45 +00:00
|
|
|
|
|
|
|
// Assert
|
2021-03-09 21:12:58 +00:00
|
|
|
let saved = sqlx::query!("SELECT email, name, status FROM subscriptions",)
|
2021-03-08 23:02:45 +00:00
|
|
|
.fetch_one(&app.db_pool)
|
|
|
|
.await
|
|
|
|
.expect("Failed to fetch saved subscription.");
|
|
|
|
|
|
|
|
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
|
|
|
|
assert_eq!(saved.name, "le guin");
|
2021-03-09 21:12:58 +00:00
|
|
|
assert_eq!(saved.status, "pending_confirmation");
|
2021-03-08 23:02:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-02 16:02:18 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_fails_if_there_is_a_fatal_database_error() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
// Sabotage the database
|
2021-05-12 21:12:41 +00:00
|
|
|
sqlx::query!("ALTER TABLE subscriptions DROP COLUMN email;",)
|
2021-05-02 16:02:18 +00:00
|
|
|
.execute(&app.db_pool)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Act
|
2021-05-12 21:12:41 +00:00
|
|
|
let response = app.post_subscriptions(body.into()).await;
|
2021-05-02 16:02:18 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(response.status().as_u16(), 500);
|
|
|
|
}
|
|
|
|
|
2021-03-08 23:02:45 +00:00
|
|
|
#[actix_rt::test]
|
2021-03-08 21:10:59 +00:00
|
|
|
async fn subscribe_sends_a_confirmation_email_for_valid_data() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.expect(1)
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
app.post_subscriptions(body.into()).await;
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
// Mock asserts on drop
|
|
|
|
}
|
|
|
|
|
2021-03-08 22:10:23 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_sends_a_confirmation_email_with_a_link() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
|
|
|
|
|
|
|
Mock::given(path("/email"))
|
|
|
|
.and(method("POST"))
|
|
|
|
.respond_with(ResponseTemplate::new(200))
|
|
|
|
.mount(&app.email_server)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
app.post_subscriptions(body.into()).await;
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
2021-03-11 21:14:29 +00:00
|
|
|
let confirmation_links = app.get_confirmation_links(&email_request);
|
|
|
|
|
2021-03-08 22:10:23 +00:00
|
|
|
// The two links should be identical
|
2021-03-11 21:14:29 +00:00
|
|
|
assert_eq!(confirmation_links.html, confirmation_links.plain_text);
|
2021-03-08 22:10:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 12:24:50 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_returns_a_400_when_data_is_missing() {
|
|
|
|
// Arrange
|
2021-02-14 15:26:43 +00:00
|
|
|
let app = spawn_app().await;
|
2021-02-14 12:24:50 +00:00
|
|
|
let test_cases = vec![
|
|
|
|
("name=le%20guin", "missing the email"),
|
|
|
|
("email=ursula_le_guin%40gmail.com", "missing the name"),
|
|
|
|
("", "missing both name and email"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (invalid_body, error_message) in test_cases {
|
|
|
|
// Act
|
2021-02-14 17:07:31 +00:00
|
|
|
let response = app.post_subscriptions(invalid_body.into()).await;
|
2021-02-14 12:24:50 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_returns_a_400_when_fields_are_present_but_invalid() {
|
|
|
|
// Arrange
|
2021-02-14 15:26:43 +00:00
|
|
|
let app = spawn_app().await;
|
2021-02-14 12:24:50 +00:00
|
|
|
let test_cases = vec![
|
|
|
|
("name=&email=ursula_le_guin%40gmail.com", "empty name"),
|
|
|
|
("name=Ursula&email=", "empty email"),
|
|
|
|
("name=Ursula&email=definitely-not-an-email", "invalid email"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (body, description) in test_cases {
|
|
|
|
// Act
|
2021-02-14 17:07:31 +00:00
|
|
|
let response = app.post_subscriptions(body.into()).await;
|
2021-02-14 12:24:50 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(
|
|
|
|
400,
|
|
|
|
response.status().as_u16(),
|
|
|
|
"The API did not return a 400 Bad Request when the payload was {}.",
|
|
|
|
description
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|