Skip to content

Commit

Permalink
refactor: extract logic for setting up test envs
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
josecelano committed May 2, 2023
1 parent 36f17f1 commit 1df870b
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 25 deletions.
2 changes: 1 addition & 1 deletion tests/e2e/contexts/about/contract.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/category/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/category/steps.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/root/contract.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/settings/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 5 additions & 5 deletions tests/e2e/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/torrent/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/contexts/user/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/*
Expand Down Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/contexts/user/steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");

Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/environments/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod app_starter;
pub mod isolated;
pub mod shared;
17 changes: 12 additions & 5 deletions tests/e2e/environment.rs → tests/environments/shared.rs
Original file line number Diff line number Diff line change
@@ -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))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/contexts/about.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
4 changes: 1 addition & 3 deletions tests/integration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub mod app_starter;
mod contexts;
pub mod environment;
pub mod contexts;
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod common;
mod databases;
mod e2e;
pub mod environments;
mod integration;
mod upgrades;

0 comments on commit 1df870b

Please sign in to comment.