Skip to content

Commit

Permalink
test: [torrust#282] compare uploaded and downloaded torrent info-hashes
Browse files Browse the repository at this point in the history
The test for downloading a torrent now compares the uplaoded torrent
info-hash with the downloaded one. It fails becuasse the Index adds the
`private` field in the `info` dictioanry always, changing the info-hash.
  • Loading branch information
josecelano committed Sep 13, 2023
1 parent b73e19e commit b34c7d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tests/common/contexts/torrent/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl TestTorrent {
}
}

pub fn info_hash(&self) -> InfoHash {
pub fn info_hash_as_hex_string(&self) -> InfoHash {
self.file_info.info_hash.clone()
}
}
Expand Down
47 changes: 31 additions & 16 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,7 @@ Get torrent info:

mod for_guests {

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

use crate::common::client::Client;
Expand Down Expand Up @@ -166,7 +166,7 @@ mod for_guests {
let uploader = new_logged_in_user(&env).await;
let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;

let response = client.get_torrent(&test_torrent.info_hash()).await;
let response = client.get_torrent(&test_torrent.info_hash_as_hex_string()).await;

let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap();

Expand Down Expand Up @@ -226,18 +226,33 @@ mod for_guests {
}

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

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

let response = client.download_torrent(&test_torrent.info_hash()).await;
// Upload a new torrent
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;

let torrent = decode_torrent(&response.bytes).expect("could not decode downloaded torrent");
let uploaded_torrent =
decode_torrent(&test_torrent.index_info.torrent_file.contents).expect("could not decode uploaded torrent");
let expected_torrent = expected_torrent(uploaded_torrent, &env, &None).await;
assert_eq!(torrent, expected_torrent);

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

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

// Response should be a torrent file and status OK
assert!(response.is_bittorrent_and_ok());

// 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"
);

// Torrent should be the same
let expected_torrent = expected_torrent(uploaded_torrent, &env, &None).await;
assert_eq!(downloaded_torrent, expected_torrent);
}

#[tokio::test]
Expand Down Expand Up @@ -283,7 +298,7 @@ mod for_guests {
let uploader = new_logged_in_user(&env).await;
let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;

let response = client.delete_torrent(&test_torrent.info_hash()).await;
let response = client.delete_torrent(&test_torrent.info_hash_as_hex_string()).await;

assert_eq!(response.status, 401);
}
Expand Down Expand Up @@ -319,7 +334,7 @@ mod for_authenticated_users {
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);

let test_torrent = random_torrent();
let info_hash = test_torrent.info_hash().clone();
let info_hash = test_torrent.info_hash_as_hex_string().clone();

let form: UploadTorrentMultipartForm = test_torrent.index_info.into();

Expand Down Expand Up @@ -462,7 +477,7 @@ mod for_authenticated_users {
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &downloader.token);

// When the user downloads the torrent
let response = client.download_torrent(&test_torrent.info_hash()).await;
let response = client.download_torrent(&test_torrent.info_hash_as_hex_string()).await;

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

Expand Down Expand Up @@ -504,7 +519,7 @@ mod for_authenticated_users {

let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);

let response = client.delete_torrent(&test_torrent.info_hash()).await;
let response = client.delete_torrent(&test_torrent.info_hash_as_hex_string()).await;

assert_eq!(response.status, 403);
}
Expand Down Expand Up @@ -532,7 +547,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.info_hash(),
&test_torrent.info_hash_as_hex_string(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand Down Expand Up @@ -577,7 +592,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.info_hash(),
&test_torrent.info_hash_as_hex_string(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand Down Expand Up @@ -624,7 +639,7 @@ mod for_authenticated_users {
let admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &admin.token);

let response = client.delete_torrent(&test_torrent.info_hash()).await;
let response = client.delete_torrent(&test_torrent.info_hash_as_hex_string()).await;

let deleted_torrent_response: DeletedTorrentResponse = serde_json::from_str(&response.body).unwrap();

Expand Down Expand Up @@ -653,7 +668,7 @@ mod for_authenticated_users {

let response = client
.update_torrent(
&test_torrent.info_hash(),
&test_torrent.info_hash_as_hex_string(),
UpdateTorrentFrom {
title: Some(new_title.clone()),
description: Some(new_description.clone()),
Expand Down

0 comments on commit b34c7d8

Please sign in to comment.