Skip to content

Commit

Permalink
refactor: [torrust#282] tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Sep 13, 2023
1 parent 6ff3600 commit d7cc040
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 50 deletions.
8 changes: 0 additions & 8 deletions tests/common/contexts/torrent/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,3 @@ pub fn assert_expected_torrent_details(torrent: &TorrentDetails, expected_torren
"torrent `magnet_link` mismatch"
);
}

/// Full assert for test debugging purposes.
pub fn _assert_eq_torrent_details(torrent: &TorrentDetails, expected_torrent: &TorrentDetails) {
assert_eq!(
torrent, expected_torrent,
"\nUnexpected torrent details:\n{torrent:#?} torrent details should match the previously indexed torrent:\n{expected_torrent:#?}"
);
}
6 changes: 3 additions & 3 deletions tests/common/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ impl BinaryResponse {
bytes: response.bytes().await.unwrap().to_vec(),
}
}
pub fn is_bittorrent_and_ok(&self) -> bool {
self.is_ok() && self.is_bittorrent()
pub fn is_a_bit_torrent_file(&self) -> bool {
self.is_ok() && self.is_bittorrent_content_type()
}

pub fn is_bittorrent(&self) -> bool {
pub fn is_bittorrent_content_type(&self) -> bool {
if let Some(content_type) = &self.content_type {
return content_type == "application/x-bittorrent";
}
Expand Down
19 changes: 10 additions & 9 deletions tests/e2e/web/api/v1/contexts/torrent/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ use crate::common::contexts::user::responses::LoggedInUserData;
use crate::e2e::environment::TestEnv;

/// 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 async fn expected_torrent(mut uploaded_torrent: Torrent, env: &TestEnv, downloader: &Option<LoggedInUserData>) -> 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.

///
/// The backend stores the canonical version of the uploaded torrent. So we need
/// to update the expected torrent to match the one generated by the backend.
pub async fn canonical_torrent_for(
mut uploaded_torrent: Torrent,
env: &TestEnv,
downloader: &Option<LoggedInUserData>,
) -> Torrent {
let tracker_url = env.server_settings().unwrap().tracker.url.to_string();

let tracker_key = match downloader {
Expand All @@ -29,6 +28,8 @@ pub async fn expected_torrent(mut uploaded_torrent: Torrent, env: &TestEnv, down

// These fields are not persisted in the database yet.
// See https://github.com/torrust/torrust-index-backend/issues/284
// They are ignore when the user uploads the torrent. So the stored
// canonical torrent does not contain them.
uploaded_torrent.encoding = None;
uploaded_torrent.creation_date = None;
uploaded_torrent.created_by = None;
Expand Down
110 changes: 80 additions & 30 deletions tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Get torrent info:

mod for_guests {

use torrust_index_backend::utils::parse_torrent::{calculate_info_hash, decode_torrent};
use torrust_index_backend::web::api;

use crate::common::client::Client;
Expand All @@ -28,7 +27,6 @@ mod for_guests {
};
use crate::common::http::{Query, QueryParam};
use crate::e2e::environment::TestEnv;
use crate::e2e::web::api::v1::contexts::torrent::asserts::expected_torrent;
use crate::e2e::web::api::v1::contexts::torrent::steps::upload_random_torrent_to_index;
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;

Expand Down Expand Up @@ -215,44 +213,96 @@ mod for_guests {
assert!(response.is_json_and_ok());
}

#[tokio::test]
async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash() {
let mut env = TestEnv::new();
env.start(api::Version::V1).await;
mod it_should_allow_guests_download_a_torrent_file_searching_by_info_hash {

if !env.provides_a_tracker() {
println!("test skipped. It requires a tracker to be running.");
return;
use torrust_index_backend::utils::parse_torrent::{calculate_info_hash, decode_torrent};
use torrust_index_backend::web::api;

use crate::common::client::Client;
use crate::e2e::environment::TestEnv;
use crate::e2e::web::api::v1::contexts::torrent::asserts::canonical_torrent_for;
use crate::e2e::web::api::v1::contexts::torrent::steps::upload_random_torrent_to_index;
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;

#[tokio::test]
async fn returning_a_bittorrent_binary_ok_response() {
let mut env = TestEnv::new();
env.start(api::Version::V1).await;

if !env.provides_a_tracker() {
println!("test skipped. It requires a tracker to be running.");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
let uploader = new_logged_in_user(&env).await;

// Upload
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;

// Download
let response = client.download_torrent(&test_torrent.info_hash_as_hex_string()).await;

assert!(response.is_a_bit_torrent_file());
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
let uploader = new_logged_in_user(&env).await;
#[tokio::test]
async fn the_downloaded_torrent_should_keep_the_same_info_hash_if_the_torrent_does_not_have_non_standard_fields_in_the_info_dict(
) {
let mut env = TestEnv::new();
env.start(api::Version::V1).await;

// Upload a new torrent
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;
if !env.provides_a_tracker() {
println!("test skipped. It requires a tracker to be running.");
return;
}

let uploaded_torrent =
decode_torrent(&test_torrent.index_info.torrent_file.contents).expect("could not decode uploaded torrent");
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
let uploader = new_logged_in_user(&env).await;

// Download the torrent
let response = client.download_torrent(&test_torrent.info_hash_as_hex_string()).await;
// Upload
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;

let downloaded_torrent_info_hash = calculate_info_hash(&response.bytes);
let downloaded_torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent");
// Download
let response = client.download_torrent(&test_torrent.info_hash_as_hex_string()).await;

// Response should be a torrent file and status OK
assert!(response.is_bittorrent_and_ok());
let downloaded_torrent_info_hash = calculate_info_hash(&response.bytes);

// Info-hash should be the same
assert_eq!(
downloaded_torrent_info_hash.to_hex_string(),
test_torrent.info_hash_as_hex_string(),
"downloaded torrent info-hash does not match uploaded torrent info-hash"
);
assert_eq!(
downloaded_torrent_info_hash.to_hex_string(),
test_torrent.info_hash_as_hex_string(),
"downloaded torrent info-hash does not match uploaded torrent info-hash"
);
}

// Torrent should be the same
let expected_torrent = expected_torrent(uploaded_torrent, &env, &None).await;
assert_eq!(downloaded_torrent, expected_torrent);
#[tokio::test]
async fn the_downloaded_torrent_should_be_the_canonical_version_of_the_uploaded_one() {
let mut env = TestEnv::new();
env.start(api::Version::V1).await;

if !env.provides_a_tracker() {
println!("test skipped. It requires a tracker to be running.");
return;
}

let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
let uploader = new_logged_in_user(&env).await;

// Upload
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;

let uploaded_torrent =
decode_torrent(&test_torrent.index_info.torrent_file.contents).expect("could not decode uploaded torrent");

// Download
let response = client.download_torrent(&test_torrent.info_hash_as_hex_string()).await;

let downloaded_torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent");

let expected_downloaded_torrent = canonical_torrent_for(uploaded_torrent, &env, &None).await;

assert_eq!(downloaded_torrent, expected_downloaded_torrent);
}
}

#[tokio::test]
Expand Down

0 comments on commit d7cc040

Please sign in to comment.