-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor integration tests, share server startup code between prod an…
…d test
- Loading branch information
Showing
8 changed files
with
223 additions
and
217 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,41 +1,15 @@ | ||
use std::net::TcpListener; | ||
|
||
use sqlx::postgres::PgPoolOptions; | ||
|
||
use zero2prod::configuration::get_configuration; | ||
use zero2prod::email_client::{AwsSesEmailSender, EmailService}; | ||
use zero2prod::startup::run; | ||
use zero2prod::startup::Application; | ||
use zero2prod::telemetry::{get_subscriber, init_subscriber}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), std::io::Error> { | ||
let subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout); | ||
init_subscriber(subscriber); | ||
let telemetry_subscriber = get_subscriber("zero2prod".into(), "info".into(), std::io::stdout); | ||
init_subscriber(telemetry_subscriber); | ||
|
||
let configuration = get_configuration().expect("Failed to read configuration."); | ||
let server = Application::build(configuration).await?; | ||
|
||
let connection_pool = PgPoolOptions::new() | ||
.connect_with(configuration.database.with_db_name()) | ||
.await | ||
.expect("Failed to connect to the database"); | ||
|
||
sqlx::migrate!("./migrations") | ||
.run(&connection_pool) | ||
.await | ||
.expect("Failed to migrate the database"); | ||
|
||
let aws_client = AwsSesEmailSender::new(configuration.aws.ses_client().await); | ||
let sender_email = configuration | ||
.email_client | ||
.sender() | ||
.expect("Invalid sender email address."); | ||
|
||
let email_client = EmailService::new(aws_client, sender_email); | ||
|
||
let address = format!( | ||
"{}:{}", | ||
configuration.application.host, configuration.application.port | ||
); | ||
let listener = TcpListener::bind(address)?; | ||
run(listener, connection_pool, email_client)?.await | ||
server.run_until_stopped().await?; | ||
Ok(()) | ||
} |
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,19 @@ | ||
use crate::helpers::spawn_app; | ||
|
||
#[tokio::test] | ||
async fn health_check_works() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
let client = reqwest::Client::new(); | ||
|
||
// Act | ||
let response = client | ||
.get(&format!("{}/health_check", &app.address)) | ||
.send() | ||
.await | ||
.expect("Failed to execute request."); | ||
|
||
// Assert | ||
assert!(response.status().is_success()); | ||
assert_eq!(Some(0), response.content_length()); | ||
} |
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,73 @@ | ||
use once_cell::sync::Lazy; | ||
use sqlx::{Connection, Executor, PgConnection, PgPool}; | ||
use uuid::Uuid; | ||
|
||
use zero2prod::configuration::{get_configuration, DatabaseSettings}; | ||
use zero2prod::startup::{get_connection_pool, Application}; | ||
use zero2prod::telemetry::{get_subscriber, init_subscriber}; | ||
|
||
pub struct TestApp { | ||
pub address: String, | ||
pub db_pool: PgPool, | ||
} | ||
|
||
impl TestApp { | ||
pub async fn post_subscriptions(&self, body: String) -> reqwest::Response { | ||
reqwest::Client::new() | ||
.post(&format!("{}/subscriptions", &self.address)) | ||
.header("Content-Type", "application/x-www-form-urlencoded") | ||
.body(body) | ||
.send() | ||
.await | ||
.expect("Failed to execute request.") | ||
} | ||
} | ||
|
||
static TRACING: Lazy<()> = Lazy::new(|| { | ||
let default_filter_level = "info".to_string(); | ||
let subscriber_name = "test".to_string(); | ||
|
||
if std::env::var("TEST_LOG").is_ok() { | ||
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::stdout); | ||
init_subscriber(subscriber); | ||
} else { | ||
let subscriber = get_subscriber(subscriber_name, default_filter_level, std::io::sink); | ||
init_subscriber(subscriber); | ||
}; | ||
}); | ||
|
||
pub async fn spawn_app() -> TestApp { | ||
Lazy::force(&TRACING); | ||
|
||
let configuration = { | ||
let mut configuration = get_configuration().expect("Failed to read configuration."); | ||
configuration.database.database_name = Uuid::new_v4().to_string(); | ||
configuration.application.port = 0; | ||
configuration | ||
}; | ||
|
||
create_database(&configuration.database).await; | ||
|
||
let application = Application::build(configuration.clone()) | ||
.await | ||
.expect("Failed to build application."); | ||
let address = format!("http://127.0.0.1:{}", application.port()); | ||
|
||
let _ = tokio::spawn(application.run_until_stopped()); | ||
|
||
TestApp { | ||
address, | ||
db_pool: get_connection_pool(&configuration.database), | ||
} | ||
} | ||
|
||
async fn create_database(config: &DatabaseSettings) { | ||
let mut connection = PgConnection::connect_with(&config.without_db_name()) | ||
.await | ||
.expect("Failed to connect to Postgres"); | ||
|
||
connection | ||
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name)) | ||
.await | ||
.expect("Failed to create database."); | ||
} |
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,67 @@ | ||
use crate::helpers::spawn_app; | ||
|
||
#[tokio::test] | ||
async fn subscribe_returns_a_200_for_valid_form_data() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com"; | ||
|
||
// Act | ||
let response = app.post_subscriptions(body.into()).await; | ||
|
||
// Assert | ||
assert_eq!(200, response.status().as_u16()); | ||
|
||
let saved = sqlx::query!("SELECT email, name FROM subscriptions",) | ||
.fetch_one(&app.db_pool) | ||
.await | ||
.expect("Failed to fetch saved subscription."); | ||
assert_eq!(saved.email, "[email protected]"); | ||
assert_eq!(saved.name, "le guin"); | ||
} | ||
|
||
#[tokio::test] | ||
async fn subscribe_returns_a_400_when_data_is_missing() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
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 | ||
let response = app.post_subscriptions(invalid_body.into()).await; | ||
|
||
// Assert | ||
assert_eq!( | ||
400, | ||
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_400_when_fields_are_present_but_invalid() { | ||
// Arrange | ||
let app = spawn_app().await; | ||
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 | ||
let response = app.post_subscriptions(body.into()).await; | ||
|
||
assert_eq!( | ||
400, | ||
response.status().as_u16(), | ||
"The API did not return a 400 Bad Request when the payload was {}.", | ||
description | ||
) | ||
} | ||
} |
Oops, something went wrong.