2021-03-11 09:24:57 +00:00
|
|
|
use crate::helpers::spawn_app;
|
2021-04-02 10:51:39 +00:00
|
|
|
use wiremock::matchers::{method, path};
|
|
|
|
use wiremock::{Mock, ResponseTemplate};
|
2021-03-11 09:24:57 +00:00
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn confirmations_without_token_are_rejected_with_a_400() {
|
|
|
|
// Arrange
|
|
|
|
let app = spawn_app().await;
|
|
|
|
|
|
|
|
// Act
|
|
|
|
let response = reqwest::get(&format!("{}/subscriptions/confirm", app.address))
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(response.status().as_u16(), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn the_link_returned_by_subscribe_returns_a_200_if_called() {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
app.post_subscriptions(body.into()).await;
|
|
|
|
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
2021-09-11 19:59:18 +00:00
|
|
|
let confirmation_links = app.get_confirmation_links(email_request);
|
2021-03-11 09:24:57 +00:00
|
|
|
|
|
|
|
// Act
|
2021-04-02 10:51:39 +00:00
|
|
|
let response = reqwest::get(confirmation_links.html).await.unwrap();
|
2021-03-11 09:24:57 +00:00
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(response.status().as_u16(), 200);
|
|
|
|
}
|
|
|
|
|
2021-03-11 21:40:39 +00:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn clicking_on_the_confirmation_link_confirms_a_subscriber() {
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
app.post_subscriptions(body.into()).await;
|
|
|
|
let email_request = &app.email_server.received_requests().await.unwrap()[0];
|
2021-09-11 19:59:18 +00:00
|
|
|
let confirmation_links = app.get_confirmation_links(email_request);
|
2021-03-11 21:40:39 +00:00
|
|
|
|
|
|
|
// Act
|
|
|
|
reqwest::get(confirmation_links.html)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.error_for_status()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
let saved = sqlx::query!("SELECT email, name, status FROM subscriptions",)
|
|
|
|
.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");
|
|
|
|
assert_eq!(saved.status, "confirmed");
|
|
|
|
}
|