Skip to content

Commit

Permalink
feat(api): [#143] axum api. GET /api/keys/reload endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 11, 2023
1 parent 6b2e3bc commit 03ba166
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ pub async fn delete_auth_key_handler(State(tracker): State<Arc<Tracker>>, Path(s
}
}

pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_keys().await {
Ok(..) => response_ok(),
Err(..) => response_err("failed to reload keys".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
6 changes: 5 additions & 1 deletion src/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use warp::hyper;
use super::middlewares::auth::auth;
use super::routes::{
add_torrent_to_whitelist_handler, delete_auth_key_handler, delete_torrent_from_whitelist_handler, generate_auth_key_handler,
get_stats_handler, get_torrent_handler, get_torrents_handler, reload_whitelist_handler,
get_stats_handler, get_torrent_handler, get_torrents_handler, reload_keys_handler, reload_whitelist_handler,
};
use crate::tracker;

Expand Down Expand Up @@ -46,6 +46,8 @@ pub fn start(socket_addr: SocketAddr, tracker: &Arc<tracker::Tracker>) -> impl F
.delete(delete_auth_key_handler)
.with_state(tracker.clone()),
)
// Key command
.route("/keys/reload", get(reload_keys_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 Down Expand Up @@ -90,6 +92,8 @@ pub fn start_tls(
.delete(delete_auth_key_handler)
.with_state(tracker.clone()),
)
// Key command
.route("/keys/reload", get(reload_keys_handler).with_state(tracker.clone()))
.layer(middleware::from_fn_with_state(tracker.config.clone(), auth));

let handle = Handle::new();
Expand Down
75 changes: 69 additions & 6 deletions tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,8 @@ mod tracker_apis {
use torrust_tracker::tracker::auth::Key;

use crate::api::asserts::{
assert_failed_to_delete_key, assert_failed_to_generate_key, assert_ok, assert_token_not_valid, assert_unauthorized,
assert_failed_to_delete_key, assert_failed_to_generate_key, assert_failed_to_reload_keys, assert_ok,
assert_token_not_valid, assert_unauthorized,
};
use crate::api::client::Client;
use crate::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
Expand Down Expand Up @@ -1209,7 +1210,7 @@ mod tracker_apis {

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

let seconds_valid = 60;
let auth_key = api_server
Expand All @@ -1220,7 +1221,7 @@ mod tracker_apis {

force_database_error(&api_server.tracker);

let response = Client::new(api_server.get_connection_info(), &Version::Warp)
let response = Client::new(api_server.get_connection_info(), &Version::Axum)
.delete_auth_key(&auth_key.key)
.await;

Expand All @@ -1229,7 +1230,7 @@ mod tracker_apis {

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

let seconds_valid = 60;

Expand All @@ -1240,7 +1241,7 @@ mod tracker_apis {
.await
.unwrap();

let response = Client::new(connection_with_invalid_token(&api_server.get_bind_address()), &Version::Warp)
let response = Client::new(connection_with_invalid_token(&api_server.get_bind_address()), &Version::Axum)
.delete_auth_key(&auth_key.key)
.await;

Expand All @@ -1253,11 +1254,73 @@ mod tracker_apis {
.await
.unwrap();

let response = Client::new(connection_with_no_token(&api_server.get_bind_address()), &Version::Warp)
let response = Client::new(connection_with_no_token(&api_server.get_bind_address()), &Version::Axum)
.delete_auth_key(&auth_key.key)
.await;

assert_unauthorized(response).await;
}

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

let seconds_valid = 60;
api_server
.tracker
.generate_auth_key(Duration::from_secs(seconds_valid))
.await
.unwrap();

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

assert_eq!(response.status(), 200);
}

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

let seconds_valid = 60;
api_server
.tracker
.generate_auth_key(Duration::from_secs(seconds_valid))
.await
.unwrap();

force_database_error(&api_server.tracker);

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

assert_failed_to_reload_keys(response).await;
}

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

let seconds_valid = 60;
api_server
.tracker
.generate_auth_key(Duration::from_secs(seconds_valid))
.await
.unwrap();

let response = Client::new(connection_with_invalid_token(&api_server.get_bind_address()), &Version::Axum)
.reload_keys()
.await;

assert_token_not_valid(response).await;

let response = Client::new(connection_with_no_token(&api_server.get_bind_address()), &Version::Axum)
.reload_keys()
.await;

assert_unauthorized(response).await;
}
}
}

0 comments on commit 03ba166

Please sign in to comment.