Skip to content

Commit

Permalink
feat(api): [#143] axum api. GET /api/torrent/:info_hash endpoint. Not…
Browse files Browse the repository at this point in the history
… found case
  • Loading branch information
josecelano committed Jan 9, 2023
1 parent 2aebf9a commit a649fe8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"hlocalhost",
"Hydranode",
"incompletei",
"infohash",
"infoschema",
"intervali",
"leecher",
Expand Down
18 changes: 10 additions & 8 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::str::FromStr;
use std::sync::Arc;

use axum::extract::{Path, State};
use axum::response::Json;
use axum::response::{IntoResponse, Json, Response};
use serde_json::json;

use crate::api::resource::stats::Stats;
use crate::api::resource::torrent::Torrent;
Expand All @@ -17,11 +18,12 @@ pub async fn get_stats(State(tracker): State<Arc<Tracker>>) -> Json<Stats> {

/// # Panics
///
/// Will panic if the torrent does not exist.
pub async fn get_torrent(State(tracker): State<Arc<Tracker>>, Path(info_hash): Path<String>) -> Json<Torrent> {
let info = get_torrent_info(tracker.clone(), &InfoHash::from_str(&info_hash).unwrap())
.await
.unwrap();
// todo: return "not found" if the torrent does not exist
Json(Torrent::from(info))
/// Will panic if it can't parse the infohash in the request
pub async fn get_torrent(State(tracker): State<Arc<Tracker>>, Path(info_hash): Path<String>) -> Response {
let optional_torrent_info = get_torrent_info(tracker.clone(), &InfoHash::from_str(&info_hash).unwrap()).await;

match optional_torrent_info {
Some(info) => Json(Torrent::from(info)).into_response(),
None => Json(json!("torrent not known")).into_response(),
}
}
15 changes: 14 additions & 1 deletion tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ mod tracker_apis {
use torrust_tracker::api::resource::torrent::Torrent;
use torrust_tracker::protocol::info_hash::InfoHash;

use crate::api::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::api::asserts::{assert_token_not_valid, assert_torrent_not_known, assert_unauthorized};
use crate::api::client::Client;
use crate::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::api::fixtures::sample_peer;
Expand Down Expand Up @@ -700,6 +700,19 @@ mod tracker_apis {
);
}

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

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

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

assert_torrent_not_known(response).await;
}

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

0 comments on commit a649fe8

Please sign in to comment.