Skip to content

Commit 87edb36

Browse files
committed
test: [#130] do not update settings for shared test evns
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.
1 parent b1ccc6c commit 87edb36

File tree

4 files changed

+23
-97
lines changed

4 files changed

+23
-97
lines changed

tests/common/client.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::Serialize;
33

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

1818
impl Client {
19+
// todo: forms in POST requests can be passed by reference. It's already
20+
// changed for the `update_settings` method.
21+
1922
pub fn unauthenticated(bind_address: &str) -> Self {
2023
Self::new(ConnectionInfo::anonymous(bind_address))
2124
}
@@ -80,7 +83,7 @@ impl Client {
8083
self.http_client.get("settings", Query::empty()).await
8184
}
8285

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
use super::Settings;
22

3-
pub type UpdateSettingsForm = Settings;
3+
pub type UpdateSettings = Settings;

tests/e2e/contexts/settings/contract.rs

Lines changed: 13 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use crate::common::client::Client;
2-
use crate::common::contexts::settings::form::UpdateSettingsForm;
32
use crate::common::contexts::settings::responses::{AllSettingsResponse, Public, PublicSettingsResponse, SiteNameResponse};
4-
use crate::common::contexts::settings::{Auth, Database, ImageCache, Mail, Net, Settings, Tracker, Website};
53
use crate::e2e::contexts::user::steps::new_logged_in_admin;
64
use crate::e2e::environment::TestEnv;
75

@@ -69,106 +67,27 @@ async fn it_should_allow_admins_to_get_all_the_settings() {
6967
#[tokio::test]
7068
async fn it_should_allow_admins_to_update_all_the_settings() {
7169
let mut env = TestEnv::new();
70+
71+
if !env.is_isolated() {
72+
// This test can't be executed in a non-isolated environment because
73+
// it will change the settings for all the other tests.
74+
return;
75+
}
76+
7277
env.start().await;
7378

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

77-
// todo: we can't actually change the settings because it would affect other E2E tests.
78-
// Location for the `config.toml` file is hardcoded. We could use a ENV variable to change it.
79-
80-
let response = client
81-
.update_settings(UpdateSettingsForm {
82-
website: Website {
83-
name: "Torrust".to_string(),
84-
},
85-
tracker: Tracker {
86-
url: "udp://tracker:6969".to_string(),
87-
mode: "Public".to_string(),
88-
api_url: "http://tracker:1212".to_string(),
89-
token: "MyAccessToken".to_string(),
90-
token_valid_seconds: 7_257_600,
91-
},
92-
net: Net {
93-
port: 3000,
94-
base_url: None,
95-
},
96-
auth: Auth {
97-
email_on_signup: "Optional".to_string(),
98-
min_password_length: 6,
99-
max_password_length: 64,
100-
secret_key: "MaxVerstappenWC2021".to_string(),
101-
},
102-
database: Database {
103-
connect_url: "sqlite://storage/database/torrust_index_backend_e2e_testing.db?mode=rwc".to_string(),
104-
torrent_info_update_interval: 3600,
105-
},
106-
mail: Mail {
107-
email_verification_enabled: false,
108-
from: "[email protected]".to_string(),
109-
reply_to: "[email protected]".to_string(),
110-
username: String::new(),
111-
password: String::new(),
112-
server: "mailcatcher".to_string(),
113-
port: 1025,
114-
},
115-
image_cache: ImageCache {
116-
max_request_timeout_ms: 1000,
117-
capacity: 128_000_000,
118-
entry_size_limit: 4_000_000,
119-
user_quota_period_seconds: 3600,
120-
user_quota_bytes: 64_000_000,
121-
},
122-
})
123-
.await;
82+
let mut new_settings = env.server_settings().unwrap();
83+
84+
new_settings.website.name = "UPDATED NAME".to_string();
85+
86+
let response = client.update_settings(&new_settings).await;
12487

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

127-
assert_eq!(
128-
res.data,
129-
Settings {
130-
website: Website {
131-
name: "Torrust".to_string(),
132-
},
133-
tracker: Tracker {
134-
url: "udp://tracker:6969".to_string(),
135-
mode: "Public".to_string(),
136-
api_url: "http://tracker:1212".to_string(),
137-
token: "MyAccessToken".to_string(),
138-
token_valid_seconds: 7_257_600,
139-
},
140-
net: Net {
141-
port: 3000,
142-
base_url: None,
143-
},
144-
auth: Auth {
145-
email_on_signup: "Optional".to_string(),
146-
min_password_length: 6,
147-
max_password_length: 64,
148-
secret_key: "MaxVerstappenWC2021".to_string(),
149-
},
150-
database: Database {
151-
connect_url: "sqlite://storage/database/torrust_index_backend_e2e_testing.db?mode=rwc".to_string(),
152-
torrent_info_update_interval: 3600,
153-
},
154-
mail: Mail {
155-
email_verification_enabled: false,
156-
from: "[email protected]".to_string(),
157-
reply_to: "[email protected]".to_string(),
158-
username: String::new(),
159-
password: String::new(),
160-
server: "mailcatcher".to_string(),
161-
port: 1025,
162-
},
163-
image_cache: ImageCache {
164-
max_request_timeout_ms: 1000,
165-
capacity: 128_000_000,
166-
entry_size_limit: 4_000_000,
167-
user_quota_period_seconds: 3600,
168-
user_quota_bytes: 64_000_000,
169-
},
170-
}
171-
);
90+
assert_eq!(res.data, new_settings);
17291
if let Some(content_type) = &response.content_type {
17392
assert_eq!(content_type, "application/json");
17493
}

tests/e2e/environment.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ impl TestEnv {
2727
Self::default()
2828
}
2929

30+
pub fn is_isolated(&self) -> bool {
31+
matches!(self.mode, State::RunningIsolated)
32+
}
33+
3034
pub async fn start(&mut self) {
3135
let e2e_shared = "TORRUST_IDX_BACK_E2E_SHARED"; // bool
3236

0 commit comments

Comments
 (0)