@@ -16,7 +16,6 @@ Get torrent info:
1616
1717mod for_guests {
1818
19- use torrust_index_backend:: utils:: parse_torrent:: decode_torrent;
2019 use torrust_index_backend:: web:: api;
2120
2221 use crate :: common:: client:: Client ;
@@ -28,7 +27,6 @@ mod for_guests {
2827 } ;
2928 use crate :: common:: http:: { Query , QueryParam } ;
3029 use crate :: e2e:: environment:: TestEnv ;
31- use crate :: e2e:: web:: api:: v1:: contexts:: torrent:: asserts:: expected_torrent;
3230 use crate :: e2e:: web:: api:: v1:: contexts:: torrent:: steps:: upload_random_torrent_to_index;
3331 use crate :: e2e:: web:: api:: v1:: contexts:: user:: steps:: new_logged_in_user;
3432
@@ -166,7 +164,7 @@ mod for_guests {
166164 let uploader = new_logged_in_user ( & env) . await ;
167165 let ( test_torrent, uploaded_torrent) = upload_random_torrent_to_index ( & uploader, & env) . await ;
168166
169- let response = client. get_torrent ( & test_torrent. info_hash ( ) ) . await ;
167+ let response = client. get_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
170168
171169 let torrent_details_response: TorrentDetailsResponse = serde_json:: from_str ( & response. body ) . unwrap ( ) ;
172170
@@ -215,29 +213,96 @@ mod for_guests {
215213 assert ! ( response. is_json_and_ok( ) ) ;
216214 }
217215
218- #[ tokio:: test]
219- async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash ( ) {
220- let mut env = TestEnv :: new ( ) ;
221- env. start ( api:: Version :: V1 ) . await ;
216+ mod it_should_allow_guests_download_a_torrent_file_searching_by_info_hash {
222217
223- if !env. provides_a_tracker ( ) {
224- println ! ( "test skipped. It requires a tracker to be running." ) ;
225- return ;
218+ use torrust_index_backend:: utils:: parse_torrent:: { calculate_info_hash, decode_torrent} ;
219+ use torrust_index_backend:: web:: api;
220+
221+ use crate :: common:: client:: Client ;
222+ use crate :: e2e:: environment:: TestEnv ;
223+ use crate :: e2e:: web:: api:: v1:: contexts:: torrent:: asserts:: canonical_torrent_for;
224+ use crate :: e2e:: web:: api:: v1:: contexts:: torrent:: steps:: upload_random_torrent_to_index;
225+ use crate :: e2e:: web:: api:: v1:: contexts:: user:: steps:: new_logged_in_user;
226+
227+ #[ tokio:: test]
228+ async fn returning_a_bittorrent_binary_ok_response ( ) {
229+ let mut env = TestEnv :: new ( ) ;
230+ env. start ( api:: Version :: V1 ) . await ;
231+
232+ if !env. provides_a_tracker ( ) {
233+ println ! ( "test skipped. It requires a tracker to be running." ) ;
234+ return ;
235+ }
236+
237+ let client = Client :: unauthenticated ( & env. server_socket_addr ( ) . unwrap ( ) ) ;
238+ let uploader = new_logged_in_user ( & env) . await ;
239+
240+ // Upload
241+ let ( test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index ( & uploader, & env) . await ;
242+
243+ // Download
244+ let response = client. download_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
245+
246+ assert ! ( response. is_a_bit_torrent_file( ) ) ;
226247 }
227248
228- let client = Client :: unauthenticated ( & env. server_socket_addr ( ) . unwrap ( ) ) ;
249+ #[ tokio:: test]
250+ async fn the_downloaded_torrent_should_keep_the_same_info_hash_if_the_torrent_does_not_have_non_standard_fields_in_the_info_dict (
251+ ) {
252+ let mut env = TestEnv :: new ( ) ;
253+ env. start ( api:: Version :: V1 ) . await ;
229254
230- let uploader = new_logged_in_user ( & env) . await ;
231- let ( test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index ( & uploader, & env) . await ;
255+ if !env. provides_a_tracker ( ) {
256+ println ! ( "test skipped. It requires a tracker to be running." ) ;
257+ return ;
258+ }
232259
233- let response = client. download_torrent ( & test_torrent. info_hash ( ) ) . await ;
260+ let client = Client :: unauthenticated ( & env. server_socket_addr ( ) . unwrap ( ) ) ;
261+ let uploader = new_logged_in_user ( & env) . await ;
234262
235- let torrent = decode_torrent ( & response. bytes ) . expect ( "could not decode downloaded torrent" ) ;
236- let uploaded_torrent =
237- decode_torrent ( & test_torrent. index_info . torrent_file . contents ) . expect ( "could not decode uploaded torrent" ) ;
238- let expected_torrent = expected_torrent ( uploaded_torrent, & env, & None ) . await ;
239- assert_eq ! ( torrent, expected_torrent) ;
240- assert ! ( response. is_bittorrent_and_ok( ) ) ;
263+ // Upload
264+ let ( test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index ( & uploader, & env) . await ;
265+
266+ // Download
267+ let response = client. download_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
268+
269+ let downloaded_torrent_info_hash = calculate_info_hash ( & response. bytes ) ;
270+
271+ assert_eq ! (
272+ downloaded_torrent_info_hash. to_hex_string( ) ,
273+ test_torrent. info_hash_as_hex_string( ) ,
274+ "downloaded torrent info-hash does not match uploaded torrent info-hash"
275+ ) ;
276+ }
277+
278+ #[ tokio:: test]
279+ async fn the_downloaded_torrent_should_be_the_canonical_version_of_the_uploaded_one ( ) {
280+ let mut env = TestEnv :: new ( ) ;
281+ env. start ( api:: Version :: V1 ) . await ;
282+
283+ if !env. provides_a_tracker ( ) {
284+ println ! ( "test skipped. It requires a tracker to be running." ) ;
285+ return ;
286+ }
287+
288+ let client = Client :: unauthenticated ( & env. server_socket_addr ( ) . unwrap ( ) ) ;
289+ let uploader = new_logged_in_user ( & env) . await ;
290+
291+ // Upload
292+ let ( test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index ( & uploader, & env) . await ;
293+
294+ let uploaded_torrent =
295+ decode_torrent ( & test_torrent. index_info . torrent_file . contents ) . expect ( "could not decode uploaded torrent" ) ;
296+
297+ // Download
298+ let response = client. download_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
299+
300+ let downloaded_torrent = decode_torrent ( & response. bytes ) . expect ( "could not decode downloaded torrent" ) ;
301+
302+ let expected_downloaded_torrent = canonical_torrent_for ( uploaded_torrent, & env, & None ) . await ;
303+
304+ assert_eq ! ( downloaded_torrent, expected_downloaded_torrent) ;
305+ }
241306 }
242307
243308 #[ tokio:: test]
@@ -283,7 +348,7 @@ mod for_guests {
283348 let uploader = new_logged_in_user ( & env) . await ;
284349 let ( test_torrent, _uploaded_torrent) = upload_random_torrent_to_index ( & uploader, & env) . await ;
285350
286- let response = client. delete_torrent ( & test_torrent. info_hash ( ) ) . await ;
351+ let response = client. delete_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
287352
288353 assert_eq ! ( response. status, 401 ) ;
289354 }
@@ -319,7 +384,7 @@ mod for_authenticated_users {
319384 let client = Client :: authenticated ( & env. server_socket_addr ( ) . unwrap ( ) , & uploader. token ) ;
320385
321386 let test_torrent = random_torrent ( ) ;
322- let info_hash = test_torrent. info_hash ( ) . clone ( ) ;
387+ let info_hash = test_torrent. info_hash_as_hex_string ( ) . clone ( ) ;
323388
324389 let form: UploadTorrentMultipartForm = test_torrent. index_info . into ( ) ;
325390
@@ -462,7 +527,7 @@ mod for_authenticated_users {
462527 let client = Client :: authenticated ( & env. server_socket_addr ( ) . unwrap ( ) , & downloader. token ) ;
463528
464529 // When the user downloads the torrent
465- let response = client. download_torrent ( & test_torrent. info_hash ( ) ) . await ;
530+ let response = client. download_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
466531
467532 let torrent = decode_torrent ( & response. bytes ) . expect ( "could not decode downloaded torrent" ) ;
468533
@@ -504,7 +569,7 @@ mod for_authenticated_users {
504569
505570 let client = Client :: authenticated ( & env. server_socket_addr ( ) . unwrap ( ) , & uploader. token ) ;
506571
507- let response = client. delete_torrent ( & test_torrent. info_hash ( ) ) . await ;
572+ let response = client. delete_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
508573
509574 assert_eq ! ( response. status, 403 ) ;
510575 }
@@ -532,7 +597,7 @@ mod for_authenticated_users {
532597
533598 let response = client
534599 . update_torrent (
535- & test_torrent. info_hash ( ) ,
600+ & test_torrent. info_hash_as_hex_string ( ) ,
536601 UpdateTorrentFrom {
537602 title : Some ( new_title. clone ( ) ) ,
538603 description : Some ( new_description. clone ( ) ) ,
@@ -577,7 +642,7 @@ mod for_authenticated_users {
577642
578643 let response = client
579644 . update_torrent (
580- & test_torrent. info_hash ( ) ,
645+ & test_torrent. info_hash_as_hex_string ( ) ,
581646 UpdateTorrentFrom {
582647 title : Some ( new_title. clone ( ) ) ,
583648 description : Some ( new_description. clone ( ) ) ,
@@ -624,7 +689,7 @@ mod for_authenticated_users {
624689 let admin = new_logged_in_admin ( & env) . await ;
625690 let client = Client :: authenticated ( & env. server_socket_addr ( ) . unwrap ( ) , & admin. token ) ;
626691
627- let response = client. delete_torrent ( & test_torrent. info_hash ( ) ) . await ;
692+ let response = client. delete_torrent ( & test_torrent. info_hash_as_hex_string ( ) ) . await ;
628693
629694 let deleted_torrent_response: DeletedTorrentResponse = serde_json:: from_str ( & response. body ) . unwrap ( ) ;
630695
@@ -653,7 +718,7 @@ mod for_authenticated_users {
653718
654719 let response = client
655720 . update_torrent (
656- & test_torrent. info_hash ( ) ,
721+ & test_torrent. info_hash_as_hex_string ( ) ,
657722 UpdateTorrentFrom {
658723 title : Some ( new_title. clone ( ) ) ,
659724 description : Some ( new_description. clone ( ) ) ,
0 commit comments