use learn_axum::configuration::{get_configuration, DatabaseSettings}; use learn_axum::startup::{get_connection_pool, Application}; use learn_axum::telemetry::{get_subscriber, init_subscriber}; use once_cell::sync::Lazy; use sqlx::{Connection, Executor, PgConnection, PgPool}; use uuid::Uuid; use wiremock::MockServer; /// Ensure that the `tracing` stack is only initialised once using `once_cell` static TRACING: Lazy<()> = Lazy::new(|| { let default_filter_level = "info".to_string(); let subscriber_name = "test".to_string(); // We cannot assign the output of `get_subscriber` to a variable based on the // value TEST_LOG` because the sink is part of the type returned by // `get_subscriber`, therefore they are not the same type. We could work around // it, but this is the most straight-forward way of moving forward. 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); }; }); /// Confirmation links embedded in the request to the email API. pub struct ConfirmationLinks { pub html: reqwest::Url, pub plain_text: reqwest::Url, } pub struct TestApp { pub address: String, pub port: u16, pub db_pool: PgPool, pub email_server: MockServer, } impl TestApp { pub async fn get_health_check(&self) -> reqwest::Response { reqwest::Client::new() .get(&format!("{}/health_check", &self.address)) .send() .await .expect("Failed to execute request.") } 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.") } pub fn get_confirmation_links(&self, email_request: &wiremock::Request) -> ConfirmationLinks { 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); let raw_link = links[0].as_str().to_owned(); let mut confirmation_link = reqwest::Url::parse(&raw_link).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(self.port)).unwrap(); confirmation_link }; let html = get_link(&body["HtmlBody"].as_str().unwrap()); let plain_text = get_link(&body["TextBody"].as_str().unwrap()); ConfirmationLinks { html, plain_text } } } pub async fn spawn_app() -> TestApp { // The first time `initialize` is invoked the code in `TRACING` is executed. // All other invocations will instead skip execution. Lazy::force(&TRACING); // Launch a mock server to stand in for Postmark's API let email_server = MockServer::start().await; // Randomise configuration to ensure test isolation let configuration = { let mut c = get_configuration().expect("Failed to read configuration."); // Use a different database for each test case c.database.name = Uuid::new_v4().to_string(); // Use a random OS port c.application.port = 0; // Use the mock server as email API c.email_client.base_url = email_server.uri(); c }; // Create and migrate the database configure_database(&configuration.database).await; let connection_pool = get_connection_pool(&configuration.database); let application = Application::build(configuration.clone()) .await .expect("Failed to build application."); // Get the port before spawning the application let application_port = application.port(); let address = format!("http://127.0.0.1:{}", application_port); // Launch the application as a background task tokio::spawn(async move { application.run().await.expect("Failed to run the server") }); TestApp { address, port: application_port, db_pool: connection_pool, email_server, // test_user: TestUser::generate(), // api_client: client, // email_client: configuration.email_client.client(), } } /// Create a new database and run the migrations. /// /// # Parameters /// * `config`: The database configuration. /// # Returns /// The connection pool. async fn configure_database(config: &DatabaseSettings) -> PgPool { // Create database let mut connection = PgConnection::connect_with(&config.without_db()) .await .expect("Failed to connect to Postgres"); connection .execute(format!(r#"CREATE DATABASE "{}";"#, config.name).as_str()) .await .expect("Failed to create database."); // Migrate database let connection_pool = PgPool::connect_with(config.with_db()) .await .expect("Failed to connect to Postgres."); sqlx::migrate!("./migrations") .run(&connection_pool) .await .expect("Failed to migrate the database"); connection_pool }