From 3c046b10b26d5407b98ba21bf802a41130fb2013 Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 25 Sep 2024 23:44:06 +0200 Subject: [PATCH 1/4] refactor: move authorization test for guests to new mod --- .../web/api/v1/contexts/torrent/contract.rs | 44 +++++++++++-------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index ed3b4f33..ed48c05a 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -23,8 +23,7 @@ mod for_guests { use crate::common::client::Client; use crate::common::contexts::category::fixtures::software_predefined_category_id; use crate::common::contexts::torrent::asserts::assert_expected_torrent_details; - use crate::common::contexts::torrent::fixtures::{random_torrent, TestTorrent}; - use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; + use crate::common::contexts::torrent::fixtures::TestTorrent; use crate::common::contexts::torrent::requests::InfoHash; use crate::common::contexts::torrent::responses::{ Category, File, TorrentDetails, TorrentDetailsResponse, TorrentListResponse, @@ -429,22 +428,6 @@ mod for_guests { assert_eq!(response.status, 404); } - #[tokio::test] - async fn it_should_not_allow_guests_to_upload_torrents() { - let mut env = TestEnv::new(); - env.start(api::Version::V1).await; - - let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); - - let test_torrent = random_torrent(); - - let form: UploadTorrentMultipartForm = test_torrent.index_info.into(); - - let response = client.upload_torrent(form.into()).await; - - assert_eq!(response.status, 401); - } - #[tokio::test] async fn it_should_not_allow_guests_to_delete_torrents() { let mut env = TestEnv::new(); @@ -464,6 +447,31 @@ mod for_guests { assert_eq!(response.status, 401); } + + mod authorization { + use torrust_index::web::api; + + use crate::common::client::Client; + use crate::common::contexts::torrent::fixtures::random_torrent; + use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; + use crate::e2e::environment::TestEnv; + + #[tokio::test] + async fn it_should_not_allow_guests_to_upload_torrents() { + let mut env = TestEnv::new(); + env.start(api::Version::V1).await; + + let client = Client::unauthenticated(&env.server_socket_addr().unwrap()); + + let test_torrent = random_torrent(); + + let form: UploadTorrentMultipartForm = test_torrent.index_info.into(); + + let response = client.upload_torrent(form.into()).await; + + assert_eq!(response.status, 401); + } + } } mod for_authenticated_users { From 6df7a7e683ae642cfe6ecb1ad5c39b39f6597825 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 26 Sep 2024 13:12:24 +0200 Subject: [PATCH 2/4] refactor: moved test to new authorization mod --- .../web/api/v1/contexts/torrent/contract.rs | 42 ++++++++++--------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index ed48c05a..b3064b20 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -428,26 +428,6 @@ mod for_guests { assert_eq!(response.status, 404); } - #[tokio::test] - async fn it_should_not_allow_guests_to_delete_torrents() { - 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; - let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await; - - let response = client.delete_torrent(&test_torrent.file_info_hash()).await; - - assert_eq!(response.status, 401); - } - mod authorization { use torrust_index::web::api; @@ -455,6 +435,8 @@ mod for_guests { use crate::common::contexts::torrent::fixtures::random_torrent; use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; use crate::e2e::environment::TestEnv; + 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 it_should_not_allow_guests_to_upload_torrents() { @@ -471,6 +453,26 @@ mod for_guests { assert_eq!(response.status, 401); } + + #[tokio::test] + async fn it_should_not_allow_guests_to_delete_torrents() { + 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; + let (test_torrent, _uploaded_torrent) = upload_random_torrent_to_index(&uploader, &env).await; + + let response = client.delete_torrent(&test_torrent.file_info_hash()).await; + + assert_eq!(response.status, 401); + } } } From e3baeda82fbcc94958484646b73801c4397cfc0f Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 27 Sep 2024 12:36:37 +0200 Subject: [PATCH 3/4] test: new authorization test for guests to download a torrent file searching by info hash --- .../web/api/v1/contexts/torrent/contract.rs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index b3064b20..c8082e36 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -473,6 +473,28 @@ mod for_guests { assert_eq!(response.status, 401); } + + #[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; + + 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.file_info_hash()).await; + + assert_eq!(response.status, 200); + } } } From 17de7297412de225b3525396ee215f50dcda9a57 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 27 Sep 2024 17:22:32 +0200 Subject: [PATCH 4/4] refactor: moved test to authorization mod --- .../web/api/v1/contexts/torrent/contract.rs | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/tests/e2e/web/api/v1/contexts/torrent/contract.rs b/tests/e2e/web/api/v1/contexts/torrent/contract.rs index c8082e36..909ec230 100644 --- a/tests/e2e/web/api/v1/contexts/torrent/contract.rs +++ b/tests/e2e/web/api/v1/contexts/torrent/contract.rs @@ -33,29 +33,6 @@ mod for_guests { use crate::e2e::web::api::v1::contexts::torrent::steps::{upload_random_torrent_to_index, upload_test_torrent}; use crate::e2e::web::api::v1::contexts::user::steps::new_logged_in_user; - #[tokio::test] - async fn it_should_allow_guests_to_get_torrents() { - 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; - let (_test_torrent, _indexed_torrent) = upload_random_torrent_to_index(&uploader, &env).await; - - let response = client.get_torrents(Query::empty()).await; - - let torrent_list_response: TorrentListResponse = serde_json::from_str(&response.body).unwrap(); - - assert!(torrent_list_response.data.total > 0); - assert!(response.is_json_and_ok()); - } - #[tokio::test] async fn it_should_allow_to_get_torrents_with_pagination() { let mut env = TestEnv::new(); @@ -434,6 +411,8 @@ mod for_guests { use crate::common::client::Client; use crate::common::contexts::torrent::fixtures::random_torrent; use crate::common::contexts::torrent::forms::UploadTorrentMultipartForm; + use crate::common::contexts::torrent::responses::TorrentListResponse; + use crate::common::http::Query; use crate::e2e::environment::TestEnv; 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; @@ -495,6 +474,29 @@ mod for_guests { assert_eq!(response.status, 200); } + + #[tokio::test] + async fn it_should_allow_guests_to_get_torrents() { + 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; + let (_test_torrent, _indexed_torrent) = upload_random_torrent_to_index(&uploader, &env).await; + + let response = client.get_torrents(Query::empty()).await; + + let torrent_list_response: TorrentListResponse = serde_json::from_str(&response.body).unwrap(); + + assert!(torrent_list_response.data.total > 0); + assert!(response.is_json_and_ok()); + } } }