Skip to content

Commit 109aef6

Browse files
committed
Merge torrust#1237: Overhaul core Tracker: add tests for whitelist mod
e994aa2 refactor: [torrust#1235] WhitelistManager tests (Jose Celano) 2862c77 refactor: [torrust#1235] remove another pub methog only used for testing (Jose Celano) f32f0bf refactor: [torrust#1235] remove pun method only used for testing (Jose Celano) 1735dfc refactor: [torrust#1235] remove unused methods (Jose Celano) 933b6b0 test: [torrust#1235] add tests for WhitelistAuthorization (Jose Celano) b4a4250 test: [torrust#1235] add tests for DatabaseWhitelist (Jose Celano) Pull request description: Overhaul core Tracker: add tests for `whitelist` mod Coverage before: ```output Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- packages/tracker-core/src/whitelist/authorization.rs 18 0 100.00% 8 0 100.00% 46 0 100.00% 0 0 - packages/tracker-core/src/whitelist/manager.rs 49 7 85.71% 21 3 85.71% 113 7 93.81% 0 0 - packages/tracker-core/src/whitelist/mod.rs 13 0 100.00% 4 0 100.00% 33 0 100.00% 0 0 - packages/tracker-core/src/whitelist/repository/in_memory.rs 28 0 100.00% 16 0 100.00% 85 0 100.00% 0 0 - packages/tracker-core/src/whitelist/repository/persisted.rs 22 6 72.73% 4 0 100.00% 24 2 91.67% 0 0 - packages/tracker-core/src/whitelist/setup.rs 1 0 100.00% 1 0 100.00% 7 0 100.00% 0 0 - packages/tracker-core/src/whitelist/whitelist_tests.rs 3 0 100.00% 2 0 100.00% 11 0 100.00% 0 0 - ``` Coverage after: ```output Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- packages/tracker-core/src/whitelist/authorization.rs 35 1 97.14% 16 0 100.00% 100 0 100.00% 0 0 - packages/tracker-core/src/whitelist/manager.rs 39 3 92.31% 15 0 100.00% 109 0 100.00% 0 0 - packages/tracker-core/src/whitelist/mod.rs 13 0 100.00% 4 0 100.00% 33 0 100.00% 0 0 - packages/tracker-core/src/whitelist/repository/in_memory.rs 28 0 100.00% 16 0 100.00% 85 0 100.00% 0 0 - packages/tracker-core/src/whitelist/repository/persisted.rs 33 4 87.88% 10 0 100.00% 79 0 100.00% 0 0 - packages/tracker-core/src/whitelist/setup.rs 1 0 100.00% 1 0 100.00% 7 0 100.00% 0 0 - packages/tracker-core/src/whitelist/whitelist_tests.rs 3 0 100.00% 2 0 100.00% 11 0 100.00% 0 0 - ``` ACKs for top commit: josecelano: ACK e994aa2 Tree-SHA512: fea5a1b6fe0d25118563e70e71c8829c0efbacbbe69137c01dedb69dea457db4b4db3c5cc0ade902429d9c6a01d4f77b839d124ca953c32829efefeb8db936d1
2 parents 4a9c083 + e994aa2 commit 109aef6

File tree

8 files changed

+231
-60
lines changed

8 files changed

+231
-60
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/authorization.rs

+84-10
Original file line numberDiff line numberDiff line change
@@ -61,33 +61,107 @@ impl WhitelistAuthorization {
6161
#[cfg(test)]
6262
mod tests {
6363

64-
mod configured_as_whitelisted {
64+
mod the_whitelist_authorization_for_announce_and_scrape_actions {
65+
use std::sync::Arc;
66+
67+
use torrust_tracker_configuration::Core;
68+
69+
use crate::whitelist::authorization::WhitelistAuthorization;
70+
use crate::whitelist::repository::in_memory::InMemoryWhitelist;
71+
72+
fn initialize_whitelist_authorization_with(config: &Core) -> Arc<WhitelistAuthorization> {
73+
let (whitelist_authorization, _in_memory_whitelist) =
74+
initialize_whitelist_authorization_and_dependencies_with(config);
75+
whitelist_authorization
76+
}
77+
78+
fn initialize_whitelist_authorization_and_dependencies_with(
79+
config: &Core,
80+
) -> (Arc<WhitelistAuthorization>, Arc<InMemoryWhitelist>) {
81+
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
82+
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(config, &in_memory_whitelist.clone()));
83+
84+
(whitelist_authorization, in_memory_whitelist)
85+
}
86+
87+
mod when_the_tacker_is_configured_as_listed {
88+
89+
use torrust_tracker_configuration::Core;
6590

66-
mod handling_authorization {
6791
use crate::core_tests::sample_info_hash;
68-
use crate::whitelist::whitelist_tests::initialize_whitelist_services_for_listed_tracker;
92+
use crate::error::Error;
93+
use crate::whitelist::authorization::tests::the_whitelist_authorization_for_announce_and_scrape_actions::{
94+
initialize_whitelist_authorization_and_dependencies_with, initialize_whitelist_authorization_with,
95+
};
96+
97+
fn configuration_for_listed_tracker() -> Core {
98+
Core {
99+
listed: true,
100+
..Default::default()
101+
}
102+
}
69103

70104
#[tokio::test]
71-
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
72-
let (whitelist_authorization, whitelist_manager) = initialize_whitelist_services_for_listed_tracker();
105+
async fn should_authorize_a_whitelisted_infohash() {
106+
let (whitelist_authorization, in_memory_whitelist) =
107+
initialize_whitelist_authorization_and_dependencies_with(&configuration_for_listed_tracker());
73108

74109
let info_hash = sample_info_hash();
75110

76-
let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
77-
assert!(result.is_ok());
111+
let _unused = in_memory_whitelist.add(&info_hash).await;
78112

79113
let result = whitelist_authorization.authorize(&info_hash).await;
114+
80115
assert!(result.is_ok());
81116
}
82117

83118
#[tokio::test]
84-
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
85-
let (whitelist_authorization, _whitelist_manager) = initialize_whitelist_services_for_listed_tracker();
119+
async fn should_not_authorize_a_non_whitelisted_infohash() {
120+
let whitelist_authorization = initialize_whitelist_authorization_with(&configuration_for_listed_tracker());
121+
122+
let result = whitelist_authorization.authorize(&sample_info_hash()).await;
123+
124+
assert!(matches!(result.unwrap_err(), Error::TorrentNotWhitelisted { .. }));
125+
}
126+
}
127+
128+
mod when_the_tacker_is_not_configured_as_listed {
129+
130+
use torrust_tracker_configuration::Core;
131+
132+
use crate::core_tests::sample_info_hash;
133+
use crate::whitelist::authorization::tests::the_whitelist_authorization_for_announce_and_scrape_actions::{
134+
initialize_whitelist_authorization_and_dependencies_with, initialize_whitelist_authorization_with,
135+
};
136+
137+
fn configuration_for_non_listed_tracker() -> Core {
138+
Core {
139+
listed: false,
140+
..Default::default()
141+
}
142+
}
143+
144+
#[tokio::test]
145+
async fn should_authorize_a_whitelisted_infohash() {
146+
let (whitelist_authorization, in_memory_whitelist) =
147+
initialize_whitelist_authorization_and_dependencies_with(&configuration_for_non_listed_tracker());
86148

87149
let info_hash = sample_info_hash();
88150

151+
let _unused = in_memory_whitelist.add(&info_hash).await;
152+
89153
let result = whitelist_authorization.authorize(&info_hash).await;
90-
assert!(result.is_err());
154+
155+
assert!(result.is_ok());
156+
}
157+
158+
#[tokio::test]
159+
async fn should_also_authorize_a_non_whitelisted_infohash() {
160+
let whitelist_authorization = initialize_whitelist_authorization_with(&configuration_for_non_listed_tracker());
161+
162+
let result = whitelist_authorization.authorize(&sample_info_hash()).await;
163+
164+
assert!(result.is_ok());
91165
}
92166
}
93167
}

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

+39-41
Original file line numberDiff line numberDiff line change
@@ -48,30 +48,6 @@ impl WhitelistManager {
4848
Ok(())
4949
}
5050

51-
/// It removes a torrent from the whitelist in the database.
52-
///
53-
/// # Errors
54-
///
55-
/// Will return a `database::Error` if unable to remove the `info_hash` from the whitelist database.
56-
pub fn remove_torrent_from_database_whitelist(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> {
57-
self.database_whitelist.remove(info_hash)
58-
}
59-
60-
/// It adds a torrent from the whitelist in memory.
61-
pub async fn add_torrent_to_memory_whitelist(&self, info_hash: &InfoHash) -> bool {
62-
self.in_memory_whitelist.add(info_hash).await
63-
}
64-
65-
/// It removes a torrent from the whitelist in memory.
66-
pub async fn remove_torrent_from_memory_whitelist(&self, info_hash: &InfoHash) -> bool {
67-
self.in_memory_whitelist.remove(info_hash).await
68-
}
69-
70-
/// It checks if a torrent is whitelisted.
71-
pub async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> bool {
72-
self.in_memory_whitelist.contains(info_hash).await
73-
}
74-
7551
/// It loads the whitelist from the database.
7652
///
7753
/// # Errors
@@ -95,17 +71,41 @@ mod tests {
9571

9672
use std::sync::Arc;
9773

98-
use torrust_tracker_test_helpers::configuration;
74+
use torrust_tracker_configuration::Core;
9975

76+
use crate::core_tests::ephemeral_configuration_for_listed_tracker;
77+
use crate::databases::setup::initialize_database;
78+
use crate::databases::Database;
10079
use crate::whitelist::manager::WhitelistManager;
101-
use crate::whitelist::whitelist_tests::initialize_whitelist_services;
80+
use crate::whitelist::repository::in_memory::InMemoryWhitelist;
81+
use crate::whitelist::repository::persisted::DatabaseWhitelist;
10282

103-
fn initialize_whitelist_manager_for_whitelisted_tracker() -> Arc<WhitelistManager> {
104-
let config = configuration::ephemeral_listed();
83+
struct WhitelistManagerDeps {
84+
pub _database: Arc<Box<dyn Database>>,
85+
pub database_whitelist: Arc<DatabaseWhitelist>,
86+
pub in_memory_whitelist: Arc<InMemoryWhitelist>,
87+
}
10588

106-
let (_whitelist_authorization, whitelist_manager) = initialize_whitelist_services(&config);
89+
fn initialize_whitelist_manager_for_whitelisted_tracker() -> (Arc<WhitelistManager>, Arc<WhitelistManagerDeps>) {
90+
let config = ephemeral_configuration_for_listed_tracker();
91+
initialize_whitelist_manager_and_deps(&config)
92+
}
10793

108-
whitelist_manager
94+
fn initialize_whitelist_manager_and_deps(config: &Core) -> (Arc<WhitelistManager>, Arc<WhitelistManagerDeps>) {
95+
let database = initialize_database(config);
96+
let database_whitelist = Arc::new(DatabaseWhitelist::new(database.clone()));
97+
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
98+
99+
let whitelist_manager = Arc::new(WhitelistManager::new(database_whitelist.clone(), in_memory_whitelist.clone()));
100+
101+
(
102+
whitelist_manager,
103+
Arc::new(WhitelistManagerDeps {
104+
_database: database,
105+
database_whitelist,
106+
in_memory_whitelist,
107+
}),
108+
)
109109
}
110110

111111
mod configured_as_whitelisted {
@@ -116,26 +116,28 @@ mod tests {
116116

117117
#[tokio::test]
118118
async fn it_should_add_a_torrent_to_the_whitelist() {
119-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
119+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
120120

121121
let info_hash = sample_info_hash();
122122

123123
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
124124

125-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
125+
assert!(services.in_memory_whitelist.contains(&info_hash).await);
126+
assert!(services.database_whitelist.load_from_database().unwrap().contains(&info_hash));
126127
}
127128

128129
#[tokio::test]
129130
async fn it_should_remove_a_torrent_from_the_whitelist() {
130-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
131+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
131132

132133
let info_hash = sample_info_hash();
133134

134135
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
135136

136137
whitelist_manager.remove_torrent_from_whitelist(&info_hash).await.unwrap();
137138

138-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
139+
assert!(!services.in_memory_whitelist.contains(&info_hash).await);
140+
assert!(!services.database_whitelist.load_from_database().unwrap().contains(&info_hash));
139141
}
140142

141143
mod persistence {
@@ -144,19 +146,15 @@ mod tests {
144146

145147
#[tokio::test]
146148
async fn it_should_load_the_whitelist_from_the_database() {
147-
let whitelist_manager = initialize_whitelist_manager_for_whitelisted_tracker();
149+
let (whitelist_manager, services) = initialize_whitelist_manager_for_whitelisted_tracker();
148150

149151
let info_hash = sample_info_hash();
150152

151-
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
152-
153-
whitelist_manager.remove_torrent_from_memory_whitelist(&info_hash).await;
154-
155-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
153+
services.database_whitelist.add(&info_hash).unwrap();
156154

157155
whitelist_manager.load_whitelist_from_database().await.unwrap();
158156

159-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
157+
assert!(services.in_memory_whitelist.contains(&info_hash).await);
160158
}
161159
}
162160
}

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

+76
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,79 @@ impl DatabaseWhitelist {
6060
self.database.load_whitelist()
6161
}
6262
}
63+
64+
#[cfg(test)]
65+
mod tests {
66+
mod the_persisted_whitelist_repository {
67+
68+
use crate::core_tests::{ephemeral_configuration_for_listed_tracker, sample_info_hash};
69+
use crate::databases::setup::initialize_database;
70+
use crate::whitelist::repository::persisted::DatabaseWhitelist;
71+
72+
fn initialize_database_whitelist() -> DatabaseWhitelist {
73+
let configuration = ephemeral_configuration_for_listed_tracker();
74+
let database = initialize_database(&configuration);
75+
DatabaseWhitelist::new(database)
76+
}
77+
78+
#[test]
79+
fn should_add_a_new_infohash_to_the_list() {
80+
let whitelist = initialize_database_whitelist();
81+
82+
let infohash = sample_info_hash();
83+
84+
let _result = whitelist.add(&infohash);
85+
86+
assert_eq!(whitelist.load_from_database().unwrap(), vec!(infohash));
87+
}
88+
89+
#[test]
90+
fn should_remove_a_infohash_from_the_list() {
91+
let whitelist = initialize_database_whitelist();
92+
93+
let infohash = sample_info_hash();
94+
95+
let _result = whitelist.add(&infohash);
96+
97+
let _result = whitelist.remove(&infohash);
98+
99+
assert_eq!(whitelist.load_from_database().unwrap(), vec!());
100+
}
101+
102+
#[test]
103+
fn should_load_all_infohashes_from_the_database() {
104+
let whitelist = initialize_database_whitelist();
105+
106+
let infohash = sample_info_hash();
107+
108+
let _result = whitelist.add(&infohash);
109+
110+
let result = whitelist.load_from_database().unwrap();
111+
112+
assert_eq!(result, vec!(infohash));
113+
}
114+
115+
#[test]
116+
fn should_not_add_the_same_infohash_to_the_list_twice() {
117+
let whitelist = initialize_database_whitelist();
118+
119+
let infohash = sample_info_hash();
120+
121+
let _result = whitelist.add(&infohash);
122+
let _result = whitelist.add(&infohash);
123+
124+
assert_eq!(whitelist.load_from_database().unwrap(), vec!(infohash));
125+
}
126+
127+
#[test]
128+
fn should_not_fail_removing_an_infohash_that_is_not_in_the_list() {
129+
let whitelist = initialize_database_whitelist();
130+
131+
let infohash = sample_info_hash();
132+
133+
let result = whitelist.remove(&infohash);
134+
135+
assert!(result.is_ok());
136+
}
137+
}
138+
}

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>>>,

0 commit comments

Comments
 (0)