From 11492a32b666747194153f74e7ecc646d429953a Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 26 Jan 2023 13:12:09 +0000 Subject: [PATCH] test(http): [#159] add tests for announce request in whitelisted mode --- tests/http/asserts.rs | 15 +++++++++++++++ tests/http/server.rs | 12 +++++++++++- tests/http_tracker.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/tests/http/asserts.rs b/tests/http/asserts.rs index b5d84b0a1..60a6a2013 100644 --- a/tests/http/asserts.rs +++ b/tests/http/asserts.rs @@ -91,3 +91,18 @@ pub async fn assert_invalid_peer_id_error_response(response: Response) { }; assert_eq!(error_response, expected_error_response); } + +pub async fn assert_torrent_not_in_whitelist_error_response(response: Response) { + assert_eq!(response.status(), 200); + let body = response.text().await.unwrap(); + let error_response: Error = serde_bencode::from_str(&body).unwrap_or_else(|_| { + panic!( + "response body should be a valid bencoded string for the 'torrent not on whitelist' error, got \"{}\"", + &body + ) + }); + let expected_error_response = Error { + failure_reason: "torrent not on whitelist".to_string(), + }; + assert_eq!(error_response, expected_error_response); +} diff --git a/tests/http/server.rs b/tests/http/server.rs index 506bf75e7..5cd1fec19 100644 --- a/tests/http/server.rs +++ b/tests/http/server.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use torrust_tracker::config::{ephemeral_configuration, Configuration}; use torrust_tracker::jobs::http_tracker; use torrust_tracker::protocol::info_hash::InfoHash; +use torrust_tracker::tracker::mode::Mode; use torrust_tracker::tracker::peer::Peer; use torrust_tracker::tracker::statistics::Keeper; use torrust_tracker::{ephemeral_instance_keys, logging, static_time, tracker}; @@ -13,7 +14,16 @@ use super::connection_info::ConnectionInfo; /// Starts a HTTP tracker with mode "public" pub async fn start_public_http_tracker() -> Server { - start_default_http_tracker().await + let mut configuration = ephemeral_configuration(); + configuration.mode = Mode::Public; + start_custom_http_tracker(Arc::new(configuration)).await +} + +/// Starts a HTTP tracker with mode "listed" +pub async fn start_whitelisted_http_tracker() -> Server { + let mut configuration = ephemeral_configuration(); + configuration.mode = Mode::Listed; + start_custom_http_tracker(Arc::new(configuration)).await } /// Starts a HTTP tracker with a wildcard IPV6 address. diff --git a/tests/http_tracker.rs b/tests/http_tracker.rs index 9cd43a155..05a2dfba1 100644 --- a/tests/http_tracker.rs +++ b/tests/http_tracker.rs @@ -678,7 +678,48 @@ mod http_tracker_server { mod configured_as_whitelisted { - mod and_receiving_an_announce_request {} + mod and_receiving_an_announce_request { + use std::str::FromStr; + + use torrust_tracker::protocol::info_hash::InfoHash; + + use crate::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response}; + use crate::http::client::Client; + use crate::http::requests::AnnounceQueryBuilder; + use crate::http::server::start_whitelisted_http_tracker; + + #[tokio::test] + async fn should_fail_if_the_torrent_is_not_in_the_whitelist() { + let http_tracker_server = start_whitelisted_http_tracker().await; + + let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); + + let response = Client::new(http_tracker_server.get_connection_info()) + .announce(&AnnounceQueryBuilder::default().with_info_hash(&info_hash).query()) + .await; + + assert_torrent_not_in_whitelist_error_response(response).await; + } + + #[tokio::test] + async fn should_allow_announcing_a_whitelisted_torrent() { + let http_tracker_server = start_whitelisted_http_tracker().await; + + let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); + + http_tracker_server + .tracker + .add_torrent_to_whitelist(&info_hash) + .await + .expect("should add the torrent to the whitelist"); + + let response = Client::new(http_tracker_server.get_connection_info()) + .announce(&AnnounceQueryBuilder::default().with_info_hash(&info_hash).query()) + .await; + + assert_is_announce_response(response).await; + } + } mod receiving_an_scrape_request {} }