-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a844ae4
commit 6bbf022
Showing
9 changed files
with
188 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,13 @@ | ||
use std::sync::Arc; | ||
|
||
use sqlx::postgres::PgPoolOptions; | ||
use zero2prod::{ | ||
configuration::get_configuration, email_client::EmailClient, startup::run, | ||
configuration::get_configuration, | ||
startup::{run_until_stopped, AppState}, | ||
telemetry::init_tracing, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> std::io::Result<()> { | ||
init_tracing(); | ||
|
||
let configuration = get_configuration().expect("Failed to read configuration."); | ||
|
||
let connection_pool = Arc::new( | ||
PgPoolOptions::new() | ||
.acquire_timeout(std::time::Duration::from_secs(2)) | ||
.connect_lazy_with(configuration.database.with_db()), | ||
); | ||
let address = format!( | ||
"{}:{}", | ||
configuration.application.host, configuration.application.port | ||
); | ||
|
||
let sender_email = configuration | ||
.email_client | ||
.sender() | ||
.expect("Invalid sender email address"); | ||
let timeout = configuration.email_client.timeout(); | ||
let email_client = Arc::new(EmailClient::new( | ||
configuration.email_client.base_url, | ||
sender_email, | ||
configuration.email_client.authorization_token, | ||
timeout, | ||
)); | ||
|
||
let listener = tokio::net::TcpListener::bind(address).await?; | ||
run(listener, connection_pool, email_client).await | ||
let app_state = AppState::build(&configuration).await; | ||
run_until_stopped(app_state, configuration).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{HeaderValue, Request}, | ||
}; | ||
use reqwest::header::CONTENT_LENGTH; | ||
use tower::ServiceExt; | ||
use zero2prod::startup::app; | ||
|
||
use crate::helpers::spawn_app; | ||
|
||
#[tokio::test] | ||
async fn health_check_works() { | ||
let state = spawn_app().await; | ||
let app = app(state); | ||
|
||
let response = app | ||
.oneshot( | ||
Request::builder() | ||
.uri("/health") | ||
.body(Body::empty()) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
assert!(response.status().is_success()); | ||
assert_eq!( | ||
response.headers().get(CONTENT_LENGTH), | ||
Some(&HeaderValue::from_str("0").unwrap()) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use once_cell::sync::Lazy; | ||
use uuid::Uuid; | ||
use zero2prod::{ | ||
configuration::get_configuration, | ||
startup::{configuration_database, AppState}, | ||
telemetry::init_tracing, | ||
}; | ||
|
||
static TRACING: Lazy<()> = Lazy::new(|| { | ||
init_tracing(); | ||
}); | ||
|
||
pub async fn spawn_app() -> AppState { | ||
Lazy::force(&TRACING); | ||
let mut configuration = get_configuration().expect("Failed to read configuration."); | ||
configuration.database.database_name = Uuid::new_v4().to_string(); | ||
configuration_database(&configuration.database).await; | ||
AppState::build(&configuration).await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mod health_check; | ||
mod helpers; | ||
mod subscriptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{self, Request}, | ||
Router, | ||
}; | ||
use tower::ServiceExt; | ||
use zero2prod::startup::app; | ||
|
||
use crate::helpers::spawn_app; | ||
|
||
pub async fn post_subscriptions(app: Router, body: &str) -> http::Response<Body> { | ||
app.oneshot( | ||
Request::builder() | ||
.method(http::Method::POST) | ||
.uri("/subscriptions") | ||
.header( | ||
http::header::CONTENT_TYPE, | ||
mime::APPLICATION_WWW_FORM_URLENCODED.as_ref(), | ||
) | ||
.body(Body::new(body.to_string())) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn subscribe_returns_a_200_for_valid_form_data() { | ||
let state = spawn_app().await; | ||
let app = app(state.clone()); | ||
let body = "name=fan-tastic.z&email=fantastic.fun.zf@gmail.com"; | ||
let response = post_subscriptions(app, body).await; | ||
|
||
assert!(response.status().is_success()); | ||
|
||
#[derive(sqlx::FromRow, Debug, PartialEq, Eq)] | ||
struct Subscription { | ||
name: String, | ||
email: String, | ||
} | ||
let saved: Subscription = sqlx::query_as("SELECT email, name FROM subscriptions") | ||
.fetch_one(state.db_pool.as_ref()) | ||
.await | ||
.expect("Failed to fetch saved subscription."); | ||
|
||
assert_eq!(saved.email, "fantastic.fun.zf@gmail.com"); | ||
assert_eq!(saved.name, "fan-tastic.z"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn subscribe_returns_a_422_when_data_is_missing() { | ||
let state = spawn_app().await; | ||
let app = app(state); | ||
let test_cases = vec![ | ||
("name=fan-tastic.z", "missing the email"), | ||
("email=fantastic.fun.zf@gmail.com", "missing the name"), | ||
("", "missing both name and email"), | ||
]; | ||
|
||
for (invalid_body, error_message) in test_cases { | ||
let response = post_subscriptions(app.clone(), invalid_body).await; | ||
assert_eq!( | ||
422, | ||
response.status().as_u16(), | ||
"The API did not fail with 400 Bad Request when the payload was {}.", | ||
error_message | ||
); | ||
} | ||
} | ||
|
||
#[tokio::test] | ||
async fn subscribe_returns_a_400_when_fields_are_present_but_empty() { | ||
let state = spawn_app().await; | ||
let app = app(state); | ||
|
||
let test_cases = vec![ | ||
("name=&email=fantastic.fun.zf@gmail.com", "empty name"), | ||
("name=fan-tastic.z&email=", "empty email"), | ||
( | ||
"name=fan-tastic.z&email=definitely-not-an-email", | ||
"invalid email", | ||
), | ||
]; | ||
for (body, description) in test_cases { | ||
let response = post_subscriptions(app.clone(), body).await; | ||
assert_eq!( | ||
400, | ||
response.status().as_u16(), | ||
"The API did not return a 200 OK when the payload was {}", | ||
description | ||
); | ||
} | ||
} |
Oops, something went wrong.