Skip to content

Commit

Permalink
refactor: [torrust#115] extractfunction expected_torrent
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 5, 2023
1 parent 4782f67 commit e8bf537
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
20 changes: 20 additions & 0 deletions tests/e2e/contexts/torrent/asserts.rs
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 5 additions & 16 deletions tests/e2e/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
1 change: 1 addition & 0 deletions tests/e2e/contexts/torrent/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod asserts;
pub mod contract;
pub mod steps;

0 comments on commit e8bf537

Please sign in to comment.