From 03243cbd132ebe3d1d195b14ad528e7f48fff7d3 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Thu, 26 Dec 2024 12:12:22 +0000 Subject: [PATCH] tests: add assertions for HTTP tracker error logs It's using the info-hash to find the ERROR in logs. IT's generated a newly random info-hash for each tests. It could have been also used a `x-request-id` header in the HTTP request but this solution is simpler and the chances to have an info-hash collision is very low. --- tests/common/fixtures.rs | 10 ++++++++++ tests/servers/http/v1/contract.rs | 28 +++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/tests/common/fixtures.rs b/tests/common/fixtures.rs index bbdebff76..562ed1544 100644 --- a/tests/common/fixtures.rs +++ b/tests/common/fixtures.rs @@ -1,3 +1,5 @@ +use bittorrent_primitives::info_hash::InfoHash; + #[allow(dead_code)] pub fn invalid_info_hashes() -> Vec { [ @@ -10,3 +12,11 @@ pub fn invalid_info_hashes() -> Vec { ] .to_vec() } + +/// Returns a random info hash. +pub fn random_info_hash() -> InfoHash { + let mut rng = rand::thread_rng(); + let random_bytes: [u8; 20] = rand::Rng::gen(&mut rng); + + InfoHash::from_bytes(&random_bytes) +} diff --git a/tests/servers/http/v1/contract.rs b/tests/servers/http/v1/contract.rs index 632f38bf4..83ebb9ae3 100644 --- a/tests/servers/http/v1/contract.rs +++ b/tests/servers/http/v1/contract.rs @@ -1217,8 +1217,10 @@ mod configured_as_whitelisted { use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_test_helpers::configuration; + use uuid::Uuid; - use crate::common::logging::{self}; + use crate::common::fixtures::random_info_hash; + use crate::common::logging::{self, logs_contains_a_line_with}; use crate::servers::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response}; use crate::servers::http::client::Client; use crate::servers::http::requests::announce::QueryBuilder; @@ -1230,14 +1232,24 @@ mod configured_as_whitelisted { let env = Started::new(&configuration::ephemeral_listed().into()).await; - let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); + let request_id = Uuid::new_v4(); + let info_hash = random_info_hash(); let response = Client::new(*env.bind_address()) - .announce(&QueryBuilder::default().with_info_hash(&info_hash).query()) + .announce_with_header( + &QueryBuilder::default().with_info_hash(&info_hash).query(), + "x-request-id", + &request_id.to_string(), + ) .await; assert_torrent_not_in_whitelist_error_response(response).await; + assert!( + logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]), + "Expected logs to contain: ERROR ... {info_hash} is not whitelisted" + ); + env.stop().await; } @@ -1272,7 +1284,8 @@ mod configured_as_whitelisted { use torrust_tracker_primitives::peer::fixture::PeerBuilder; use torrust_tracker_test_helpers::configuration; - use crate::common::logging::{self}; + use crate::common::fixtures::random_info_hash; + use crate::common::logging::{self, logs_contains_a_line_with}; use crate::servers::http::asserts::assert_scrape_response; use crate::servers::http::client::Client; use crate::servers::http::responses::scrape::{File, ResponseBuilder}; @@ -1284,7 +1297,7 @@ mod configured_as_whitelisted { let env = Started::new(&configuration::ephemeral_listed().into()).await; - let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap(); + let info_hash = random_info_hash(); env.add_torrent_peer( &info_hash, @@ -1306,6 +1319,11 @@ mod configured_as_whitelisted { assert_scrape_response(response, &expected_scrape_response).await; + assert!( + logs_contains_a_line_with(&["ERROR", &format!("{info_hash}"), "is not whitelisted"]), + "Expected logs to contain: ERROR ... {info_hash} is not whitelisted" + ); + env.stop().await; }