Skip to content

Commit 632c8ba

Browse files
committed
refactor: move Configuration unit test to inner mods
1 parent 146b77d commit 632c8ba

File tree

3 files changed

+80
-173
lines changed

3 files changed

+80
-173
lines changed

packages/configuration/src/lib.rs

-133
Original file line numberDiff line numberDiff line change
@@ -157,136 +157,3 @@ impl From<figment::Error> for Error {
157157
}
158158
}
159159
}
160-
161-
#[cfg(test)]
162-
mod tests {
163-
use crate::Configuration;
164-
165-
#[cfg(test)]
166-
fn default_config_toml() -> String {
167-
let config = r#"log_level = "info"
168-
mode = "public"
169-
db_driver = "Sqlite3"
170-
db_path = "./storage/tracker/lib/database/sqlite3.db"
171-
announce_interval = 120
172-
min_announce_interval = 120
173-
on_reverse_proxy = false
174-
external_ip = "0.0.0.0"
175-
tracker_usage_statistics = true
176-
persistent_torrent_completed_stat = false
177-
max_peer_timeout = 900
178-
inactive_peer_cleanup_interval = 600
179-
remove_peerless_torrents = true
180-
181-
[[udp_trackers]]
182-
enabled = false
183-
bind_address = "0.0.0.0:6969"
184-
185-
[[http_trackers]]
186-
enabled = false
187-
bind_address = "0.0.0.0:7070"
188-
ssl_enabled = false
189-
ssl_cert_path = ""
190-
ssl_key_path = ""
191-
192-
[http_api]
193-
enabled = true
194-
bind_address = "127.0.0.1:1212"
195-
ssl_enabled = false
196-
ssl_cert_path = ""
197-
ssl_key_path = ""
198-
199-
[http_api.access_tokens]
200-
admin = "MyAccessToken"
201-
202-
[health_check_api]
203-
bind_address = "127.0.0.1:1313"
204-
"#
205-
.lines()
206-
.map(str::trim_start)
207-
.collect::<Vec<&str>>()
208-
.join("\n");
209-
config
210-
}
211-
212-
#[test]
213-
fn configuration_should_have_default_values() {
214-
let configuration = Configuration::default();
215-
216-
let toml = toml::to_string(&configuration).expect("Could not encode TOML value");
217-
218-
assert_eq!(toml, default_config_toml());
219-
}
220-
221-
#[test]
222-
fn configuration_should_contain_the_external_ip() {
223-
let configuration = Configuration::default();
224-
225-
assert_eq!(configuration.external_ip, Some(String::from("0.0.0.0")));
226-
}
227-
228-
#[test]
229-
fn configuration_should_be_saved_in_a_toml_config_file() {
230-
use std::{env, fs};
231-
232-
use uuid::Uuid;
233-
234-
// Build temp config file path
235-
let temp_directory = env::temp_dir();
236-
let temp_file = temp_directory.join(format!("test_config_{}.toml", Uuid::new_v4()));
237-
238-
// Convert to argument type for Configuration::save_to_file
239-
let config_file_path = temp_file;
240-
let path = config_file_path.to_string_lossy().to_string();
241-
242-
let default_configuration = Configuration::default();
243-
244-
default_configuration
245-
.save_to_file(&path)
246-
.expect("Could not save configuration to file");
247-
248-
let contents = fs::read_to_string(&path).expect("Something went wrong reading the file");
249-
250-
assert_eq!(contents, default_config_toml());
251-
}
252-
253-
#[cfg(test)]
254-
fn create_temp_config_file_with_default_config() -> String {
255-
use std::env;
256-
use std::fs::File;
257-
use std::io::Write;
258-
259-
use uuid::Uuid;
260-
261-
// Build temp config file path
262-
let temp_directory = env::temp_dir();
263-
let temp_file = temp_directory.join(format!("test_config_{}.toml", Uuid::new_v4()));
264-
265-
// Convert to argument type for Configuration::load_from_file
266-
let config_file_path = temp_file.clone();
267-
let path = config_file_path.to_string_lossy().to_string();
268-
269-
// Write file contents
270-
let mut file = File::create(temp_file).unwrap();
271-
writeln!(&mut file, "{}", default_config_toml()).unwrap();
272-
273-
path
274-
}
275-
276-
#[test]
277-
fn configuration_should_be_loaded_from_a_toml_config_file() {
278-
let config_file_path = create_temp_config_file_with_default_config();
279-
280-
let configuration = Configuration::load_from_file(&config_file_path).expect("Could not load configuration from file");
281-
282-
assert_eq!(configuration, Configuration::default());
283-
}
284-
285-
#[test]
286-
fn http_api_configuration_should_check_if_it_contains_a_token() {
287-
let configuration = Configuration::default();
288-
289-
assert!(configuration.http_api.access_tokens.values().any(|t| t == "MyAccessToken"));
290-
assert!(!configuration.http_api.access_tokens.values().any(|t| t == "NonExistingToken"));
291-
}
292-
}

packages/configuration/src/v1/mod.rs

+51-40
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,7 @@ impl Default for Configuration {
345345
remove_peerless_torrents: true,
346346
udp_trackers: Vec::new(),
347347
http_trackers: Vec::new(),
348-
http_api: HttpApi {
349-
enabled: true,
350-
bind_address: String::from("127.0.0.1:1212"),
351-
ssl_enabled: false,
352-
ssl_cert_path: None,
353-
ssl_key_path: None,
354-
access_tokens: [(String::from("admin"), String::from("MyAccessToken"))]
355-
.iter()
356-
.cloned()
357-
.collect(),
358-
},
348+
http_api: HttpApi::default(),
359349
health_check_api: HealthCheckApi {
360350
bind_address: String::from("127.0.0.1:1313"),
361351
},
@@ -399,17 +389,9 @@ impl Configuration {
399389
///
400390
/// Will return `Err` if `path` does not exist or has a bad configuration.
401391
pub fn load_from_file(path: &str) -> Result<Configuration, Error> {
402-
let figment = Figment::new().merge(Toml::file(path));
403-
//.merge(Env::prefixed("TORRUST_TRACKER_"));
404-
405-
// code-review: merging values from env vars makes the test
406-
// "configuration_should_be_loaded_from_a_toml_config_file" fail.
407-
//
408-
// It's because this line in a new test:
409-
//
410-
// jail.set_env("TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN", "NewToken");
411-
//
412-
// It seems env vars are shared between tests.
392+
let figment = Figment::new()
393+
.merge(Toml::file(path))
394+
.merge(Env::prefixed("TORRUST_TRACKER_"));
413395

414396
let config: Configuration = figment.extract()?;
415397

@@ -475,8 +457,6 @@ impl Configuration {
475457

476458
#[cfg(test)]
477459
mod tests {
478-
use figment::providers::{Env, Format, Toml};
479-
use figment::Figment;
480460

481461
use crate::v1::Configuration;
482462

@@ -527,19 +507,55 @@ mod tests {
527507
config
528508
}
529509

510+
#[test]
511+
fn configuration_should_have_default_values() {
512+
let configuration = Configuration::default();
513+
514+
let toml = toml::to_string(&configuration).expect("Could not encode TOML value");
515+
516+
assert_eq!(toml, default_config_toml());
517+
}
518+
519+
#[test]
520+
fn configuration_should_contain_the_external_ip() {
521+
let configuration = Configuration::default();
522+
523+
assert_eq!(configuration.external_ip, Some(String::from("0.0.0.0")));
524+
}
525+
526+
#[test]
527+
fn configuration_should_be_saved_in_a_toml_config_file() {
528+
use std::{env, fs};
529+
530+
use uuid::Uuid;
531+
532+
// Build temp config file path
533+
let temp_directory = env::temp_dir();
534+
let temp_file = temp_directory.join(format!("test_config_{}.toml", Uuid::new_v4()));
535+
536+
// Convert to argument type for Configuration::save_to_file
537+
let config_file_path = temp_file;
538+
let path = config_file_path.to_string_lossy().to_string();
539+
540+
let default_configuration = Configuration::default();
541+
542+
default_configuration
543+
.save_to_file(&path)
544+
.expect("Could not save configuration to file");
545+
546+
let contents = fs::read_to_string(&path).expect("Something went wrong reading the file");
547+
548+
assert_eq!(contents, default_config_toml());
549+
}
550+
530551
#[test]
531552
fn configuration_should_be_loaded_from_a_toml_config_file() {
532553
figment::Jail::expect_with(|jail| {
533-
jail.create_file("Config.toml", &default_config_toml())?;
534-
535-
// todo: replace with Configuration method
536-
let figment = Figment::new()
537-
.merge(Toml::file("Config.toml"))
538-
.merge(Env::prefixed("TORRUST_TRACKER_"));
554+
jail.create_file("tracker.toml", &default_config_toml())?;
539555

540-
let config: Configuration = figment.extract()?;
556+
let configuration = Configuration::load_from_file("tracker.toml").expect("Could not load configuration from file");
541557

542-
assert_eq!(config, Configuration::default());
558+
assert_eq!(configuration, Configuration::default());
543559

544560
Ok(())
545561
});
@@ -548,19 +564,14 @@ mod tests {
548564
#[test]
549565
fn configuration_should_allow_to_overwrite_the_default_tracker_api_token_for_admin() {
550566
figment::Jail::expect_with(|jail| {
551-
jail.create_file("Config.toml", &default_config_toml())?;
567+
jail.create_file("tracker.toml", &default_config_toml())?;
552568

553569
jail.set_env("TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN", "NewToken");
554570

555-
// todo: replace with Configuration method
556-
let figment = Figment::new()
557-
.merge(Toml::file("Config.toml"))
558-
.merge(Env::prefixed("TORRUST_TRACKER_"));
559-
560-
let config: Configuration = figment.extract()?;
571+
let configuration = Configuration::load_from_file("tracker.toml").expect("Could not load configuration from file");
561572

562573
assert_eq!(
563-
config.http_api.access_tokens.get("admin"),
574+
configuration.http_api.access_tokens.get("admin"),
564575
Some("NewToken".to_owned()).as_ref()
565576
);
566577

packages/configuration/src/v1/tracker_api.rs

+29
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,37 @@ pub struct HttpApi {
3131
pub access_tokens: AccessTokens,
3232
}
3333

34+
impl Default for HttpApi {
35+
fn default() -> Self {
36+
Self {
37+
enabled: true,
38+
bind_address: String::from("127.0.0.1:1212"),
39+
ssl_enabled: false,
40+
ssl_cert_path: None,
41+
ssl_key_path: None,
42+
access_tokens: [(String::from("admin"), String::from("MyAccessToken"))]
43+
.iter()
44+
.cloned()
45+
.collect(),
46+
}
47+
}
48+
}
49+
3450
impl HttpApi {
3551
pub fn override_admin_token(&mut self, api_admin_token: &str) {
3652
self.access_tokens.insert("admin".to_string(), api_admin_token.to_string());
3753
}
3854
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use crate::v1::tracker_api::HttpApi;
59+
60+
#[test]
61+
fn http_api_configuration_should_check_if_it_contains_a_token() {
62+
let configuration = HttpApi::default();
63+
64+
assert!(configuration.access_tokens.values().any(|t| t == "MyAccessToken"));
65+
assert!(!configuration.access_tokens.values().any(|t| t == "NonExistingToken"));
66+
}
67+
}

0 commit comments

Comments
 (0)