Skip to content

Commit cc26263

Browse files
committed
wip
1 parent a6d52e1 commit cc26263

File tree

4 files changed

+218
-5
lines changed

4 files changed

+218
-5
lines changed

tests/common/client.rs

+6
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ impl Client {
177177
pub async fn ban_user(&self, username: Username) -> TextResponse {
178178
self.http_client.delete(&format!("/user/ban/{}", &username.value)).await
179179
}
180+
181+
// Context: proxy
182+
183+
/* pub async fn get_image_by_url(&self, url: &str) -> TextResponse {
184+
self.http_client.get(&format!("/proxy/image/{url}"), Query::empty()).await
185+
} */
180186
}
181187

182188
/// Generic HTTP Client
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
11
//! API contract for `proxy` context.
22
3-
// todo
3+
/* mod for_guest_users {
4+
use torrust_index::web::api;
5+
6+
use crate::common::client::Client;
7+
use crate::common::contexts::torrent::fixtures::random_torrent;
8+
use crate::e2e::environment::TestEnv;
9+
use crate::e2e::web::api::v1::contexts::torrent::steps::upload_torrent;
10+
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;
11+
12+
#[tokio::test]
13+
async fn it_should_not_allow_guest_user_to_get_a_proxied_image_by_url() {
14+
let mut env = TestEnv::new();
15+
16+
env.start(api::Version::V1).await;
17+
18+
/* if !env.provides_a_tracker() {
19+
println!("test skipped. It requires a tracker to be running.");
20+
return;
21+
} */
22+
23+
let mut test_torrent = random_torrent();
24+
25+
test_torrent.index_info.description = String::from(
26+
"![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)",
27+
);
28+
29+
let torrent_uploader = new_logged_in_user(&env).await;
30+
31+
upload_torrent(&torrent_uploader, &test_torrent.index_info, &env).await;
32+
33+
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
34+
35+
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";
36+
37+
let response = client.get_image_by_url(url).await;
38+
39+
assert_eq!(response.status, 401);
40+
}
41+
} */

tests/e2e/web/api/v1/contexts/torrent/contract.rs

+142-4
Original file line numberDiff line numberDiff line change
@@ -1337,12 +1337,150 @@ mod for_authenticated_users {
13371337
}
13381338

13391339
#[tokio::test]
1340-
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash() {
1340+
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash_using_a_public_tracker() {
13411341
let mut env = TestEnv::new();
13421342
env.start(api::Version::V1).await;
13431343

1344-
if !env.provides_a_tracker() {
1345-
println!("test skipped. It requires a tracker to be running.");
1344+
if !env.provides_a_public_tracker() {
1345+
println!("test skipped. It requires a public tracker to be running.");
1346+
return;
1347+
}
1348+
1349+
let uploader = new_logged_in_user(&env).await;
1350+
let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
1351+
1352+
let logged_in_admin = new_logged_in_admin(&env).await;
1353+
1354+
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
1355+
1356+
let response = client.get_torrent(&test_torrent.file_info_hash()).await;
1357+
1358+
let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap();
1359+
1360+
let tracker_url = env.server_settings().unwrap().tracker.url.to_string();
1361+
let encoded_tracker_url = urlencoding::encode(&tracker_url);
1362+
1363+
let expected_torrent = TorrentDetails {
1364+
torrent_id: uploaded_torrent.torrent_id,
1365+
uploader: uploader.username,
1366+
info_hash: test_torrent.file_info.info_hash.to_lowercase(),
1367+
title: test_torrent.index_info.title.clone(),
1368+
description: test_torrent.index_info.description,
1369+
category: Category {
1370+
id: software_predefined_category_id(),
1371+
name: test_torrent.index_info.category,
1372+
num_torrents: 19, // Ignored in assertion
1373+
},
1374+
upload_date: "2023-04-27 07:56:08".to_string(), // Ignored in assertion
1375+
file_size: test_torrent.file_info.content_size,
1376+
seeders: 0,
1377+
leechers: 0,
1378+
files: vec![File {
1379+
path: vec![test_torrent.file_info.files[0].clone()],
1380+
// Using one file torrent for testing: content_size = first file size
1381+
length: test_torrent.file_info.content_size,
1382+
md5sum: None, // DevSkim: ignore DS126858
1383+
}],
1384+
trackers: vec![tracker_url.clone().to_string()],
1385+
magnet_link: format!(
1386+
// cspell:disable-next-line
1387+
"magnet:?xt=urn:btih:{}&dn={}&tr={}",
1388+
test_torrent.file_info.info_hash.to_lowercase(),
1389+
urlencoding::encode(&test_torrent.index_info.title),
1390+
encoded_tracker_url
1391+
),
1392+
tags: vec![],
1393+
name: test_torrent.index_info.name.clone(),
1394+
comment: test_torrent.file_info.comment.clone(),
1395+
creation_date: test_torrent.file_info.creation_date,
1396+
created_by: test_torrent.file_info.created_by.clone(),
1397+
encoding: test_torrent.file_info.encoding.clone(),
1398+
canonical_info_hash_group: vec![test_torrent.file_info.info_hash.to_lowercase()],
1399+
};
1400+
1401+
assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);
1402+
assert!(response.is_json_and_ok());
1403+
}
1404+
1405+
/* #[tokio::test]
1406+
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash_using_a_private_tracker() {
1407+
let mut env = TestEnv::new();
1408+
env.start(api::Version::V1).await;
1409+
1410+
if !env.provides_a_private_tracker() {
1411+
println!("test skipped. It requires a private tracker to be running.");
1412+
return;
1413+
}
1414+
1415+
let uploader = new_logged_in_user(&env).await;
1416+
let (test_torrent, uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
1417+
1418+
let logged_in_admin = new_logged_in_admin(&env).await;
1419+
1420+
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token);
1421+
1422+
let response = client.get_torrent(&test_torrent.file_info_hash()).await;
1423+
1424+
let torrent_details_response: TorrentDetailsResponse = serde_json::from_str(&response.body).unwrap();
1425+
1426+
let tracker_url = env.server_settings().unwrap().tracker.url.to_string();
1427+
let encoded_tracker_url = urlencoding::encode(&tracker_url);
1428+
1429+
let expected_torrent_info = client.get_torrent(&test_torrent.file_info.info_hash.to_lowercase()).await;
1430+
1431+
//expected_torrent_info.body.
1432+
1433+
let parsed_response = serde_json::from_str(&expected_torrent_info.body);
1434+
1435+
let expected_torrent = TorrentDetails {
1436+
torrent_id: uploaded_torrent.torrent_id,
1437+
uploader: uploader.username,
1438+
info_hash: test_torrent.file_info.info_hash.to_lowercase(),
1439+
title: test_torrent.index_info.title.clone(),
1440+
description: test_torrent.index_info.description,
1441+
category: Category {
1442+
id: software_predefined_category_id(),
1443+
name: test_torrent.index_info.category,
1444+
num_torrents: 19, // Ignored in assertion
1445+
},
1446+
upload_date: "2023-04-27 07:56:08".to_string(), // Ignored in assertion
1447+
file_size: test_torrent.file_info.content_size,
1448+
seeders: 0,
1449+
leechers: 0,
1450+
files: vec![File {
1451+
path: vec![test_torrent.file_info.files[0].clone()],
1452+
// Using one file torrent for testing: content_size = first file size
1453+
length: test_torrent.file_info.content_size,
1454+
md5sum: None, // DevSkim: ignore DS126858
1455+
}],
1456+
trackers: vec![tracker_url.clone().to_string()],
1457+
magnet_link: format!(
1458+
// cspell:disable-next-line
1459+
"magnet:?xt=urn:btih:{}&dn={}&tr={}",
1460+
test_torrent.file_info.info_hash.to_lowercase(),
1461+
urlencoding::encode(&test_torrent.index_info.title),
1462+
encoded_tracker_url
1463+
),
1464+
tags: vec![],
1465+
name: test_torrent.index_info.name.clone(),
1466+
comment: test_torrent.file_info.comment.clone(),
1467+
creation_date: test_torrent.file_info.creation_date,
1468+
created_by: test_torrent.file_info.created_by.clone(),
1469+
encoding: test_torrent.file_info.encoding.clone(),
1470+
canonical_info_hash_group: vec![test_torrent.file_info.info_hash.to_lowercase()],
1471+
};
1472+
1473+
assert_expected_torrent_details(&torrent_details_response.data, &expected_torrent);
1474+
assert!(response.is_json_and_ok());
1475+
} */
1476+
1477+
#[tokio::test]
1478+
async fn it_should_allow_admin_users_to_get_torrent_details_searching_by_info_hash_using_a_private_tracker() {
1479+
let mut env = TestEnv::new();
1480+
env.start(api::Version::V1).await;
1481+
1482+
if !env.provides_a_private_tracker() {
1483+
println!("test skipped. It requires a private tracker to be running.");
13461484
return;
13471485
}
13481486

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

15851723
if !env.provides_a_private_tracker() {
1586-
println!("test skipped. It requires a public tracker to be running.");
1724+
println!("test skipped. It requires a private tracker to be running.");
15871725
return;
15881726
}
15891727

tests/e2e/web/api/v1/contexts/torrent/steps.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::str::FromStr;
2+
use std::sync::Arc;
23

4+
use torrust_index::databases::database;
35
use torrust_index::models::info_hash::InfoHash;
6+
use torrust_index::services::torrent::Index;
47
use torrust_index::web::api::server::v1::responses::ErrorResponseData;
58

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

5861
Ok(canonical_info_hash)
5962
}
63+
64+
/// Gets tracker announce urls.
65+
///
66+
/// # Errors
67+
///
68+
/// Returns an `ErrorResponseData` if the response is not a 200.
69+
pub async fn get_trackers(env: &TestEnv, user_id: i64, torrent_id: i64) -> () {
70+
let database = Arc::new(
71+
database::connect(&env.database_connect_url().unwrap())
72+
.await
73+
.expect("Database error."),
74+
);
75+
76+
let announce_urls = database.get_torrent_announce_urls_from_id(torrent_id);
77+
78+
let user_tracker_key = database.get_user_tracker_key(user_id);
79+
80+
announce_urls.retain(|tracker| *tracker != tracker_url.to_string());
81+
}
82+
83+
/* /// It adds the tracker URL in the first position of the tracker list.
84+
pub fn include_url_as_main_tracker(&mut self, tracker_url: &Url) {
85+
// Remove any existing instances of tracker_url
86+
self.trackers.retain(|tracker| *tracker != tracker_url.to_string());
87+
88+
// Insert tracker_url at the first position
89+
self.trackers.insert(0, tracker_url.to_owned().to_string());
90+
} */

0 commit comments

Comments
 (0)