Skip to content

Commit 6864423

Browse files
committed
Merge #737: Authorization tests for the torrent context
17de729 refactor: moved test to authorization mod (Mario) e3baeda test: new authorization test for guests to download a torrent file searching by info hash (Mario) 6df7a7e refactor: moved test to new authorization mod (Mario) 3c046b1 refactor: move authorization test for guests to new mod (Mario) Pull request description: Part of #718 ACKs for top commit: josecelano: ACK 17de729 Tree-SHA512: c5ac01bb7fbc14a9988e00a48bffe00b191d41874fbbfb8bc79a952216be2ee3c63f6b60819b6cbe19553aff855f6cf7989d6775b89534d0a27f88d1b3c08118
2 parents e953836 + 17de729 commit 6864423

File tree

1 file changed

+81
-47
lines changed

1 file changed

+81
-47
lines changed

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

+81-47
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ mod for_guests {
2323
use crate::common::client::Client;
2424
use crate::common::contexts::category::fixtures::software_predefined_category_id;
2525
use crate::common::contexts::torrent::asserts::assert_expected_torrent_details;
26-
use crate::common::contexts::torrent::fixtures::{random_torrent, TestTorrent};
27-
use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm;
26+
use crate::common::contexts::torrent::fixtures::TestTorrent;
2827
use crate::common::contexts::torrent::requests::InfoHash;
2928
use crate::common::contexts::torrent::responses::{
3029
Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse,
@@ -34,29 +33,6 @@ mod for_guests {
3433
use crate::e2e::web::api::v1::contexts::torrent::steps::{upload_random_torrent_to_index, upload_test_torrent};
3534
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;
3635

37-
#[tokio::test]
38-
async fn it_should_allow_guests_to_get_torrents() {
39-
let mut env = TestEnv::new();
40-
env.start(api::Version::V1).await;
41-
42-
if !env.provides_a_tracker() {
43-
println!("test skipped. It requires a tracker to be running.");
44-
return;
45-
}
46-
47-
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
48-
49-
let uploader = new_logged_in_user(&env).await;
50-
let (_test_torrent, _indexed_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
51-
52-
let response = client.get_torrents(Query::empty()).await;
53-
54-
let torrent_list_response: TorrentListResponse = serde_json::from_str(&response.body).unwrap();
55-
56-
assert!(torrent_list_response.data.total > 0);
57-
assert!(response.is_json_and_ok());
58-
}
59-
6036
#[tokio::test]
6137
async fn it_should_allow_to_get_torrents_with_pagination() {
6238
let mut env = TestEnv::new();
@@ -429,40 +405,98 @@ mod for_guests {
429405
assert_eq!(response.status, 404);
430406
}
431407

432-
#[tokio::test]
433-
async fn it_should_not_allow_guests_to_upload_torrents() {
434-
let mut env = TestEnv::new();
435-
env.start(api::Version::V1).await;
408+
mod authorization {
409+
use torrust_index::web::api;
436410

437-
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
411+
use crate::common::client::Client;
412+
use crate::common::contexts::torrent::fixtures::random_torrent;
413+
use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm;
414+
use crate::common::contexts::torrent::responses::TorrentListResponse;
415+
use crate::common::http::Query;
416+
use crate::e2e::environment::TestEnv;
417+
use crate::e2e::web::api::v1::contexts::torrent::steps::upload_random_torrent_to_index;
418+
use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user;
438419

439-
let test_torrent = random_torrent();
420+
#[tokio::test]
421+
async fn it_should_not_allow_guests_to_upload_torrents() {
422+
let mut env = TestEnv::new();
423+
env.start(api::Version::V1).await;
440424

441-
let form: UploadTorrentMultipartForm = test_torrent.index_info.into();
425+
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
442426

443-
let response = client.upload_torrent(form.into()).await;
427+
let test_torrent = random_torrent();
444428

445-
assert_eq!(response.status, 401);
446-
}
429+
let form: UploadTorrentMultipartForm = test_torrent.index_info.into();
447430

448-
#[tokio::test]
449-
async fn it_should_not_allow_guests_to_delete_torrents() {
450-
let mut env = TestEnv::new();
451-
env.start(api::Version::V1).await;
431+
let response = client.upload_torrent(form.into()).await;
452432

453-
if !env.provides_a_tracker() {
454-
println!("test skipped. It requires a tracker to be running.");
455-
return;
433+
assert_eq!(response.status, 401);
456434
}
457435

458-
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
436+
#[tokio::test]
437+
async fn it_should_not_allow_guests_to_delete_torrents() {
438+
let mut env = TestEnv::new();
439+
env.start(api::Version::V1).await;
459440

460-
let uploader = new_logged_in_user(&env).await;
461-
let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
441+
if !env.provides_a_tracker() {
442+
println!("test skipped. It requires a tracker to be running.");
443+
return;
444+
}
445+
446+
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
447+
448+
let uploader = new_logged_in_user(&env).await;
449+
let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
450+
451+
let response = client.delete_torrent(&test_torrent.file_info_hash()).await;
452+
453+
assert_eq!(response.status, 401);
454+
}
455+
456+
#[tokio::test]
457+
async fn it_should_allow_guests_to_download_a_torrent_file_searching_by_info_hash() {
458+
let mut env = TestEnv::new();
459+
env.start(api::Version::V1).await;
460+
461+
if !env.provides_a_tracker() {
462+
println!("test skipped. It requires a tracker to be running.");
463+
return;
464+
}
465+
466+
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
467+
let uploader = new_logged_in_user(&env).await;
462468

463-
let response = client.delete_torrent(&test_torrent.file_info_hash()).await;
469+
// Upload
470+
let (test_torrent, _torrent_listed_in_index) = upload_random_torrent_to_index(&uploader, &env).await;
471+
472+
// Download
473+
let response = client.download_torrent(&test_torrent.file_info_hash()).await;
474+
475+
assert_eq!(response.status, 200);
476+
}
464477

465-
assert_eq!(response.status, 401);
478+
#[tokio::test]
479+
async fn it_should_allow_guests_to_get_torrents() {
480+
let mut env = TestEnv::new();
481+
env.start(api::Version::V1).await;
482+
483+
if !env.provides_a_tracker() {
484+
println!("test skipped. It requires a tracker to be running.");
485+
return;
486+
}
487+
488+
let client = Client::unauthenticated(&env.server_socket_addr().unwrap());
489+
490+
let uploader = new_logged_in_user(&env).await;
491+
let (_test_torrent, _indexed_torrent) = upload_random_torrent_to_index(&uploader, &env).await;
492+
493+
let response = client.get_torrents(Query::empty()).await;
494+
495+
let torrent_list_response: TorrentListResponse = serde_json::from_str(&response.body).unwrap();
496+
497+
assert!(torrent_list_response.data.total > 0);
498+
assert!(response.is_json_and_ok());
499+
}
466500
}
467501
}
468502

0 commit comments

Comments
 (0)