@@ -89,3 +89,112 @@ impl WhiteListManager {
89
89
Ok ( ( ) )
90
90
}
91
91
}
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
+ }
0 commit comments