Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy context authorization tests #764

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
102 changes: 101 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,103 @@
//! 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_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() {
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);
}
#[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);
}
#[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);
}
}
Loading