From f208876bb797b3812496d5648a585ea574f5f6fc Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Tue, 28 Jan 2025 16:08:50 +0000 Subject: [PATCH] refactor: [#1215] instantiate only needed services --- src/core/services/statistics/mod.rs | 14 +- src/core/services/torrent.rs | 55 +----- src/servers/http/v1/handlers/announce.rs | 165 ++++++++---------- src/servers/http/v1/handlers/scrape.rs | 203 +++++++---------------- 4 files changed, 143 insertions(+), 294 deletions(-) diff --git a/src/core/services/statistics/mod.rs b/src/core/services/statistics/mod.rs index 18d96605..79bc5f26 100644 --- a/src/core/services/statistics/mod.rs +++ b/src/core/services/statistics/mod.rs @@ -117,8 +117,8 @@ mod tests { use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::services::statistics::{self, get_metrics, TrackerMetrics}; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::{self}; use crate::servers::udp::server::banning::BanService; use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP; @@ -131,19 +131,9 @@ mod tests { async fn the_statistics_service_should_return_the_tracker_metrics() { let config = tracker_configuration(); - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_repository = Arc::new(stats_repository); - let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP))); let tracker_metrics = get_metrics( diff --git a/src/core/services/torrent.rs b/src/core/services/torrent.rs index 6ae2c26a..d809fc26 100644 --- a/src/core/services/torrent.rs +++ b/src/core/services/torrent.rs @@ -112,29 +112,10 @@ pub async fn get_torrents( #[cfg(test)] mod tests { use std::net::{IpAddr, Ipv4Addr, SocketAddr}; - use std::sync::Arc; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; - use torrust_tracker_configuration::Configuration; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; - use crate::app_test::initialize_tracker_dependencies; - use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - - fn initialize_in_memory_torrent_repository(config: &Configuration) -> Arc { - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(config); - - in_memory_torrent_repository - } - fn sample_peer() -> peer::Peer { peer::Peer { peer_id: PeerId(*b"-qB00000000000000000"), @@ -153,17 +134,11 @@ mod tests { use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Configuration; - use torrust_tracker_test_helpers::configuration; - use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer}; + use crate::core::services::torrent::tests::sample_peer; use crate::core::services::torrent::{get_torrent_info, Info}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - pub fn tracker_configuration() -> Configuration { - configuration::ephemeral() - } - #[tokio::test] async fn should_return_none_if_the_tracker_does_not_have_the_torrent() { let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); @@ -179,9 +154,7 @@ mod tests { #[tokio::test] async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash = InfoHash::from_str(&hash).unwrap(); @@ -210,17 +183,11 @@ mod tests { use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Configuration; - use torrust_tracker_test_helpers::configuration; - use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer}; + use crate::core::services::torrent::tests::sample_peer; use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - pub fn tracker_configuration() -> Configuration { - configuration::ephemeral() - } - #[tokio::test] async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() { let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); @@ -232,9 +199,7 @@ mod tests { #[tokio::test] async fn should_return_a_summarized_info_for_all_torrents() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash = InfoHash::from_str(&hash).unwrap(); @@ -256,9 +221,7 @@ mod tests { #[tokio::test] async fn should_allow_limiting_the_number_of_torrents_in_the_result() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); @@ -279,9 +242,7 @@ mod tests { #[tokio::test] async fn should_allow_using_pagination_in_the_result() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); @@ -311,9 +272,7 @@ mod tests { #[tokio::test] async fn should_return_torrents_ordered_by_info_hash() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index 247c6b8c..d6c85032 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -250,76 +250,73 @@ mod tests { use bittorrent_http_protocol::v1::requests::announce::Announce; use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; + use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; - use crate::core::services::statistics; + use crate::core::core_tests::sample_info_hash; + use crate::core::services::{initialize_database, statistics}; use crate::core::statistics::event::sender::Sender; - use crate::core::whitelist; - - type TrackerAndDeps = ( - Arc, - Arc, - Arc>>, - Arc, - Arc, - ); - - fn private_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_private()) + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; + + struct CoreTrackerServices { + pub core_config: Arc, + pub announce_handler: Arc, + pub stats_event_sender: Arc>>, + pub whitelist_authorization: Arc, + pub authentication_service: Arc, } - fn whitelisted_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_listed()) + fn initialize_private_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_private()) } - fn tracker_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_with_reverse_proxy()) + fn initialize_listed_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_listed()) } - fn tracker_not_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_without_reverse_proxy()) + fn initialize_tracker_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy()) } - /// Initialize tracker's dependencies and tracker. - fn initialize_tracker_and_deps(config: Configuration) -> TrackerAndDeps { - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + fn initialize_tracker_not_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy()) + } + fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices { + let core_config = Arc::new(config.core.clone()); + let database = initialize_database(config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); + let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_event_sender = Arc::new(stats_event_sender); - let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &in_memory_torrent_repository, &db_torrent_repository, )); - let config = Arc::new(config.core); - - ( - config, + CoreTrackerServices { + core_config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service, - ) + } } fn sample_announce_request() -> Announce { Announce { - info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap(), + info_hash: sample_info_hash(), peer_id: PeerId(*b"-qB00000000000000001"), port: 17548, downloaded: None, @@ -348,28 +345,24 @@ mod tests { mod with_tracker_in_private_mode { use std::str::FromStr; - use std::sync::Arc; - use super::{private_tracker, sample_announce_request, sample_client_ip_sources}; + use super::{initialize_private_tracker, sample_announce_request, sample_client_ip_sources}; use crate::core::authentication; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_missing() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - private_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_private_tracker(); let maybe_key = None; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &sample_client_ip_sources(), maybe_key, @@ -385,21 +378,18 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_invalid() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - private_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_private_tracker(); let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); let maybe_key = Some(unregistered_key); let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &sample_client_ip_sources(), maybe_key, @@ -413,27 +403,22 @@ mod tests { mod with_tracker_in_listed_mode { - use std::sync::Arc; - - use super::{sample_announce_request, sample_client_ip_sources, whitelisted_tracker}; + use super::{initialize_listed_tracker, sample_announce_request, sample_client_ip_sources}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - whitelisted_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_listed_tracker(); let announce_request = sample_announce_request(); let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &announce_request, &sample_client_ip_sources(), None, @@ -453,20 +438,15 @@ mod tests { mod with_tracker_on_reverse_proxy { - use std::sync::Arc; - use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_announce_request, tracker_on_reverse_proxy}; + use super::{initialize_tracker_on_reverse_proxy, sample_announce_request}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - tracker_on_reverse_proxy(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_tracker_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -474,11 +454,11 @@ mod tests { }; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &client_ip_sources, None, @@ -495,20 +475,15 @@ mod tests { mod with_tracker_not_on_reverse_proxy { - use std::sync::Arc; - use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_announce_request, tracker_not_on_reverse_proxy}; + use super::{initialize_tracker_not_on_reverse_proxy, sample_announce_request}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - tracker_not_on_reverse_proxy(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_tracker_not_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -516,11 +491,11 @@ mod tests { }; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &client_ip_sources, None, diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index c4013d8e..a197263e 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -171,137 +171,62 @@ mod tests { use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Core; + use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; + use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; use crate::core::scrape_handler::ScrapeHandler; use crate::core::services::statistics; - - #[allow(clippy::type_complexity)] - fn private_tracker() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_private(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; + + struct CoreTrackerServices { + pub core_config: Arc, + pub scrape_handler: Arc, + pub stats_event_sender: Arc>>, + pub authentication_service: Arc, } - #[allow(clippy::type_complexity)] - fn whitelisted_tracker() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_listed(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + fn initialize_private_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_private()) } - #[allow(clippy::type_complexity)] - fn tracker_on_reverse_proxy() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_with_reverse_proxy(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + fn initialize_listed_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_listed()) } - #[allow(clippy::type_complexity)] - fn tracker_not_on_reverse_proxy() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_without_reverse_proxy(); + fn initialize_tracker_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy()) + } - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + fn initialize_tracker_not_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy()) + } + fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices { + let core_config = Arc::new(config.core.clone()); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); + let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (core_config, scrape_handler, stats_event_sender, authentication_service) + CoreTrackerServices { + core_config, + scrape_handler, + stats_event_sender, + authentication_service, + } } fn sample_scrape_request() -> Scrape { Scrape { - info_hashes: vec!["3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap()], // # DevSkim: ignore DS173237 + info_hashes: vec!["3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap()], // DevSkim: ignore DS173237 } } @@ -324,22 +249,22 @@ mod tests { use torrust_tracker_primitives::core::ScrapeData; - use super::{private_tracker, sample_client_ip_sources, sample_scrape_request}; + use super::{initialize_private_tracker, sample_client_ip_sources, sample_scrape_request}; use crate::core::authentication; use crate::servers::http::v1::handlers::scrape::handle_scrape; #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_missing() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let core_tracker_services = initialize_private_tracker(); let scrape_request = sample_scrape_request(); let maybe_key = None; let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), maybe_key, @@ -354,17 +279,17 @@ mod tests { #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_invalid() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let core_tracker_services = initialize_private_tracker(); let scrape_request = sample_scrape_request(); let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); let maybe_key = Some(unregistered_key); let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), maybe_key, @@ -382,20 +307,20 @@ mod tests { use torrust_tracker_primitives::core::ScrapeData; - use super::{sample_client_ip_sources, sample_scrape_request, whitelisted_tracker}; + use super::{initialize_listed_tracker, sample_client_ip_sources, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_torrent_is_not_whitelisted() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = whitelisted_tracker(); + let core_tracker_services = initialize_listed_tracker(); let scrape_request = sample_scrape_request(); let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), None, @@ -413,13 +338,13 @@ mod tests { use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_scrape_request, tracker_on_reverse_proxy}; + use super::{initialize_tracker_on_reverse_proxy, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; use crate::servers::http::v1::handlers::scrape::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = tracker_on_reverse_proxy(); + let core_tracker_services = initialize_tracker_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -427,10 +352,10 @@ mod tests { }; let response = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &sample_scrape_request(), &client_ip_sources, None, @@ -449,13 +374,13 @@ mod tests { use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_scrape_request, tracker_not_on_reverse_proxy}; + use super::{initialize_tracker_not_on_reverse_proxy, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; use crate::servers::http::v1::handlers::scrape::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = tracker_not_on_reverse_proxy(); + let core_tracker_services = initialize_tracker_not_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -463,10 +388,10 @@ mod tests { }; let response = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &sample_scrape_request(), &client_ip_sources, None,