Skip to content

Commit e2d573b

Browse files
committed
refactor: [torrust#1211] move tests to whitelist module
1 parent c785fd1 commit e2d573b

File tree

5 files changed

+273
-86
lines changed

5 files changed

+273
-86
lines changed

src/core/announce_handler.rs

-2
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ fn assign_ip_address_to_peer(remote_client_ip: &IpAddr, tracker_external_ip: Opt
165165

166166
#[cfg(test)]
167167
mod tests {
168-
// Integration tests for the core module.
169-
170168
mod the_announce_handler {
171169

172170
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

src/core/mod.rs

-84
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,6 @@ pub mod peer_tests;
453453

454454
#[cfg(test)]
455455
mod tests {
456-
// Integration tests for the core module.
457-
458456
mod the_tracker {
459457

460458
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
@@ -626,88 +624,6 @@ mod tests {
626624

627625
mod configured_as_whitelisted {
628626

629-
mod handling_authorization {
630-
use crate::core::tests::the_tracker::{sample_info_hash, whitelisted_tracker};
631-
632-
#[tokio::test]
633-
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
634-
let (_announce_handler, whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
635-
636-
let info_hash = sample_info_hash();
637-
638-
let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
639-
assert!(result.is_ok());
640-
641-
let result = whitelist_authorization.authorize(&info_hash).await;
642-
assert!(result.is_ok());
643-
}
644-
645-
#[tokio::test]
646-
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
647-
let (_announce_handler, whitelist_authorization, _whitelist_manager, _scrape_handler) = whitelisted_tracker();
648-
649-
let info_hash = sample_info_hash();
650-
651-
let result = whitelist_authorization.authorize(&info_hash).await;
652-
assert!(result.is_err());
653-
}
654-
}
655-
656-
mod handling_the_torrent_whitelist {
657-
use crate::core::tests::the_tracker::{sample_info_hash, whitelisted_tracker};
658-
659-
// todo: after extracting the WhitelistManager from the Tracker,
660-
// there is no need to use the tracker to test the whitelist.
661-
// Test not using the `tracker` (`_tracker` variable) should be
662-
// moved to the whitelist module.
663-
664-
#[tokio::test]
665-
async fn it_should_add_a_torrent_to_the_whitelist() {
666-
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
667-
668-
let info_hash = sample_info_hash();
669-
670-
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
671-
672-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
673-
}
674-
675-
#[tokio::test]
676-
async fn it_should_remove_a_torrent_from_the_whitelist() {
677-
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
678-
679-
let info_hash = sample_info_hash();
680-
681-
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
682-
683-
whitelist_manager.remove_torrent_from_whitelist(&info_hash).await.unwrap();
684-
685-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
686-
}
687-
688-
mod persistence {
689-
use crate::core::tests::the_tracker::{sample_info_hash, whitelisted_tracker};
690-
691-
#[tokio::test]
692-
async fn it_should_load_the_whitelist_from_the_database() {
693-
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) =
694-
whitelisted_tracker();
695-
696-
let info_hash = sample_info_hash();
697-
698-
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
699-
700-
whitelist_manager.remove_torrent_from_memory_whitelist(&info_hash).await;
701-
702-
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
703-
704-
whitelist_manager.load_whitelist_from_database().await.unwrap();
705-
706-
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
707-
}
708-
}
709-
}
710-
711627
mod handling_an_scrape_request {
712628

713629
use bittorrent_primitives::info_hash::InfoHash;

src/core/whitelist/authorization.rs

+82
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,85 @@ impl Authorization {
5757
self.in_memory_whitelist.contains(info_hash).await
5858
}
5959
}
60+
61+
#[cfg(test)]
62+
mod tests {
63+
64+
use std::sync::Arc;
65+
66+
use bittorrent_primitives::info_hash::InfoHash;
67+
use torrust_tracker_test_helpers::configuration;
68+
69+
use crate::app_test::initialize_tracker_dependencies;
70+
use crate::core::announce_handler::AnnounceHandler;
71+
use crate::core::scrape_handler::ScrapeHandler;
72+
use crate::core::services::initialize_whitelist_manager;
73+
use crate::core::whitelist;
74+
use crate::core::whitelist::manager::WhiteListManager;
75+
76+
#[allow(clippy::type_complexity)]
77+
fn whitelisted_tracker() -> (
78+
Arc<AnnounceHandler>,
79+
Arc<whitelist::authorization::Authorization>,
80+
Arc<WhiteListManager>,
81+
Arc<ScrapeHandler>,
82+
) {
83+
let config = configuration::ephemeral_listed();
84+
85+
let (
86+
database,
87+
in_memory_whitelist,
88+
whitelist_authorization,
89+
_authentication_service,
90+
in_memory_torrent_repository,
91+
db_torrent_repository,
92+
_torrents_manager,
93+
) = initialize_tracker_dependencies(&config);
94+
95+
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
96+
97+
let announce_handler = Arc::new(AnnounceHandler::new(
98+
&config.core,
99+
&in_memory_torrent_repository,
100+
&db_torrent_repository,
101+
));
102+
103+
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
104+
105+
(announce_handler, whitelist_authorization, whitelist_manager, scrape_handler)
106+
}
107+
108+
fn sample_info_hash() -> InfoHash {
109+
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap()
110+
}
111+
112+
mod configured_as_whitelisted {
113+
114+
mod handling_authorization {
115+
use crate::core::whitelist::authorization::tests::{sample_info_hash, whitelisted_tracker};
116+
117+
#[tokio::test]
118+
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
119+
let (_announce_handler, whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
120+
121+
let info_hash = sample_info_hash();
122+
123+
let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
124+
assert!(result.is_ok());
125+
126+
let result = whitelist_authorization.authorize(&info_hash).await;
127+
assert!(result.is_ok());
128+
}
129+
130+
#[tokio::test]
131+
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
132+
let (_announce_handler, whitelist_authorization, _whitelist_manager, _scrape_handler) = whitelisted_tracker();
133+
134+
let info_hash = sample_info_hash();
135+
136+
let result = whitelist_authorization.authorize(&info_hash).await;
137+
assert!(result.is_err());
138+
}
139+
}
140+
}
141+
}

src/core/whitelist/manager.rs

+109
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,112 @@ impl WhiteListManager {
8989
Ok(())
9090
}
9191
}
92+
93+
#[cfg(test)]
94+
mod tests {
95+
96+
use std::sync::Arc;
97+
98+
use bittorrent_primitives::info_hash::InfoHash;
99+
use torrust_tracker_test_helpers::configuration;
100+
101+
use crate::app_test::initialize_tracker_dependencies;
102+
use crate::core::announce_handler::AnnounceHandler;
103+
use crate::core::scrape_handler::ScrapeHandler;
104+
use crate::core::services::initialize_whitelist_manager;
105+
use crate::core::whitelist;
106+
use crate::core::whitelist::manager::WhiteListManager;
107+
108+
#[allow(clippy::type_complexity)]
109+
fn whitelisted_tracker() -> (
110+
Arc<AnnounceHandler>,
111+
Arc<whitelist::authorization::Authorization>,
112+
Arc<WhiteListManager>,
113+
Arc<ScrapeHandler>,
114+
) {
115+
let config = configuration::ephemeral_listed();
116+
117+
let (
118+
database,
119+
in_memory_whitelist,
120+
whitelist_authorization,
121+
_authentication_service,
122+
in_memory_torrent_repository,
123+
db_torrent_repository,
124+
_torrents_manager,
125+
) = initialize_tracker_dependencies(&config);
126+
127+
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
128+
129+
let announce_handler = Arc::new(AnnounceHandler::new(
130+
&config.core,
131+
&in_memory_torrent_repository,
132+
&db_torrent_repository,
133+
));
134+
135+
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
136+
137+
(announce_handler, whitelist_authorization, whitelist_manager, scrape_handler)
138+
}
139+
140+
fn sample_info_hash() -> InfoHash {
141+
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap()
142+
}
143+
144+
mod configured_as_whitelisted {
145+
146+
mod handling_the_torrent_whitelist {
147+
use crate::core::whitelist::manager::tests::{sample_info_hash, whitelisted_tracker};
148+
149+
// todo: after extracting the WhitelistManager from the Tracker,
150+
// there is no need to use the tracker to test the whitelist.
151+
// Test not using the `tracker` (`_tracker` variable) should be
152+
// moved to the whitelist module.
153+
154+
#[tokio::test]
155+
async fn it_should_add_a_torrent_to_the_whitelist() {
156+
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
157+
158+
let info_hash = sample_info_hash();
159+
160+
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
161+
162+
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
163+
}
164+
165+
#[tokio::test]
166+
async fn it_should_remove_a_torrent_from_the_whitelist() {
167+
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
168+
169+
let info_hash = sample_info_hash();
170+
171+
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
172+
173+
whitelist_manager.remove_torrent_from_whitelist(&info_hash).await.unwrap();
174+
175+
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
176+
}
177+
178+
mod persistence {
179+
use crate::core::whitelist::manager::tests::{sample_info_hash, whitelisted_tracker};
180+
181+
#[tokio::test]
182+
async fn it_should_load_the_whitelist_from_the_database() {
183+
let (_announce_handler, _whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
184+
185+
let info_hash = sample_info_hash();
186+
187+
whitelist_manager.add_torrent_to_whitelist(&info_hash).await.unwrap();
188+
189+
whitelist_manager.remove_torrent_from_memory_whitelist(&info_hash).await;
190+
191+
assert!(!whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
192+
193+
whitelist_manager.load_whitelist_from_database().await.unwrap();
194+
195+
assert!(whitelist_manager.is_info_hash_whitelisted(&info_hash).await);
196+
}
197+
}
198+
}
199+
}
200+
}

src/core/whitelist/mod.rs

+82
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,85 @@
11
pub mod authorization;
22
pub mod manager;
33
pub mod repository;
4+
5+
#[cfg(test)]
6+
mod tests {
7+
8+
use std::sync::Arc;
9+
10+
use bittorrent_primitives::info_hash::InfoHash;
11+
use torrust_tracker_test_helpers::configuration;
12+
13+
use crate::app_test::initialize_tracker_dependencies;
14+
use crate::core::announce_handler::AnnounceHandler;
15+
use crate::core::scrape_handler::ScrapeHandler;
16+
use crate::core::services::initialize_whitelist_manager;
17+
use crate::core::whitelist;
18+
use crate::core::whitelist::manager::WhiteListManager;
19+
20+
#[allow(clippy::type_complexity)]
21+
fn whitelisted_tracker() -> (
22+
Arc<AnnounceHandler>,
23+
Arc<whitelist::authorization::Authorization>,
24+
Arc<WhiteListManager>,
25+
Arc<ScrapeHandler>,
26+
) {
27+
let config = configuration::ephemeral_listed();
28+
29+
let (
30+
database,
31+
in_memory_whitelist,
32+
whitelist_authorization,
33+
_authentication_service,
34+
in_memory_torrent_repository,
35+
db_torrent_repository,
36+
_torrents_manager,
37+
) = initialize_tracker_dependencies(&config);
38+
39+
let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone());
40+
41+
let announce_handler = Arc::new(AnnounceHandler::new(
42+
&config.core,
43+
&in_memory_torrent_repository,
44+
&db_torrent_repository,
45+
));
46+
47+
let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository));
48+
49+
(announce_handler, whitelist_authorization, whitelist_manager, scrape_handler)
50+
}
51+
52+
fn sample_info_hash() -> InfoHash {
53+
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap()
54+
}
55+
56+
mod configured_as_whitelisted {
57+
58+
mod handling_authorization {
59+
use crate::core::whitelist::tests::{sample_info_hash, whitelisted_tracker};
60+
61+
#[tokio::test]
62+
async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() {
63+
let (_announce_handler, whitelist_authorization, whitelist_manager, _scrape_handler) = whitelisted_tracker();
64+
65+
let info_hash = sample_info_hash();
66+
67+
let result = whitelist_manager.add_torrent_to_whitelist(&info_hash).await;
68+
assert!(result.is_ok());
69+
70+
let result = whitelist_authorization.authorize(&info_hash).await;
71+
assert!(result.is_ok());
72+
}
73+
74+
#[tokio::test]
75+
async fn it_should_not_authorize_the_announce_and_scrape_actions_on_not_whitelisted_torrents() {
76+
let (_announce_handler, whitelist_authorization, _whitelist_manager, _scrape_handler) = whitelisted_tracker();
77+
78+
let info_hash = sample_info_hash();
79+
80+
let result = whitelist_authorization.authorize(&info_hash).await;
81+
assert!(result.is_err());
82+
}
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)