Skip to content

Commit

Permalink
test(http): [#159] add tests for announce request in whitelisted mode
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 26, 2023
1 parent 86155d6 commit 11492a3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
15 changes: 15 additions & 0 deletions tests/http/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
12 changes: 11 additions & 1 deletion tests/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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.
Expand Down
43 changes: 42 additions & 1 deletion tests/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down

0 comments on commit 11492a3

Please sign in to comment.