From e8bf53752bdf346a2fbde95e9f16b523678cbdbe Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Fri, 5 May 2023 13:29:03 +0100 Subject: [PATCH] refactor: [#115] extractfunction expected_torrent --- tests/e2e/contexts/torrent/asserts.rs | 20 ++++++++++++++++++++ tests/e2e/contexts/torrent/contract.rs | 21 +++++---------------- tests/e2e/contexts/torrent/mod.rs | 1 + 3 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 tests/e2e/contexts/torrent/asserts.rs diff --git a/tests/e2e/contexts/torrent/asserts.rs b/tests/e2e/contexts/torrent/asserts.rs new file mode 100644 index 00000000..a7561a97 --- /dev/null +++ b/tests/e2e/contexts/torrent/asserts.rs @@ -0,0 +1,20 @@ +use torrust_index_backend::models::torrent_file::Torrent; + +/// The backend does not generate exactly the same torrent that was uploaded. +/// So we need to update the expected torrent to match the one generated by +/// the backend. +pub fn expected_torrent(mut uploaded_torrent: Torrent) -> Torrent { + // code-review: The backend does not generate exactly the same torrent + // that was uploaded and created by the `imdl` command-line tool. + // So we need to update the expected torrent to match the one generated + // by the backend. For some of them it makes sense (`announce` and `announce_list`), + // for others it does not. + uploaded_torrent.info.private = Some(0); + uploaded_torrent.announce = Some("udp://tracker:6969".to_string()); + uploaded_torrent.encoding = None; + uploaded_torrent.announce_list = Some(vec![vec!["udp://tracker:6969".to_string()]]); + uploaded_torrent.creation_date = None; + uploaded_torrent.created_by = None; + + uploaded_torrent +} diff --git a/tests/e2e/contexts/torrent/contract.rs b/tests/e2e/contexts/torrent/contract.rs index d1cdc0cf..3e102f4e 100644 --- a/tests/e2e/contexts/torrent/contract.rs +++ b/tests/e2e/contexts/torrent/contract.rs @@ -30,6 +30,7 @@ mod for_guests { use crate::common::contexts::torrent::responses::{ Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse, }; + use crate::e2e::contexts::torrent::asserts::expected_torrent; use crate::e2e::contexts::torrent::steps::upload_random_torrent_to_index; use crate::e2e::contexts::user::steps::new_logged_in_user; use crate::e2e::environment::TestEnv; @@ -125,25 +126,13 @@ mod for_guests { let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); let uploader = new_logged_in_user(&env).await; - let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await; + let (test_torrent, torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await; - let response = client.download_torrent(uploaded_torrent.torrent_id).await; + let response = client.download_torrent(torrent_listed_in_index.torrent_id).await; let torrent = decode_torrent(&response.bytes).unwrap(); - let mut expected_torrent = decode_torrent(&test_torrent.index_info.torrent_file.contents).unwrap(); - - // code-review: The backend does not generate exactly the same torrent - // that was uploaded and created by the `imdl` command-line tool. - // So we need to update the expected torrent to match the one generated - // by the backend. For some of them it makes sense (`announce` and `announce_list`), - // for others it does not. - expected_torrent.info.private = Some(0); - expected_torrent.announce = Some("udp://tracker:6969".to_string()); - expected_torrent.encoding = None; - expected_torrent.announce_list = Some(vec![vec!["udp://tracker:6969".to_string()]]); - expected_torrent.creation_date = None; - expected_torrent.created_by = None; - + let uploaded_torrent = decode_torrent(&test_torrent.index_info.torrent_file.contents).unwrap(); + let expected_torrent = expected_torrent(uploaded_torrent); assert_eq!(torrent, expected_torrent); assert!(response.is_bittorrent_and_ok()); } diff --git a/tests/e2e/contexts/torrent/mod.rs b/tests/e2e/contexts/torrent/mod.rs index 2001efb8..c0126d77 100644 --- a/tests/e2e/contexts/torrent/mod.rs +++ b/tests/e2e/contexts/torrent/mod.rs @@ -1,2 +1,3 @@ +pub mod asserts; pub mod contract; pub mod steps;