Roudntrip test.

This commit is contained in:
LukeMathWalker 2021-03-11 09:24:57 +00:00
parent 967b16b19b
commit b3ad683b9a
10 changed files with 94 additions and 64 deletions

View file

@ -1,5 +1,5 @@
application: application:
host: 127.0.0.1 host: 127.0.0.1
url: "http://127.0.0.1" base_url: "http://127.0.0.1"
database: database:
require_ssl: false require_ssl: false

View file

@ -15,7 +15,7 @@ pub struct ApplicationSettings {
#[serde(deserialize_with = "deserialize_number_from_string")] #[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16, pub port: u16,
pub host: String, pub host: String,
pub url: String, pub base_url: String,
} }
#[derive(serde::Deserialize, Clone)] #[derive(serde::Deserialize, Clone)]

View file

@ -1,5 +1,7 @@
mod health_check; mod health_check;
mod subscriptions; mod subscriptions;
mod subscriptions_confirm;
pub use health_check::*; pub use health_check::*;
pub use subscriptions::*; pub use subscriptions::*;
pub use subscriptions_confirm::*;

View file

@ -1,6 +1,6 @@
use crate::domain::{NewSubscriber, SubscriberEmail, SubscriberName}; use crate::domain::{NewSubscriber, SubscriberEmail, SubscriberName};
use crate::email_client::EmailClient; use crate::email_client::EmailClient;
use crate::startup::ApplicationUrl; use crate::startup::ApplicationBaseUrl;
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
use chrono::Utc; use chrono::Utc;
use sqlx::PgPool; use sqlx::PgPool;
@ -25,7 +25,7 @@ impl TryInto<NewSubscriber> for FormData {
#[tracing::instrument( #[tracing::instrument(
name = "Adding a new subscriber", name = "Adding a new subscriber",
skip(form, pool, email_client, application_url), skip(form, pool, email_client, base_url),
fields( fields(
email = %form.email, email = %form.email,
name = %form.name name = %form.name
@ -35,7 +35,7 @@ pub async fn subscribe(
form: web::Form<FormData>, form: web::Form<FormData>,
pool: web::Data<PgPool>, pool: web::Data<PgPool>,
email_client: web::Data<EmailClient>, email_client: web::Data<EmailClient>,
application_url: web::Data<ApplicationUrl>, base_url: web::Data<ApplicationBaseUrl>,
) -> Result<HttpResponse, HttpResponse> { ) -> Result<HttpResponse, HttpResponse> {
let new_subscriber = form let new_subscriber = form
.0 .0
@ -45,20 +45,20 @@ pub async fn subscribe(
.await .await
.map_err(|_| HttpResponse::InternalServerError().finish())?; .map_err(|_| HttpResponse::InternalServerError().finish())?;
// We are swallowing the error for the time being. // We are swallowing the error for the time being.
let _ = send_confirmation_email(&email_client, new_subscriber, &application_url.0).await; let _ = send_confirmation_email(&email_client, new_subscriber, &base_url.0).await;
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::Ok().finish())
} }
#[tracing::instrument( #[tracing::instrument(
name = "Send a confirmation email to a new subscriber", name = "Send a confirmation email to a new subscriber",
skip(email_client, new_subscriber, application_url) skip(email_client, new_subscriber, base_url)
)] )]
pub async fn send_confirmation_email( pub async fn send_confirmation_email(
email_client: &EmailClient, email_client: &EmailClient,
new_subscriber: NewSubscriber, new_subscriber: NewSubscriber,
application_url: &str, base_url: &str,
) -> Result<(), reqwest::Error> { ) -> Result<(), reqwest::Error> {
let confirmation_link = format!("{}/subscriptions/confirm", application_url); let confirmation_link = format!("{}/subscriptions/confirm?subscription_token=mytoken", base_url);
let plain_body = format!( let plain_body = format!(
"Welcome to our newsletter!\nVisit {} to confirm your subscription.", "Welcome to our newsletter!\nVisit {} to confirm your subscription.",
confirmation_link confirmation_link

View file

@ -0,0 +1,14 @@
use actix_web::{HttpResponse, web};
#[derive(serde::Deserialize)]
pub struct Parameters {
subscription_token: String
}
#[tracing::instrument(
name = "Confirm a pending subscriber",
skip(_parameters)
)]
pub async fn confirm(_parameters: web::Query<Parameters>) -> HttpResponse {
HttpResponse::Ok().finish()
}

View file

@ -1,6 +1,6 @@
use crate::configuration::{DatabaseSettings, Settings}; use crate::configuration::{DatabaseSettings, Settings};
use crate::email_client::EmailClient; use crate::email_client::EmailClient;
use crate::routes::{health_check, subscribe}; use crate::routes::{health_check, subscribe, confirm};
use actix_web::dev::Server; use actix_web::dev::Server;
use actix_web::web::Data; use actix_web::web::Data;
use actix_web::{web, App, HttpServer}; use actix_web::{web, App, HttpServer};
@ -40,7 +40,7 @@ impl Application {
listener, listener,
connection_pool, connection_pool,
email_client, email_client,
configuration.application.url, configuration.application.base_url,
)?; )?;
Ok(Self { port, server }) Ok(Self { port, server })
@ -62,13 +62,13 @@ pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result<PgP
.await .await
} }
pub struct ApplicationUrl(pub String); pub struct ApplicationBaseUrl(pub String);
fn run( fn run(
listener: TcpListener, listener: TcpListener,
db_pool: PgPool, db_pool: PgPool,
email_client: EmailClient, email_client: EmailClient,
application_url: String, base_url: String,
) -> Result<Server, std::io::Error> { ) -> Result<Server, std::io::Error> {
let db_pool = Data::new(db_pool); let db_pool = Data::new(db_pool);
let email_client = Data::new(email_client); let email_client = Data::new(email_client);
@ -77,9 +77,10 @@ fn run(
.wrap(TracingLogger) .wrap(TracingLogger)
.route("/health_check", web::get().to(health_check)) .route("/health_check", web::get().to(health_check))
.route("/subscriptions", web::post().to(subscribe)) .route("/subscriptions", web::post().to(subscribe))
.route("/subscriptions/confirm", web::get().to(confirm))
.app_data(db_pool.clone()) .app_data(db_pool.clone())
.app_data(email_client.clone()) .app_data(email_client.clone())
.data(ApplicationUrl(application_url.clone())) .data(ApplicationBaseUrl(base_url.clone()))
}) })
.listen(listener)? .listen(listener)?
.run(); .run();

View file

@ -16,6 +16,7 @@ lazy_static::lazy_static! {
pub struct TestApp { pub struct TestApp {
pub address: String, pub address: String,
pub port: u16,
pub db_pool: PgPool, pub db_pool: PgPool,
pub email_server: MockServer, pub email_server: MockServer,
} }
@ -57,11 +58,12 @@ pub async fn spawn_app() -> TestApp {
let application = Application::build(configuration.clone()) let application = Application::build(configuration.clone())
.await .await
.expect("Failed to build application."); .expect("Failed to build application.");
let address = format!("http://localhost:{}", application.port()); let application_port = application.port();
let _ = tokio::spawn(application.run_until_stopped()); let _ = tokio::spawn(application.run_until_stopped());
TestApp { TestApp {
address, address: format!("http://localhost:{}", application_port),
port: application_port,
db_pool: get_connection_pool(&configuration.database) db_pool: get_connection_pool(&configuration.database)
.await .await
.expect("Failed to connect to the database"), .expect("Failed to connect to the database"),

View file

@ -1,3 +1,4 @@
mod health_check; mod health_check;
mod helpers; mod helpers;
mod subscriptions; mod subscriptions;
mod subscriptions_confirm;

View file

@ -1,5 +1,4 @@
use crate::helpers::spawn_app; use crate::helpers::spawn_app;
use reqwest::Url;
use wiremock::matchers::{method, path}; use wiremock::matchers::{method, path};
use wiremock::{Mock, ResponseTemplate}; use wiremock::{Mock, ResponseTemplate};
@ -91,53 +90,6 @@ async fn subscribe_sends_a_confirmation_email_with_a_link() {
assert_eq!(html_link, text_link); assert_eq!(html_link, text_link);
} }
#[actix_rt::test]
async fn the_link_returned_by_subscribe_sets_a_subscriber_as_confirmed() {
// 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];
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
// Extract the link from one of the request fields.
let get_link = |s: &str| {
let links: Vec<_> = linkify::LinkFinder::new()
.links(s)
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect();
assert_eq!(links.len(), 1);
links[0].as_str().to_owned()
};
let confirmation_link = Url::parse(&get_link(&body["HtmlBody"].as_str().unwrap())).unwrap();
// Let's make sure we don't call random APIs on the web
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
// Let's rewrite the URL to include the port
let confirmation_link = format!("{}{}", app.address, confirmation_link.path());
// Act
let response = reqwest::get(&confirmation_link)
.await
.unwrap()
.error_for_status()
.unwrap();
// Assert
let saved = sqlx::query!(r#"SELECT status FROM subscriptions WHERE name = 'le guin' AND email = 'ursula_le_guin@gmail.com'"#,)
.fetch_one(&app.db_pool)
.await
.expect("Failed to fetch saved subscription.");
assert_eq!(saved.status, "confirmed");
}
#[actix_rt::test] #[actix_rt::test]
async fn subscribe_returns_a_400_when_data_is_missing() { async fn subscribe_returns_a_400_when_data_is_missing() {
// Arrange // Arrange

View file

@ -0,0 +1,58 @@
use crate::helpers::spawn_app;
use reqwest::Url;
use wiremock::{ResponseTemplate, Mock};
use wiremock::matchers::{path, method};
#[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];
let body: serde_json::Value = serde_json::from_slice(&email_request.body).unwrap();
// Extract the link from one of the request fields.
let get_link = |s: &str| {
let links: Vec<_> = linkify::LinkFinder::new()
.links(s)
.filter(|l| *l.kind() == linkify::LinkKind::Url)
.collect();
assert_eq!(links.len(), 1);
links[0].as_str().to_owned()
};
let mut confirmation_link = Url::parse(&get_link(&body["HtmlBody"].as_str().unwrap())).unwrap();
// Let's make sure we don't call random APIs on the web
assert_eq!(confirmation_link.host_str().unwrap(), "127.0.0.1");
confirmation_link.set_port(Some(app.port)).unwrap();
// Act
let response = reqwest::get(confirmation_link)
.await
.unwrap();
// Assert
assert_eq!(response.status().as_u16(), 200);
}