Skip to content

Commit

Permalink
feat(api): [torrust#143] axum api. DELETE /api/whitelist/:info_hash e…
Browse files Browse the repository at this point in the history
…ndpoint
  • Loading branch information
josecelano committed Jan 11, 2023
1 parent 5c5fcbd commit 2ddf268
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ pub async fn add_torrent_to_whitelist_handler(State(tracker): State<Arc<Tracker>
}
}

/// # Panics
///
/// Will panic if it can't parse the infohash in the request
pub async fn delete_torrent_from_whitelist_handler(
State(tracker): State<Arc<Tracker>>,
Path(info_hash): Path<String>,
) -> Response {
match tracker
.remove_torrent_from_whitelist(&InfoHash::from_str(&info_hash).unwrap())
.await
{
Ok(..) => response_ok(),
Err(..) => response_err("failed to remove torrent from whitelist".to_string()),
}
}

/// Serde deserialization decorator to map empty Strings to None,
fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
Expand Down
15 changes: 13 additions & 2 deletions src/apis/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::SocketAddr;
use std::sync::Arc;

use axum::routing::{get, post};
use axum::routing::{delete, get, post};
use axum::{middleware, Router};
use axum_server::tls_rustls::RustlsConfig;
use axum_server::Handle;
Expand All @@ -10,7 +10,10 @@ use log::info;
use warp::hyper;

use super::middlewares::auth::auth;
use super::routes::{add_torrent_to_whitelist_handler, get_stats_handler, get_torrent_handler, get_torrents_handler};
use super::routes::{
add_torrent_to_whitelist_handler, delete_torrent_from_whitelist_handler, get_stats_handler, get_torrent_handler,
get_torrents_handler,
};
use crate::tracker;

pub fn start(socket_addr: SocketAddr, tracker: &Arc<tracker::Tracker>) -> impl Future<Output = hyper::Result<()>> {
Expand All @@ -25,6 +28,10 @@ pub fn start(socket_addr: SocketAddr, tracker: &Arc<tracker::Tracker>) -> impl F
"/whitelist/:info_hash",
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()),
)
.route(
"/whitelist/:info_hash",
delete(delete_torrent_from_whitelist_handler).with_state(tracker.clone()),
)
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth));

let server = axum::Server::bind(&socket_addr).serve(app.into_make_service());
Expand All @@ -51,6 +58,10 @@ pub fn start_tls(
"/whitelist/:info_hash",
post(add_torrent_to_whitelist_handler).with_state(tracker.clone()),
)
.route(
"/whitelist/:info_hash",
delete(delete_torrent_from_whitelist_handler).with_state(tracker.clone()),
)
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth));

let handle = Handle::new();
Expand Down
60 changes: 59 additions & 1 deletion tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,10 @@ mod tracker_apis {

use torrust_tracker::protocol::info_hash::InfoHash;

use crate::api::asserts::{assert_failed_to_whitelist_torrent, assert_token_not_valid, assert_unauthorized};
use crate::api::asserts::{
assert_failed_to_remove_torrent_from_whitelist, assert_failed_to_whitelist_torrent, assert_token_not_valid,
assert_unauthorized,
};
use crate::api::client::Client;
use crate::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::api::server::start_default_api;
Expand Down Expand Up @@ -1022,5 +1025,60 @@ mod tracker_apis {

assert_failed_to_whitelist_torrent(response).await;
}

#[tokio::test]
async fn should_allow_removing_a_torrent_from_the_whitelist() {
let api_server = start_default_api(&Version::Axum).await;

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
api_server.tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();

let response = Client::new(api_server.get_connection_info(), &Version::Axum)
.remove_torrent_from_whitelist(&hash)
.await;

assert_eq!(response.status(), 200);
assert!(!api_server.tracker.is_info_hash_whitelisted(&info_hash).await);
}

#[tokio::test]
async fn should_return_an_error_when_the_torrent_cannot_be_removed_from_the_whitelist() {
let api_server = start_default_api(&Version::Axum).await;

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();
api_server.tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();

force_database_error(&api_server.tracker);

let response = Client::new(api_server.get_connection_info(), &Version::Axum)
.remove_torrent_from_whitelist(&hash)
.await;

assert_failed_to_remove_torrent_from_whitelist(response).await;
}

#[tokio::test]
async fn should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthenticated_users() {
let api_server = start_default_api(&Version::Axum).await;

let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash = InfoHash::from_str(&hash).unwrap();

api_server.tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();
let response = Client::new(connection_with_invalid_token(&api_server.get_bind_address()), &Version::Axum)
.remove_torrent_from_whitelist(&hash)
.await;

assert_token_not_valid(response).await;

api_server.tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();
let response = Client::new(connection_with_no_token(&api_server.get_bind_address()), &Version::Axum)
.remove_torrent_from_whitelist(&hash)
.await;

assert_unauthorized(response).await;
}
}
}

0 comments on commit 2ddf268

Please sign in to comment.