From 967b16b19b2a7945ea976f59e7945d4bb8b7bc51 Mon Sep 17 00:00:00 2001 From: LukeMathWalker Date: Tue, 9 Mar 2021 22:40:14 +0000 Subject: [PATCH] Get a 404. --- configuration/local.yaml | 1 + src/configuration.rs | 1 + src/routes/subscriptions.rs | 11 +++++++---- src/startup.rs | 11 ++++++++++- tests/api/helpers.rs | 2 ++ tests/api/subscriptions.rs | 7 ++++--- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/configuration/local.yaml b/configuration/local.yaml index 8fd67fa..6c18707 100644 --- a/configuration/local.yaml +++ b/configuration/local.yaml @@ -1,4 +1,5 @@ application: host: 127.0.0.1 + url: "http://127.0.0.1" database: require_ssl: false diff --git a/src/configuration.rs b/src/configuration.rs index b4a31c8..ae71c6d 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -15,6 +15,7 @@ pub struct ApplicationSettings { #[serde(deserialize_with = "deserialize_number_from_string")] pub port: u16, pub host: String, + pub url: String, } #[derive(serde::Deserialize, Clone)] diff --git a/src/routes/subscriptions.rs b/src/routes/subscriptions.rs index e013e4a..2b5ecaf 100644 --- a/src/routes/subscriptions.rs +++ b/src/routes/subscriptions.rs @@ -1,5 +1,6 @@ use crate::domain::{NewSubscriber, SubscriberEmail, SubscriberName}; use crate::email_client::EmailClient; +use crate::startup::ApplicationUrl; use actix_web::{web, HttpResponse}; use chrono::Utc; use sqlx::PgPool; @@ -24,7 +25,7 @@ impl TryInto for FormData { #[tracing::instrument( name = "Adding a new subscriber", - skip(form, pool, email_client), + skip(form, pool, email_client, application_url), fields( email = %form.email, name = %form.name @@ -34,6 +35,7 @@ pub async fn subscribe( form: web::Form, pool: web::Data, email_client: web::Data, + application_url: web::Data, ) -> Result { let new_subscriber = form .0 @@ -43,19 +45,20 @@ pub async fn subscribe( .await .map_err(|_| HttpResponse::InternalServerError().finish())?; // We are swallowing the error for the time being. - let _ = send_confirmation_email(&email_client, new_subscriber).await; + let _ = send_confirmation_email(&email_client, new_subscriber, &application_url.0).await; Ok(HttpResponse::Ok().finish()) } #[tracing::instrument( name = "Send a confirmation email to a new subscriber", - skip(email_client, new_subscriber) + skip(email_client, new_subscriber, application_url) )] pub async fn send_confirmation_email( email_client: &EmailClient, new_subscriber: NewSubscriber, + application_url: &str, ) -> Result<(), reqwest::Error> { - let confirmation_link = "https://my-api.com/subscriptions/confirm"; + let confirmation_link = format!("{}/subscriptions/confirm", application_url); let plain_body = format!( "Welcome to our newsletter!\nVisit {} to confirm your subscription.", confirmation_link diff --git a/src/startup.rs b/src/startup.rs index 6b93720..d7b745c 100644 --- a/src/startup.rs +++ b/src/startup.rs @@ -36,7 +36,12 @@ impl Application { ); let listener = TcpListener::bind(&address)?; let port = listener.local_addr().unwrap().port(); - let server = run(listener, connection_pool, email_client)?; + let server = run( + listener, + connection_pool, + email_client, + configuration.application.url, + )?; Ok(Self { port, server }) } @@ -57,10 +62,13 @@ pub async fn get_connection_pool(configuration: &DatabaseSettings) -> Result Result { let db_pool = Data::new(db_pool); let email_client = Data::new(email_client); @@ -71,6 +79,7 @@ fn run( .route("/subscriptions", web::post().to(subscribe)) .app_data(db_pool.clone()) .app_data(email_client.clone()) + .data(ApplicationUrl(application_url.clone())) }) .listen(listener)? .run(); diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index dfe0e5f..fba61d3 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -33,6 +33,8 @@ impl TestApp { } pub async fn spawn_app() -> TestApp { + lazy_static::initialize(&TRACING); + // Launch a mock server to stand in for Postmark's API let email_server = MockServer::start().await; diff --git a/tests/api/subscriptions.rs b/tests/api/subscriptions.rs index d617fb5..0053195 100644 --- a/tests/api/subscriptions.rs +++ b/tests/api/subscriptions.rs @@ -118,15 +118,16 @@ async fn the_link_returned_by_subscribe_sets_a_subscriber_as_confirmed() { }; 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.domain().unwrap(), "127.0.0.1"); + 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) + let response = reqwest::get(&confirmation_link) .await .unwrap() .error_for_status() .unwrap(); - dbg!(&response); // Assert let saved = sqlx::query!(r#"SELECT status FROM subscriptions WHERE name = 'le guin' AND email = 'ursula_le_guin@gmail.com'"#,)