@@ -345,17 +345,7 @@ impl Default for Configuration {
345
345
remove_peerless_torrents : true ,
346
346
udp_trackers : Vec :: new ( ) ,
347
347
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 ( ) ,
359
349
health_check_api : HealthCheckApi {
360
350
bind_address : String :: from ( "127.0.0.1:1313" ) ,
361
351
} ,
@@ -399,17 +389,9 @@ impl Configuration {
399
389
///
400
390
/// Will return `Err` if `path` does not exist or has a bad configuration.
401
391
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_" ) ) ;
413
395
414
396
let config: Configuration = figment. extract ( ) ?;
415
397
@@ -475,8 +457,6 @@ impl Configuration {
475
457
476
458
#[ cfg( test) ]
477
459
mod tests {
478
- use figment:: providers:: { Env , Format , Toml } ;
479
- use figment:: Figment ;
480
460
481
461
use crate :: v1:: Configuration ;
482
462
@@ -527,19 +507,55 @@ mod tests {
527
507
config
528
508
}
529
509
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
+
530
551
#[ test]
531
552
fn configuration_should_be_loaded_from_a_toml_config_file ( ) {
532
553
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 ( ) ) ?;
539
555
540
- let config : Configuration = figment . extract ( ) ? ;
556
+ let configuration = Configuration :: load_from_file ( "tracker.toml" ) . expect ( "Could not load configuration from file" ) ;
541
557
542
- assert_eq ! ( config , Configuration :: default ( ) ) ;
558
+ assert_eq ! ( configuration , Configuration :: default ( ) ) ;
543
559
544
560
Ok ( ( ) )
545
561
} ) ;
@@ -548,19 +564,14 @@ mod tests {
548
564
#[ test]
549
565
fn configuration_should_allow_to_overwrite_the_default_tracker_api_token_for_admin ( ) {
550
566
figment:: Jail :: expect_with ( |jail| {
551
- jail. create_file ( "Config .toml" , & default_config_toml ( ) ) ?;
567
+ jail. create_file ( "tracker .toml" , & default_config_toml ( ) ) ?;
552
568
553
569
jail. set_env ( "TORRUST_TRACKER_HTTP_API.ACCESS_TOKENS.ADMIN" , "NewToken" ) ;
554
570
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" ) ;
561
572
562
573
assert_eq ! (
563
- config . http_api. access_tokens. get( "admin" ) ,
574
+ configuration . http_api. access_tokens. get( "admin" ) ,
564
575
Some ( "NewToken" . to_owned( ) ) . as_ref( )
565
576
) ;
566
577
0 commit comments