Skip to content

Commit

Permalink
test: [torrust#130] do not update settings for shared test evns
Browse files Browse the repository at this point in the history
The test `it_should_allow_admins_to_update_all_the_settings` overwrites
the config file `config.toml`. It uses the same values but:

1. That does not assert that the file was actually updated.
2. It can lead to conflicts with other tests since all of them use the
   same shared env.

For isolated environments, it should not be a problem becuase we inject
the configuration into the app directly wuthout getting it from the
environment via confif file or env var.
  • Loading branch information
josecelano committed May 4, 2023
1 parent b1ccc6c commit 87edb36
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 97 deletions.
7 changes: 5 additions & 2 deletions tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;

use super::connection_info::ConnectionInfo;
use super::contexts::category::forms::{AddCategoryForm, DeleteCategoryForm};
use super::contexts::settings::form::UpdateSettingsForm;
use super::contexts::settings::form::UpdateSettings;
use super::contexts::torrent::forms::UpdateTorrentFrom;
use super::contexts::torrent::requests::TorrentId;
use super::contexts::user::forms::{LoginForm, RegistrationForm, TokenRenewalForm, TokenVerificationForm, Username};
Expand All @@ -16,6 +16,9 @@ pub struct Client {
}

impl Client {
// todo: forms in POST requests can be passed by reference. It's already
// changed for the `update_settings` method.

pub fn unauthenticated(bind_address: &str) -> Self {
Self::new(ConnectionInfo::anonymous(bind_address))
}
Expand Down Expand Up @@ -80,7 +83,7 @@ impl Client {
self.http_client.get("settings", Query::empty()).await
}

pub async fn update_settings(&self, update_settings_form: UpdateSettingsForm) -> TextResponse {
pub async fn update_settings(&self, update_settings_form: &UpdateSettings) -> TextResponse {
self.http_client.post("settings", &update_settings_form).await
}

Expand Down
2 changes: 1 addition & 1 deletion tests/common/contexts/settings/form.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
use super::Settings;

pub type UpdateSettingsForm = Settings;
pub type UpdateSettings = Settings;
107 changes: 13 additions & 94 deletions tests/e2e/contexts/settings/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::common::client::Client;
use crate::common::contexts::settings::form::UpdateSettingsForm;
use crate::common::contexts::settings::responses::{AllSettingsResponse, Public, PublicSettingsResponse, SiteNameResponse};
use crate::common::contexts::settings::{Auth, Database, ImageCache, Mail, Net, Settings, Tracker, Website};
use crate::e2e::contexts::user::steps::new_logged_in_admin;
use crate::e2e::environment::TestEnv;

Expand Down Expand Up @@ -69,106 +67,27 @@ async fn it_should_allow_admins_to_get_all_the_settings() {
#[tokio::test]
async fn it_should_allow_admins_to_update_all_the_settings() {
let mut env = TestEnv::new();

if !env.is_isolated() {
// This test can't be executed in a non-isolated environment because
// it will change the settings for all the other tests.
return;
}

env.start().await;

let logged_in_admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);

// todo: we can't actually change the settings because it would affect other E2E tests.
// Location for the `config.toml` file is hardcoded. We could use a ENV variable to change it.

let response = client
.update_settings(UpdateSettingsForm {
website: Website {
name: "Torrust".to_string(),
},
tracker: Tracker {
url: "udp://tracker:6969".to_string(),
mode: "Public".to_string(),
api_url: "http://tracker:1212".to_string(),
token: "MyAccessToken".to_string(),
token_valid_seconds: 7_257_600,
},
net: Net {
port: 3000,
base_url: None,
},
auth: Auth {
email_on_signup: "Optional".to_string(),
min_password_length: 6,
max_password_length: 64,
secret_key: "MaxVerstappenWC2021".to_string(),
},
database: Database {
connect_url: "sqlite://storage/database/torrust_index_backend_e2e_testing.db?mode=rwc".to_string(),
torrent_info_update_interval: 3600,
},
mail: Mail {
email_verification_enabled: false,
from: "[email protected]".to_string(),
reply_to: "[email protected]".to_string(),
username: String::new(),
password: String::new(),
server: "mailcatcher".to_string(),
port: 1025,
},
image_cache: ImageCache {
max_request_timeout_ms: 1000,
capacity: 128_000_000,
entry_size_limit: 4_000_000,
user_quota_period_seconds: 3600,
user_quota_bytes: 64_000_000,
},
})
.await;
let mut new_settings = env.server_settings().unwrap();

new_settings.website.name = "UPDATED NAME".to_string();

let response = client.update_settings(&new_settings).await;

let res: AllSettingsResponse = serde_json::from_str(&response.body).unwrap();

assert_eq!(
res.data,
Settings {
website: Website {
name: "Torrust".to_string(),
},
tracker: Tracker {
url: "udp://tracker:6969".to_string(),
mode: "Public".to_string(),
api_url: "http://tracker:1212".to_string(),
token: "MyAccessToken".to_string(),
token_valid_seconds: 7_257_600,
},
net: Net {
port: 3000,
base_url: None,
},
auth: Auth {
email_on_signup: "Optional".to_string(),
min_password_length: 6,
max_password_length: 64,
secret_key: "MaxVerstappenWC2021".to_string(),
},
database: Database {
connect_url: "sqlite://storage/database/torrust_index_backend_e2e_testing.db?mode=rwc".to_string(),
torrent_info_update_interval: 3600,
},
mail: Mail {
email_verification_enabled: false,
from: "[email protected]".to_string(),
reply_to: "[email protected]".to_string(),
username: String::new(),
password: String::new(),
server: "mailcatcher".to_string(),
port: 1025,
},
image_cache: ImageCache {
max_request_timeout_ms: 1000,
capacity: 128_000_000,
entry_size_limit: 4_000_000,
user_quota_period_seconds: 3600,
user_quota_bytes: 64_000_000,
},
}
);
assert_eq!(res.data, new_settings);
if let Some(content_type) = &response.content_type {
assert_eq!(content_type, "application/json");
}
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ impl TestEnv {
Self::default()
}

pub fn is_isolated(&self) -> bool {
matches!(self.mode, State::RunningIsolated)
}

pub async fn start(&mut self) {
let e2e_shared = "TORRUST_IDX_BACK_E2E_SHARED"; // bool

Expand Down

0 comments on commit 87edb36

Please sign in to comment.