2021-11-05 14:17:36 +10:00
|
|
|
use std::net::TcpListener;
|
2021-11-08 15:41:52 +10:00
|
|
|
use sqlx::{Connection, Executor, PgConnection, PgPool};
|
|
|
|
use uuid::Uuid;
|
2021-11-16 13:50:32 +10:00
|
|
|
use once_cell::sync::Lazy;
|
2021-11-08 15:41:52 +10:00
|
|
|
|
2021-11-07 00:08:33 +10:00
|
|
|
use mail_app::startup::run;
|
2021-11-08 15:41:52 +10:00
|
|
|
use mail_app::configuration::{get_configuration, DatabaseSettings};
|
2021-11-16 13:50:32 +10:00
|
|
|
use mail_app::telemetry::{get_subscriber, init_subscriber};
|
2021-11-05 14:17:36 +10:00
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
pub struct TestApp {
|
|
|
|
pub address: String,
|
|
|
|
pub db_pool: PgPool,
|
|
|
|
}
|
2021-11-07 00:23:49 +10:00
|
|
|
|
2021-11-16 13:50:32 +10:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-11-05 14:17:36 +10:00
|
|
|
// Create new instance of the application on a random port and return address [`http://localhost:XXXX`]
|
2021-11-08 15:41:52 +10:00
|
|
|
async fn spawn_app() -> TestApp {
|
2021-11-16 13:50:32 +10:00
|
|
|
Lazy::force(&TRACING);
|
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
let listener = TcpListener::bind("127.0.0.1:0")
|
|
|
|
.expect("Failed to bind to random port");
|
2021-11-05 14:17:36 +10:00
|
|
|
let port = listener.local_addr().unwrap().port();
|
2021-11-08 15:41:52 +10:00
|
|
|
let address = format!("http://127.0.0.1:{}", port);
|
2021-11-05 14:17:36 +10:00
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
let mut configuration = get_configuration()
|
|
|
|
.expect("Failed to read configuration.");
|
|
|
|
configuration.database.database_name = Uuid::new_v4().to_string(); // Adjust database string to be random!
|
|
|
|
let connection_pool = configure_database(&configuration.database).await;
|
2021-11-05 14:17:36 +10:00
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
let server = run(listener, connection_pool.clone())
|
|
|
|
.expect("Failed to bind address");
|
2021-11-05 14:17:36 +10:00
|
|
|
let _ = tokio::spawn(server);
|
2021-11-08 15:41:52 +10:00
|
|
|
TestApp {
|
|
|
|
address,
|
|
|
|
db_pool: connection_pool,
|
|
|
|
}
|
|
|
|
}
|
2021-11-05 14:17:36 +10:00
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
pub async fn configure_database(config: &DatabaseSettings) -> PgPool {
|
|
|
|
// Create database
|
|
|
|
let mut connection = PgConnection::connect(&config.connection_string_without_db())
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to Postgres");
|
|
|
|
connection
|
|
|
|
.execute(&*format!(r#"CREATE DATABASE "{}";"#, config.database_name))
|
|
|
|
.await
|
|
|
|
.expect("Failed to create database.");
|
|
|
|
|
|
|
|
// Migrate database
|
|
|
|
let connection_pool = PgPool::connect(&config.connection_string())
|
|
|
|
.await
|
|
|
|
.expect("Failed to connect to Postgres.");
|
|
|
|
sqlx::migrate!("./migrations")
|
|
|
|
.run(&connection_pool)
|
|
|
|
.await
|
|
|
|
.expect("Failed to migrate the database");
|
|
|
|
|
|
|
|
connection_pool
|
2021-11-05 14:17:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn health_check_works() {
|
|
|
|
// Arrange
|
2021-11-08 15:41:52 +10:00
|
|
|
let app = spawn_app().await;
|
2021-11-05 14:17:36 +10:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
|
2021-11-08 15:41:52 +10:00
|
|
|
// Act
|
2021-11-05 14:17:36 +10:00
|
|
|
let response = client
|
2021-11-08 15:41:52 +10:00
|
|
|
.get(&format!("{}/health_check", &app.address))
|
2021-11-05 14:17:36 +10:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Failed to execute request.");
|
|
|
|
|
|
|
|
// Assert our test
|
|
|
|
assert!(response.status().is_success());
|
|
|
|
assert_eq!(Some(0), response.content_length());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn subscribe_returns_200_for_valid_form_data() {
|
|
|
|
// Arrange
|
2021-11-08 15:41:52 +10:00
|
|
|
let app = spawn_app().await;
|
2021-11-05 14:17:36 +10:00
|
|
|
let client = reqwest::Client::new();
|
2021-11-08 15:41:52 +10:00
|
|
|
let body = "name=le%20guin&email=ursula_le_guin%40gmail.com";
|
2021-11-05 14:17:36 +10:00
|
|
|
|
|
|
|
// Act
|
|
|
|
let response = client
|
2021-11-08 15:41:52 +10:00
|
|
|
.post(&format!("{}/subscriptions", &app.address))
|
2021-11-05 14:17:36 +10:00
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
.body(body)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Failed to execute request.");
|
|
|
|
|
|
|
|
// Assert test
|
|
|
|
assert_eq!(200, response.status().as_u16());
|
2021-11-07 00:23:49 +10:00
|
|
|
|
|
|
|
let saved = sqlx::query!("SELECT email, name FROM subscriptions",)
|
2021-11-08 15:41:52 +10:00
|
|
|
.fetch_one(&app.db_pool)
|
2021-11-07 00:23:49 +10:00
|
|
|
.await
|
|
|
|
.expect("Failed to fetch saved subscription.");
|
|
|
|
|
|
|
|
assert_eq!(saved.email, "ursula_le_guin@gmail.com");
|
|
|
|
assert_eq!(saved.name, "le guin");
|
2021-11-05 14:17:36 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
2021-11-08 15:41:52 +10:00
|
|
|
async fn subscribe_returns_400_for_missing_form_data() {
|
2021-11-05 14:17:36 +10:00
|
|
|
//Arrange
|
2021-11-08 15:41:52 +10:00
|
|
|
let app = spawn_app().await;
|
2021-11-05 14:17:36 +10:00
|
|
|
let client = reqwest::Client::new();
|
|
|
|
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 = client
|
2021-11-08 15:41:52 +10:00
|
|
|
.post(&format!("{}/subscriptions", &app.address))
|
2021-11-05 14:17:36 +10:00
|
|
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
.body(invalid_body)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.expect("Failed to execute request.");
|
|
|
|
|
|
|
|
// Assert
|
|
|
|
assert_eq!(
|
|
|
|
400,
|
|
|
|
response.status().as_u16(),
|
|
|
|
// Customised error message on test failure
|
|
|
|
"The API did not fail with 400 Bad Request when the payload was {}.", error_message
|
|
|
|
);
|
|
|
|
}
|
2021-10-22 12:38:38 +10:00
|
|
|
}
|