Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Sep 25, 2024
1 parent a6d52e1 commit cc26263
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 5 deletions.
6 changes: 6 additions & 0 deletions tests/common/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ impl Client {
pub async fn ban_user(&self, username: Username) -> TextResponse {
self.http_client.delete(&format!("/user/ban/{}", &username.value)).await
}

// Context: proxy

/* pub async fn get_image_by_url(&self, url: &str) -> TextResponse {
self.http_client.get(&format!("/proxy/image/{url}"), Query::empty()).await
} */
}

/// Generic HTTP Client
Expand Down
40 changes: 39 additions & 1 deletion tests/e2e/web/api/v1/contexts/proxy/contract.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
//! API contract for `proxy` context.

// todo
/* mod for_guest_users {
use torrust_index::web::api;
use crate::common::client::Client;
use crate::common::contexts::torrent::fixtures::random_torrent;
use crate::e2e::environment::TestEnv;
use crate::e2e::web::api::v1::contexts::torrent::steps::upload_torrent;
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;
#[tokio::test]
async fn it_should_not_allow_guest_user_to_get_a_proxied_image_by_url() {
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 mut test_torrent = random_torrent();
test_torrent.index_info.description = String::from(
"![image info](https://upload.wikimedia.org/wikipedia/commons/thumb/7/71/Zaadpluizen_van_een_Clematis_texensis_%27Princess_Diana%27._18-07-2023_%28actm.%29_02.jpg/1024px-Zaadpluizen_van_een_Clematis_texensis_%27Princess_Diana%27._18-07-2023_%28actm.%29_02.jpg)",
);
let torrent_uploader = new_logged_in_user(&env).await;
upload_torrent(&torrent_uploader, &test_torrent.index_info, &env).await;
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
let url = "https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F7%2F71%2FZaadpluizen_van_een_Clematis_texensis_%2527Princess_Diana%2527._18-07-2023_%2528actm.%2529_02.jpg%2F1024px-Zaadpluizen_van_een_Clematis_texensis_%2527Princess_Diana%2527._18-07-2023_%2528actm.%2529_02.jpg";
let response = client.get_image_by_url(url).await;
assert_eq!(response.status, 401);
}
} */
146 changes: 142 additions & 4 deletions tests/e2e/web/api/v1/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,12 +1337,150 @@ mod for_authenticated_users {
}

#[tokio::test]
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash() {
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash_using_a_public_tracker() {
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.");
if !env.provides_a_public_tracker() {
println!("test skipped. It requires a public tracker to be running.");
return;
}

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

let logged_in_admin = new_logged_in_admin(&env).await;

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

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

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

let tracker_url = env.server_settings().unwrap().tracker.url.to_string();
let encoded_tracker_url = urlencoding::encode(&tracker_url);

let expected_torrent = TorrentDetails {
torrent_id: uploaded_torrent.torrent_id,
uploader: uploader.username,
info_hash: test_torrent.file_info.info_hash.to_lowercase(),
title: test_torrent.index_info.title.clone(),
description: test_torrent.index_info.description,
category: Category {
id: software_predefined_category_id(),
name: test_torrent.index_info.category,
num_torrents: 19, // Ignored in assertion
},
upload_date: "2023-04-27 07:56:08".to_string(), // Ignored in assertion
file_size: test_torrent.file_info.content_size,
seeders: 0,
leechers: 0,
files: vec![File {
path: vec![test_torrent.file_info.files[0].clone()],
// Using one file torrent for testing: content_size = first file size
length: test_torrent.file_info.content_size,
md5sum: None, // DevSkim: ignore DS126858
}],
trackers: vec![tracker_url.clone().to_string()],
magnet_link: format!(
// cspell:disable-next-line
"magnet:?xt=urn:btih:{}&dn={}&tr={}",
test_torrent.file_info.info_hash.to_lowercase(),
urlencoding::encode(&test_torrent.index_info.title),
encoded_tracker_url
),
tags: vec![],
name: test_torrent.index_info.name.clone(),
comment: test_torrent.file_info.comment.clone(),
creation_date: test_torrent.file_info.creation_date,
created_by: test_torrent.file_info.created_by.clone(),
encoding: test_torrent.file_info.encoding.clone(),
canonical_info_hash_group: vec![test_torrent.file_info.info_hash.to_lowercase()],
};

assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);
assert!(response.is_json_and_ok());
}

/* #[tokio::test]
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash_using_a_private_tracker() {
let mut env = TestEnv::new();
env.start(api::Version::V1).await;
if !env.provides_a_private_tracker() {
println!("test skipped. It requires a private tracker to be running.");
return;
}
let uploader = new_logged_in_user(&env).await;
let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
let logged_in_admin = new_logged_in_admin(&env).await;
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
let response = client.get_torrent(&test_torrent.file_info_hash()).await;
let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap();
let tracker_url = env.server_settings().unwrap().tracker.url.to_string();
let encoded_tracker_url = urlencoding::encode(&tracker_url);
let expected_torrent_info = client.get_torrent(&test_torrent.file_info.info_hash.to_lowercase()).await;
//expected_torrent_info.body.
let parsed_response = serde_json::from_str(&expected_torrent_info.body);
let expected_torrent = TorrentDetails {
torrent_id: uploaded_torrent.torrent_id,
uploader: uploader.username,
info_hash: test_torrent.file_info.info_hash.to_lowercase(),
title: test_torrent.index_info.title.clone(),
description: test_torrent.index_info.description,
category: Category {
id: software_predefined_category_id(),
name: test_torrent.index_info.category,
num_torrents: 19, // Ignored in assertion
},
upload_date: "2023-04-27 07:56:08".to_string(), // Ignored in assertion
file_size: test_torrent.file_info.content_size,
seeders: 0,
leechers: 0,
files: vec![File {
path: vec![test_torrent.file_info.files[0].clone()],
// Using one file torrent for testing: content_size = first file size
length: test_torrent.file_info.content_size,
md5sum: None, // DevSkim: ignore DS126858
}],
trackers: vec![tracker_url.clone().to_string()],
magnet_link: format!(
// cspell:disable-next-line
"magnet:?xt=urn:btih:{}&dn={}&tr={}",
test_torrent.file_info.info_hash.to_lowercase(),
urlencoding::encode(&test_torrent.index_info.title),
encoded_tracker_url
),
tags: vec![],
name: test_torrent.index_info.name.clone(),
comment: test_torrent.file_info.comment.clone(),
creation_date: test_torrent.file_info.creation_date,
created_by: test_torrent.file_info.created_by.clone(),
encoding: test_torrent.file_info.encoding.clone(),
canonical_info_hash_group: vec![test_torrent.file_info.info_hash.to_lowercase()],
};
assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);
assert!(response.is_json_and_ok());
} */

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

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

Expand Down Expand Up @@ -1583,7 +1721,7 @@ mod for_authenticated_users {
env.start(api::Version::V1).await;

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

Expand Down
31 changes: 31 additions & 0 deletions tests/e2e/web/api/v1/contexts/torrent/steps.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::str::FromStr;
use std::sync::Arc;

use torrust_index::databases::database;
use torrust_index::models::info_hash::InfoHash;
use torrust_index::services::torrent::Index;
use torrust_index::web::api::server::v1::responses::ErrorResponseData;

use crate::common::client::Client;
Expand Down Expand Up @@ -57,3 +60,31 @@ pub async fn upload_test_torrent(client: &Client, test_torrent: &TestTorrent) ->

Ok(canonical_info_hash)
}

/// Gets tracker announce urls.
///
/// # Errors
///
/// Returns an `ErrorResponseData` if the response is not a 200.
pub async fn get_trackers(env: &TestEnv, user_id: i64, torrent_id: i64) -> () {
let database = Arc::new(
database::connect(&env.database_connect_url().unwrap())
.await
.expect("Database error."),
);

let announce_urls = database.get_torrent_announce_urls_from_id(torrent_id);

let user_tracker_key = database.get_user_tracker_key(user_id);

announce_urls.retain(|tracker| *tracker != tracker_url.to_string());
}

/* /// It adds the tracker URL in the first position of the tracker list.
pub fn include_url_as_main_tracker(&mut self, tracker_url: &Url) {
// Remove any existing instances of tracker_url
self.trackers.retain(|tracker| *tracker != tracker_url.to_string());
// Insert tracker_url at the first position
self.trackers.insert(0, tracker_url.to_owned().to_string());
} */

0 comments on commit cc26263

Please sign in to comment.