Skip to content

Commit f32f0bf

Browse files
committed
refactor: [#1235] remove pun method only used for testing
Now that we inject dependencies we can write assert using the dependencies instead of exposing public methods.
1 parent 1735dfc commit f32f0bf

File tree

7 files changed

+70
-43
lines changed

7 files changed

+70
-43
lines changed

packages/tracker-core/src/core_tests.rs

+21
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ use std::sync::Arc;
55
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
66
use bittorrent_primitives::info_hash::InfoHash;
77
use torrust_tracker_configuration::Configuration;
8+
#[cfg(test)]
9+
use torrust_tracker_configuration::Core;
810
use torrust_tracker_primitives::peer::Peer;
911
use torrust_tracker_primitives::DurationSinceUnixEpoch;
12+
#[cfg(test)]
13+
use torrust_tracker_test_helpers::configuration::ephemeral_sqlite_database;
1014

1115
use super::announce_handler::AnnounceHandler;
1216
use super::databases::setup::initialize_database;
@@ -103,3 +107,20 @@ pub fn initialize_handlers(config: &Configuration) -> (Arc<AnnounceHandler>, Arc
103107

104108
(announce_handler, scrape_handler)
105109
}
110+
111+
/// # Panics
112+
///
113+
/// Will panic if the temporary file path is not a valid UFT string.
114+
#[cfg(test)]
115+
#[must_use]
116+
pub fn ephemeral_configuration_for_listed_tracker() -> Core {
117+
let mut config = Core {
118+
listed: true,
119+
..Default::default()
120+
};
121+
122+
let temp_file = ephemeral_sqlite_database();
123+
temp_file.to_str().unwrap().clone_into(&mut config.database.path);
124+
125+
config
126+
}

packages/tracker-core/src/whitelist/manager.rs

+37-18
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ impl WhitelistManager {
5353
self.in_memory_whitelist.remove(info_hash).await
5454
}
5555

56-
/// It checks if a torrent is whitelisted.
57-
pub async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> bool {
58-
self.in_memory_whitelist.contains(info_hash).await
59-
}
60-
6156
/// It loads the whitelist from the database.
6257
///
6358
/// # Errors
@@ -81,17 +76,41 @@ mod tests {
8176

8277
use std::sync::Arc;
8378

84-
use torrust_tracker_test_helpers::configuration;
79+
use torrust_tracker_configuration::Core;
8580

81+
use crate::core_tests::ephemeral_configuration_for_listed_tracker;
82+
use crate::databases::setup::initialize_database;
83+
use crate::databases::Database;
8684
use crate::whitelist::manager::WhitelistManager;
87-
use crate::whitelist::whitelist_tests::initialize_whitelist_services;
85+
use crate::whitelist::repository::in_memory::InMemoryWhitelist;
86+
use crate::whitelist::repository::persisted::DatabaseWhitelist;
8887

89-
fn initialize_whitelist_manager_for_whitelisted_tracker() -> Arc<WhitelistManager> {
90-
let config = configuration::ephemeral_listed();
88+
struct WhitelistManagerDeps {
89+
pub _database: Arc<Box<dyn Database>>,
90+
pub _database_whitelist: Arc<DatabaseWhitelist>,
91+
pub in_memory_whitelist: Arc<InMemoryWhitelist>,
92+
}
9193

92-
let (_whitelist_authorization, whitelist_manager) = initialize_whitelist_services(&config);
94+
fn initialize_whitelist_manager_for_whitelisted_tracker() -> (Arc<WhitelistManager>, Arc<WhitelistManagerDeps>) {
95+
let config = ephemeral_configuration_for_listed_tracker();
96+
initialize_whitelist_manager_and_deps(&config)
97+
}
9398

94-
whitelist_manager
99+
fn initialize_whitelist_manager_and_deps(config: &Core) -> (Arc<WhitelistManager>, Arc<WhitelistManagerDeps>) {
100+
let database = initialize_database(config);
101+
let database_whitelist = Arc::new(DatabaseWhitelist::new(database.clone()));
102+
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
103+
104+
let whitelist_manager = Arc::new(WhitelistManager::new(database_whitelist.clone(), in_memory_whitelist.clone()));
105+
106+
(
107+
whitelist_manager,
108+
Arc::new(WhitelistManagerDeps {
109+
_database: database,
110+
_database_whitelist: database_whitelist,
111+
in_memory_whitelist,
112+
}),
113+
)
95114
}
96115

97116
mod configured_as_whitelisted {
@@ -102,26 +121,26 @@ mod tests {
102121

103122
#[tokio::test]
104123
async fn it_should_add_a_torrent_to_the_whitelist() {
105-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
124+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
106125

107126
let info_hash = sample_info_hash();
108127

109128
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
110129

111-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
130+
assert!(services.in_memory_whitelist.contains(&info_hash).await);
112131
}
113132

114133
#[tokio::test]
115134
async fn it_should_remove_a_torrent_from_the_whitelist() {
116-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
135+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
117136

118137
let info_hash = sample_info_hash();
119138

120139
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
121140

122141
whitelist_manager.remove_torrent_from_whitelist(&info_hash).await.unwrap();
123142

124-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
143+
assert!(!services.in_memory_whitelist.contains(&info_hash).await);
125144
}
126145

127146
mod persistence {
@@ -130,19 +149,19 @@ mod tests {
130149

131150
#[tokio::test]
132151
async fn it_should_load_the_whitelist_from_the_database() {
133-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
152+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
134153

135154
let info_hash = sample_info_hash();
136155

137156
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
138157

139158
whitelist_manager.remove_torrent_from_memory_whitelist(&info_hash).await;
140159

141-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
160+
assert!(!services.in_memory_whitelist.contains(&info_hash).await);
142161

143162
whitelist_manager.load_whitelist_from_database().await.unwrap();
144163

145-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
164+
assert!(services.in_memory_whitelist.contains(&info_hash).await);
146165
}
147166
}
148167
}

packages/tracker-core/src/whitelist/repository/persisted.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,7 @@ impl DatabaseWhitelist {
6565
mod tests {
6666
mod the_persisted_whitelist_repository {
6767

68-
use torrust_tracker_configuration::Core;
69-
use torrust_tracker_test_helpers::configuration::ephemeral_sqlite_database;
70-
71-
use crate::core_tests::sample_info_hash;
68+
use crate::core_tests::{ephemeral_configuration_for_listed_tracker, sample_info_hash};
7269
use crate::databases::setup::initialize_database;
7370
use crate::whitelist::repository::persisted::DatabaseWhitelist;
7471

@@ -78,18 +75,6 @@ mod tests {
7875
DatabaseWhitelist::new(database)
7976
}
8077

81-
fn ephemeral_configuration_for_listed_tracker() -> Core {
82-
let mut config = Core {
83-
listed: true,
84-
..Default::default()
85-
};
86-
87-
let temp_file = ephemeral_sqlite_database();
88-
temp_file.to_str().unwrap().clone_into(&mut config.database.path);
89-
90-
config
91-
}
92-
9378
#[test]
9479
fn should_add_a_new_infohash_to_the_list() {
9580
let whitelist = initialize_database_whitelist();

src/bootstrap/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer {
141141
scrape_handler,
142142
keys_handler,
143143
authentication_service,
144+
in_memory_whitelist,
144145
whitelist_authorization,
145146
ban_service,
146147
http_stats_event_sender,

src/container.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
1010
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1111
use bittorrent_tracker_core::whitelist;
1212
use bittorrent_tracker_core::whitelist::manager::WhitelistManager;
13+
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
1314
use tokio::sync::RwLock;
1415
use torrust_tracker_configuration::{Core, HttpApi, HttpTracker, UdpTracker};
1516

@@ -23,6 +24,7 @@ pub struct AppContainer {
2324
pub scrape_handler: Arc<ScrapeHandler>,
2425
pub keys_handler: Arc<KeysHandler>,
2526
pub authentication_service: Arc<AuthenticationService>,
27+
pub in_memory_whitelist: Arc<InMemoryWhitelist>,
2628
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
2729
pub ban_service: Arc<RwLock<BanService>>,
2830
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,

tests/servers/api/environment.rs

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use bittorrent_primitives::info_hash::InfoHash;
55
use bittorrent_tracker_core::authentication::service::AuthenticationService;
66
use bittorrent_tracker_core::databases::Database;
7+
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
78
use futures::executor::block_on;
89
use torrust_tracker_api_client::connection_info::{ConnectionInfo, Origin};
910
use torrust_tracker_configuration::Configuration;
@@ -22,6 +23,7 @@ where
2223

2324
pub database: Arc<Box<dyn Database>>,
2425
pub authentication_service: Arc<AuthenticationService>,
26+
pub in_memory_whitelist: Arc<InMemoryWhitelist>,
2527

2628
pub registar: Registar,
2729
pub server: ApiServer<S>,
@@ -70,6 +72,7 @@ impl Environment<Stopped> {
7072

7173
database: app_container.database.clone(),
7274
authentication_service: app_container.authentication_service.clone(),
75+
in_memory_whitelist: app_container.in_memory_whitelist.clone(),
7376

7477
registar: Registar::default(),
7578
server,
@@ -84,6 +87,7 @@ impl Environment<Stopped> {
8487

8588
database: self.database.clone(),
8689
authentication_service: self.authentication_service.clone(),
90+
in_memory_whitelist: self.in_memory_whitelist.clone(),
8791

8892
registar: self.registar.clone(),
8993
server: self
@@ -106,6 +110,7 @@ impl Environment<Running> {
106110

107111
database: self.database,
108112
authentication_service: self.authentication_service,
113+
in_memory_whitelist: self.in_memory_whitelist,
109114

110115
registar: Registar::default(),
111116
server: self.server.stop().await.unwrap(),

tests/servers/api/v1/contract/context/whitelist.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ async fn should_allow_whitelisting_a_torrent() {
3131

3232
assert_ok(response).await;
3333
assert!(
34-
env.http_api_container
35-
.whitelist_manager
36-
.is_info_hash_whitelisted(&InfoHash::from_str(&info_hash).unwrap())
34+
env.in_memory_whitelist
35+
.contains(&InfoHash::from_str(&info_hash).unwrap())
3736
.await
3837
);
3938

@@ -181,12 +180,7 @@ async fn should_allow_removing_a_torrent_from_the_whitelist() {
181180
.await;
182181

183182
assert_ok(response).await;
184-
assert!(
185-
!env.http_api_container
186-
.whitelist_manager
187-
.is_info_hash_whitelisted(&info_hash)
188-
.await
189-
);
183+
assert!(!env.in_memory_whitelist.contains(&info_hash).await);
190184

191185
env.stop().await;
192186
}

0 commit comments

Comments
 (0)