From 1df870b2c3aa4892789f0859e3cb4d39e5d012b9 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 2 May 2023 15:40:20 +0100 Subject: [PATCH] refactor: extract logic for setting up test envs E2E tests can be executed with an external shared running app (that must be started before running all the tests) or with an isolated env per test (that can be customized and it lasts only until the end of the test). --- tests/e2e/contexts/about/contract.rs | 2 +- tests/e2e/contexts/category/contract.rs | 2 +- tests/e2e/contexts/category/steps.rs | 2 +- tests/e2e/contexts/root/contract.rs | 2 +- tests/e2e/contexts/settings/contract.rs | 2 +- tests/e2e/contexts/torrent/contract.rs | 10 +++++----- tests/e2e/contexts/torrent/steps.rs | 2 +- tests/e2e/contexts/user/contract.rs | 4 ++-- tests/e2e/contexts/user/steps.rs | 2 +- tests/e2e/mod.rs | 3 +-- .../app_starter.rs | 0 .../environment.rs => environments/isolated.rs} | 6 ++++++ tests/environments/mod.rs | 3 +++ .../environment.rs => environments/shared.rs} | 17 ++++++++++++----- tests/integration/contexts/about.rs | 2 +- tests/integration/mod.rs | 4 +--- tests/mod.rs | 1 + 17 files changed, 39 insertions(+), 25 deletions(-) rename tests/{integration => environments}/app_starter.rs (100%) rename tests/{integration/environment.rs => environments/isolated.rs} (91%) create mode 100644 tests/environments/mod.rs rename tests/{e2e/environment.rs => environments/shared.rs} (59%) diff --git a/tests/e2e/contexts/about/contract.rs b/tests/e2e/contexts/about/contract.rs index 4378ba1a..e9ec2a22 100644 --- a/tests/e2e/contexts/about/contract.rs +++ b/tests/e2e/contexts/about/contract.rs @@ -1,6 +1,6 @@ //! API contract for `about` context. use crate::common::asserts::{assert_response_title, assert_text_ok}; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] diff --git a/tests/e2e/contexts/category/contract.rs b/tests/e2e/contexts/category/contract.rs index 4c952b81..f3cc31f0 100644 --- a/tests/e2e/contexts/category/contract.rs +++ b/tests/e2e/contexts/category/contract.rs @@ -5,7 +5,7 @@ use crate::common::contexts::category::forms::{AddCategoryForm, DeleteCategoryFo use crate::common::contexts::category::responses::{AddedCategoryResponse, ListResponse}; use crate::e2e::contexts::category::steps::add_category; use crate::e2e::contexts::user::steps::{logged_in_admin, logged_in_user}; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; /* todo: - it should allow adding a new category to authenticated clients diff --git a/tests/e2e/contexts/category/steps.rs b/tests/e2e/contexts/category/steps.rs index 4b418760..62bb6924 100644 --- a/tests/e2e/contexts/category/steps.rs +++ b/tests/e2e/contexts/category/steps.rs @@ -1,7 +1,7 @@ use crate::common::contexts::category::forms::AddCategoryForm; use crate::common::responses::TextResponse; use crate::e2e::contexts::user::steps::logged_in_admin; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; pub async fn add_category(category_name: &str) -> TextResponse { let logged_in_admin = logged_in_admin().await; diff --git a/tests/e2e/contexts/root/contract.rs b/tests/e2e/contexts/root/contract.rs index 77feaa84..2d13132a 100644 --- a/tests/e2e/contexts/root/contract.rs +++ b/tests/e2e/contexts/root/contract.rs @@ -1,6 +1,6 @@ //! API contract for `root` context. use crate::common::asserts::{assert_response_title, assert_text_ok}; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] diff --git a/tests/e2e/contexts/settings/contract.rs b/tests/e2e/contexts/settings/contract.rs index 50834c37..256db721 100644 --- a/tests/e2e/contexts/settings/contract.rs +++ b/tests/e2e/contexts/settings/contract.rs @@ -2,7 +2,7 @@ use crate::common::contexts::settings::form::UpdateSettingsForm; use crate::common::contexts::settings::responses::{AllSettingsResponse, Public, PublicSettingsResponse, SiteNameResponse}; use crate::common::contexts::settings::{Auth, Database, Mail, Net, Settings, Tracker, Website}; use crate::e2e::contexts::user::steps::logged_in_admin; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] diff --git a/tests/e2e/contexts/torrent/contract.rs b/tests/e2e/contexts/torrent/contract.rs index 68dc91f9..ae6c1f1a 100644 --- a/tests/e2e/contexts/torrent/contract.rs +++ b/tests/e2e/contexts/torrent/contract.rs @@ -31,7 +31,7 @@ mod for_guests { }; use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::logged_in_user; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] @@ -146,7 +146,7 @@ mod for_authenticated_users { use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; use crate::common::contexts::torrent::responses::UploadedTorrentResponse; use crate::e2e::contexts::user::steps::logged_in_user; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] @@ -235,7 +235,7 @@ mod for_authenticated_users { mod and_non_admins { use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::logged_in_user; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] @@ -256,7 +256,7 @@ mod for_authenticated_users { use crate::common::contexts::torrent::responses::UpdatedTorrentResponse; use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::logged_in_user; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] @@ -294,7 +294,7 @@ mod for_authenticated_users { use crate::common::contexts::torrent::responses::{DeletedTorrentResponse, UpdatedTorrentResponse}; use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::{logged_in_admin, logged_in_user}; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] diff --git a/tests/e2e/contexts/torrent/steps.rs b/tests/e2e/contexts/torrent/steps.rs index d1325d03..db1ca016 100644 --- a/tests/e2e/contexts/torrent/steps.rs +++ b/tests/e2e/contexts/torrent/steps.rs @@ -2,7 +2,7 @@ use crate::common::contexts::torrent::fixtures::{random_torrent, TestTorrent, To use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; use crate::common::contexts::torrent::responses::UploadedTorrentResponse; use crate::common::contexts::user::responses::LoggedInUserData; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; /// Add a new random torrent to the index pub async fn upload_random_torrent_to_index(uploader: &LoggedInUserData) -> (TestTorrent, TorrentListedInIndex) { diff --git a/tests/e2e/contexts/user/contract.rs b/tests/e2e/contexts/user/contract.rs index e5423aa0..4322d40c 100644 --- a/tests/e2e/contexts/user/contract.rs +++ b/tests/e2e/contexts/user/contract.rs @@ -5,7 +5,7 @@ use crate::common::contexts::user::responses::{ SuccessfulLoginResponse, TokenRenewalData, TokenRenewalResponse, TokenVerifiedResponse, }; use crate::e2e::contexts::user::steps::{logged_in_user, registered_user}; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; /* @@ -129,7 +129,7 @@ mod banned_user_list { use crate::common::contexts::user::forms::Username; use crate::common::contexts::user::responses::BannedUserResponse; use crate::e2e::contexts::user::steps::{logged_in_admin, logged_in_user, registered_user}; - use crate::e2e::environment::TestEnv; + use crate::environments::shared::TestEnv; #[tokio::test] #[cfg_attr(not(feature = "e2e-tests"), ignore)] diff --git a/tests/e2e/contexts/user/steps.rs b/tests/e2e/contexts/user/steps.rs index 896153c2..843dae78 100644 --- a/tests/e2e/contexts/user/steps.rs +++ b/tests/e2e/contexts/user/steps.rs @@ -5,7 +5,7 @@ use torrust_index_backend::databases::database::connect_database; use crate::common::contexts::user::fixtures::random_user_registration; use crate::common::contexts::user::forms::{LoginForm, RegisteredUser}; use crate::common::contexts::user::responses::{LoggedInUserData, SuccessfulLoginResponse}; -use crate::e2e::environment::TestEnv; +use crate::environments::shared::TestEnv; pub async fn logged_in_admin() -> LoggedInUserData { let user = logged_in_user().await; diff --git a/tests/e2e/mod.rs b/tests/e2e/mod.rs index d80b0505..1ad6beb7 100644 --- a/tests/e2e/mod.rs +++ b/tests/e2e/mod.rs @@ -29,5 +29,4 @@ //! clean database, delete the files before running the tests. //! //! See the [docker documentation](https://github.com/torrust/torrust-index-backend/tree/develop/docker) for more information on how to run the API. -mod contexts; -pub mod environment; +pub mod contexts; diff --git a/tests/integration/app_starter.rs b/tests/environments/app_starter.rs similarity index 100% rename from tests/integration/app_starter.rs rename to tests/environments/app_starter.rs diff --git a/tests/integration/environment.rs b/tests/environments/isolated.rs similarity index 91% rename from tests/integration/environment.rs rename to tests/environments/isolated.rs index 4cf65582..a10354bb 100644 --- a/tests/integration/environment.rs +++ b/tests/environments/isolated.rs @@ -6,6 +6,9 @@ use crate::common::client::Client; use crate::common::connection_info::{anonymous_connection, authenticated_connection}; use crate::common::random; +/// Provides an isolated test environment for testing. The environment is +/// launched with a temporary directory and a default ephemeral configuration +/// before running the test. pub struct TestEnv { pub app_starter: AppStarter, pub temp_dir: TempDir, @@ -21,6 +24,7 @@ impl TestEnv { /// Provides a test environment with a default configuration for testing /// application. + #[must_use] pub fn with_test_configuration() -> Self { let temp_dir = TempDir::new().expect("failed to create a temporary directory"); @@ -37,6 +41,7 @@ impl TestEnv { } /// Provides an unauthenticated client for integration tests. + #[must_use] pub fn unauthenticated_client(&self) -> Client { Client::new(anonymous_connection( &self @@ -46,6 +51,7 @@ impl TestEnv { } /// Provides an authenticated client for integration tests. + #[must_use] pub fn _authenticated_client(&self, token: &str) -> Client { Client::new(authenticated_connection( &self diff --git a/tests/environments/mod.rs b/tests/environments/mod.rs new file mode 100644 index 00000000..abbdbd41 --- /dev/null +++ b/tests/environments/mod.rs @@ -0,0 +1,3 @@ +pub mod app_starter; +pub mod isolated; +pub mod shared; diff --git a/tests/e2e/environment.rs b/tests/environments/shared.rs similarity index 59% rename from tests/e2e/environment.rs rename to tests/environments/shared.rs index 676253fe..5c1df844 100644 --- a/tests/e2e/environment.rs +++ b/tests/environments/shared.rs @@ -1,26 +1,33 @@ use crate::common::client::Client; use crate::common::connection_info::{anonymous_connection, authenticated_connection}; +/// Provides a shared test environment for testing. All tests shared the same +/// application instance. pub struct TestEnv { pub authority: String, } impl TestEnv { + /// Provides a wrapper for an external running app instance. + /// + /// # Panics + /// + /// Will panic if the app is not running. This function requires the app to + /// be running to provide a valid environment. pub async fn running() -> Self { let env = Self::default(); let client = env.unauthenticated_client(); - assert!( - client.server_is_running().await, - "Test server is not running on {}", - env.authority - ); + let is_running = client.server_is_running().await; + assert!(is_running, "Test server is not running on {}", env.authority); env } + #[must_use] pub fn unauthenticated_client(&self) -> Client { Client::new(anonymous_connection(&self.authority)) } + #[must_use] pub fn authenticated_client(&self, token: &str) -> Client { Client::new(authenticated_connection(&self.authority, token)) } diff --git a/tests/integration/contexts/about.rs b/tests/integration/contexts/about.rs index 8efe9d19..88794909 100644 --- a/tests/integration/contexts/about.rs +++ b/tests/integration/contexts/about.rs @@ -1,5 +1,5 @@ use crate::common::asserts::{assert_response_title, assert_text_ok}; -use crate::integration::environment::TestEnv; +use crate::environments::isolated::TestEnv; #[tokio::test] async fn it_should_load_the_about_page_with_information_about_the_api() { diff --git a/tests/integration/mod.rs b/tests/integration/mod.rs index 343f39d8..0f9779b8 100644 --- a/tests/integration/mod.rs +++ b/tests/integration/mod.rs @@ -1,3 +1 @@ -pub mod app_starter; -mod contexts; -pub mod environment; +pub mod contexts; diff --git a/tests/mod.rs b/tests/mod.rs index d99c2c97..519729e7 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -1,5 +1,6 @@ mod common; mod databases; mod e2e; +pub mod environments; mod integration; mod upgrades;