From 26451f9361e8426f7ef407cf46864d43a0a83a33 Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 6 Nov 2024 16:17:46 +0100 Subject: [PATCH 1/5] test: new method for tests http client --- tests/common/client.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/common/client.rs b/tests/common/client.rs index c9c22d2e..1351493a 100644 --- a/tests/common/client.rs +++ b/tests/common/client.rs @@ -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 From e94e9cd8b1f4854eab4e2aec06231995c363997b Mon Sep 17 00:00:00 2001 From: Mario Date: Wed, 6 Nov 2024 16:28:55 +0100 Subject: [PATCH 2/5] test: authorization test for guest users --- .../e2e/web/api/v1/contexts/proxy/contract.rs | 40 ++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/tests/e2e/web/api/v1/contexts/proxy/contract.rs b/tests/e2e/web/api/v1/contexts/proxy/contract.rs index 0b63dfc4..bee591a6 100644 --- a/tests/e2e/web/api/v1/contexts/proxy/contract.rs +++ b/tests/e2e/web/api/v1/contexts/proxy/contract.rs @@ -1,3 +1,41 @@ //! API contract for `proxy` context. -// todo +mod authorization { + 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_return_an_ok_response_when_a_guest_user_tries_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, 200); + } +} From a6b2f7aa12614bddc9866d4d40d6579f0a6b4c76 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Nov 2024 10:31:27 +0100 Subject: [PATCH 3/5] test: authorization tests for registered users --- .../e2e/web/api/v1/contexts/proxy/contract.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/e2e/web/api/v1/contexts/proxy/contract.rs b/tests/e2e/web/api/v1/contexts/proxy/contract.rs index bee591a6..979c2e5d 100644 --- a/tests/e2e/web/api/v1/contexts/proxy/contract.rs +++ b/tests/e2e/web/api/v1/contexts/proxy/contract.rs @@ -36,6 +36,37 @@ mod authorization { let response = client.get_image_by_url(url).await; + assert_eq!(response.status, 200); + } + #[tokio::test] + async fn it_should_allow_registered_users_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 logged_non_admin = new_logged_in_user(&env).await; + + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_non_admin.token); + + 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, 200); } } From 29db82f7f7094008c17801820ac1f91bc1930b87 Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Nov 2024 10:44:27 +0100 Subject: [PATCH 4/5] test: authorization tests for admin users --- .../e2e/web/api/v1/contexts/proxy/contract.rs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/e2e/web/api/v1/contexts/proxy/contract.rs b/tests/e2e/web/api/v1/contexts/proxy/contract.rs index 979c2e5d..d0f153c1 100644 --- a/tests/e2e/web/api/v1/contexts/proxy/contract.rs +++ b/tests/e2e/web/api/v1/contexts/proxy/contract.rs @@ -67,6 +67,37 @@ mod authorization { let response = client.get_image_by_url(url).await; + assert_eq!(response.status, 200); + } + #[tokio::test] + async fn it_should_allow_admin_users_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 logged_in_admin = new_logged_in_admin(&env).await; + + let client = Client::authenticated(&env.server_socket_addr().unwrap(), &logged_in_admin.token); + + 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, 200); } } From 329a451f8b16752387a6c2bf669aab8bf8335fba Mon Sep 17 00:00:00 2001 From: Mario Date: Thu, 7 Nov 2024 12:46:31 +0100 Subject: [PATCH 5/5] refactor: added missing import --- tests/e2e/web/api/v1/contexts/proxy/contract.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/web/api/v1/contexts/proxy/contract.rs b/tests/e2e/web/api/v1/contexts/proxy/contract.rs index d0f153c1..b2de1433 100644 --- a/tests/e2e/web/api/v1/contexts/proxy/contract.rs +++ b/tests/e2e/web/api/v1/contexts/proxy/contract.rs @@ -7,7 +7,7 @@ mod authorization { 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; + use crate::e2e::web::api::v1::contexts::user::steps::{new_logged_in_admin, new_logged_in_user}; #[tokio::test] async fn it_should_return_an_ok_response_when_a_guest_user_tries_to_get_a_proxied_image_by_url() {