Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log assertions: add assertions for log errors in HTTP tracker #1163

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/common/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use bittorrent_primitives::info_hash::InfoHash;

#[allow(dead_code)]
pub fn invalid_info_hashes() -> Vec<String> {
[
Expand All @@ -10,3 +12,11 @@ pub fn invalid_info_hashes() -> Vec<String> {
]
.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)
}
28 changes: 23 additions & 5 deletions tests/servers/http/v1/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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};
Expand All @@ -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,
Expand All @@ -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;
}

Expand Down
Loading