Skip to content

Commit ebc0971

Browse files
committed
test: write assertions for error logs in the API
These tests produce expected errors in the logs: ``` servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_empty servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_invalid servers::api::v1::contract::authentication::should_not_authenticate_requests_when_the_token_is_missing servers::api::v1::contract::context::auth_key::deprecated_generate_key_endpoint::should_fail_when_the_auth_key_cannot_be_generated servers::api::v1::contract::context::auth_key::deprecated_generate_key_endpoint::should_not_allow_generating_a_new_auth_key_for_unauthenticated_users servers::api::v1::contract::context::auth_key::should_fail_when_keys_cannot_be_reloaded servers::api::v1::contract::context::auth_key::should_fail_when_the_auth_key_cannot_be_deleted servers::api::v1::contract::context::auth_key::should_fail_when_the_auth_key_cannot_be_generated servers::api::v1::contract::context::auth_key::should_not_allow_deleting_an_auth_key_for_unauthenticated_users servers::api::v1::contract::context::auth_key::should_not_allow_generating_a_new_auth_key_for_unauthenticated_users servers::api::v1::contract::context::auth_key::should_not_allow_reloading_keys_for_unauthenticated_users servers::api::v1::contract::context::stats::should_not_allow_getting_tracker_statistics_for_unauthenticated_users servers::api::v1::contract::context::torrent::should_not_allow_getting_a_torrent_info_for_unauthenticated_users servers::api::v1::contract::context::torrent::should_not_allow_getting_torrents_for_unauthenticated_users servers::api::v1::contract::context::whitelist::should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist servers::api::v1::contract::context::whitelist::should_fail_when_the_whitelist_cannot_be_reloaded_from_the_database servers::api::v1::contract::context::whitelist::should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthenticated_users ``` This change adds assertions for triggered errors in logs.
1 parent b024575 commit ebc0971

File tree

6 files changed

+92
-2
lines changed

6 files changed

+92
-2
lines changed

tests/servers/api/v1/contract/authentication.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use torrust_tracker_test_helpers::configuration;
2+
use tracing::level_filters::LevelFilter;
23

34
use crate::common::http::{Query, QueryParam};
5+
use crate::common::logging::{self, logs_contains_a_line_with};
46
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
57
use crate::servers::api::v1::client::Client;
68
use crate::servers::api::Started;
@@ -22,6 +24,8 @@ async fn should_authenticate_requests_by_using_a_token_query_param() {
2224

2325
#[tokio::test]
2426
async fn should_not_authenticate_requests_when_the_token_is_missing() {
27+
logging::setup(LevelFilter::ERROR);
28+
2529
let env = Started::new(&configuration::ephemeral().into()).await;
2630

2731
let response = Client::new(env.get_connection_info())
@@ -31,10 +35,14 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {
3135
assert_unauthorized(response).await;
3236

3337
env.stop().await;
38+
39+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
3440
}
3541

3642
#[tokio::test]
3743
async fn should_not_authenticate_requests_when_the_token_is_empty() {
44+
logging::setup(LevelFilter::ERROR);
45+
3846
let env = Started::new(&configuration::ephemeral().into()).await;
3947

4048
let response = Client::new(env.get_connection_info())
@@ -44,10 +52,14 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {
4452
assert_token_not_valid(response).await;
4553

4654
env.stop().await;
55+
56+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
4757
}
4858

4959
#[tokio::test]
5060
async fn should_not_authenticate_requests_when_the_token_is_invalid() {
61+
logging::setup(LevelFilter::ERROR);
62+
5163
let env = Started::new(&configuration::ephemeral().into()).await;
5264

5365
let response = Client::new(env.get_connection_info())
@@ -57,6 +69,8 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {
5769
assert_token_not_valid(response).await;
5870

5971
env.stop().await;
72+
73+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
6074
}
6175

6276
#[tokio::test]

tests/servers/api/v1/contract/context/auth_key.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ async fn should_allow_uploading_a_preexisting_auth_key() {
6161

6262
#[tokio::test]
6363
async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users() {
64+
logging::setup(LevelFilter::ERROR);
65+
6466
let env = Started::new(&configuration::ephemeral().into()).await;
6567

6668
let response = Client::new(connection_with_invalid_token(env.get_connection_info().bind_address.as_str()))
@@ -82,10 +84,14 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
8284
assert_unauthorized(response).await;
8385

8486
env.stop().await;
87+
88+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
8589
}
8690

8791
#[tokio::test]
8892
async fn should_fail_when_the_auth_key_cannot_be_generated() {
93+
logging::setup(LevelFilter::ERROR);
94+
8995
let env = Started::new(&configuration::ephemeral().into()).await;
9096

9197
force_database_error(&env.tracker);
@@ -100,6 +106,8 @@ async fn should_fail_when_the_auth_key_cannot_be_generated() {
100106
assert_failed_to_generate_key(response).await;
101107

102108
env.stop().await;
109+
110+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
103111
}
104112

105113
#[tokio::test]
@@ -237,13 +245,15 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {
237245

238246
assert_failed_to_delete_key(response).await;
239247

240-
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
241-
242248
env.stop().await;
249+
250+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
243251
}
244252

245253
#[tokio::test]
246254
async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
255+
logging::setup(LevelFilter::ERROR);
256+
247257
let env = Started::new(&configuration::ephemeral().into()).await;
248258

249259
let seconds_valid = 60;
@@ -275,6 +285,8 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
275285
assert_unauthorized(response).await;
276286

277287
env.stop().await;
288+
289+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
278290
}
279291

280292
#[tokio::test]
@@ -296,6 +308,8 @@ async fn should_allow_reloading_keys() {
296308

297309
#[tokio::test]
298310
async fn should_fail_when_keys_cannot_be_reloaded() {
311+
logging::setup(LevelFilter::ERROR);
312+
299313
let env = Started::new(&configuration::ephemeral().into()).await;
300314

301315
let seconds_valid = 60;
@@ -311,10 +325,14 @@ async fn should_fail_when_keys_cannot_be_reloaded() {
311325
assert_failed_to_reload_keys(response).await;
312326

313327
env.stop().await;
328+
329+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
314330
}
315331

316332
#[tokio::test]
317333
async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
334+
logging::setup(LevelFilter::ERROR);
335+
318336
let env = Started::new(&configuration::ephemeral().into()).await;
319337

320338
let seconds_valid = 60;
@@ -336,13 +354,17 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
336354
assert_unauthorized(response).await;
337355

338356
env.stop().await;
357+
358+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
339359
}
340360

341361
mod deprecated_generate_key_endpoint {
342362

343363
use torrust_tracker::core::auth::Key;
344364
use torrust_tracker_test_helpers::configuration;
365+
use tracing::level_filters::LevelFilter;
345366

367+
use crate::common::logging::{self, logs_contains_a_line_with};
346368
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
347369
use crate::servers::api::v1::asserts::{
348370
assert_auth_key_utf8, assert_failed_to_generate_key, assert_invalid_key_duration_param, assert_token_not_valid,
@@ -372,6 +394,8 @@ mod deprecated_generate_key_endpoint {
372394

373395
#[tokio::test]
374396
async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users() {
397+
logging::setup(LevelFilter::ERROR);
398+
375399
let env = Started::new(&configuration::ephemeral().into()).await;
376400

377401
let seconds_valid = 60;
@@ -389,6 +413,8 @@ mod deprecated_generate_key_endpoint {
389413
assert_unauthorized(response).await;
390414

391415
env.stop().await;
416+
417+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
392418
}
393419

394420
#[tokio::test]
@@ -414,6 +440,8 @@ mod deprecated_generate_key_endpoint {
414440

415441
#[tokio::test]
416442
async fn should_fail_when_the_auth_key_cannot_be_generated() {
443+
logging::setup(LevelFilter::ERROR);
444+
417445
let env = Started::new(&configuration::ephemeral().into()).await;
418446

419447
force_database_error(&env.tracker);
@@ -424,5 +452,7 @@ mod deprecated_generate_key_endpoint {
424452
assert_failed_to_generate_key(response).await;
425453

426454
env.stop().await;
455+
456+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
427457
}
428458
}

tests/servers/api/v1/contract/context/stats.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use bittorrent_primitives::info_hash::InfoHash;
44
use torrust_tracker::servers::apis::v1::context::stats::resources::Stats;
55
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
66
use torrust_tracker_test_helpers::configuration;
7+
use tracing::level_filters::LevelFilter;
78

9+
use crate::common::logging::{self, logs_contains_a_line_with};
810
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
911
use crate::servers::api::v1::asserts::{assert_stats, assert_token_not_valid, assert_unauthorized};
1012
use crate::servers::api::v1::client::Client;
@@ -58,6 +60,8 @@ async fn should_allow_getting_tracker_statistics() {
5860

5961
#[tokio::test]
6062
async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users() {
63+
logging::setup(LevelFilter::ERROR);
64+
6165
let env = Started::new(&configuration::ephemeral().into()).await;
6266

6367
let response = Client::new(connection_with_invalid_token(env.get_connection_info().bind_address.as_str()))
@@ -73,4 +77,6 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
7377
assert_unauthorized(response).await;
7478

7579
env.stop().await;
80+
81+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
7682
}

tests/servers/api/v1/contract/context/torrent.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ use torrust_tracker::servers::apis::v1::context::torrent::resources::peer::Peer;
55
use torrust_tracker::servers::apis::v1::context::torrent::resources::torrent::{self, Torrent};
66
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
77
use torrust_tracker_test_helpers::configuration;
8+
use tracing::level_filters::LevelFilter;
89

910
use crate::common::http::{Query, QueryParam};
11+
use crate::common::logging::{self, logs_contains_a_line_with};
1012
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
1113
use crate::servers::api::v1::asserts::{
1214
assert_bad_request, assert_invalid_infohash_param, assert_not_found, assert_token_not_valid, assert_torrent_info,
@@ -199,6 +201,8 @@ async fn should_fail_getting_torrents_when_the_info_hash_parameter_is_invalid()
199201

200202
#[tokio::test]
201203
async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
204+
logging::setup(LevelFilter::ERROR);
205+
202206
let env = Started::new(&configuration::ephemeral().into()).await;
203207

204208
let response = Client::new(connection_with_invalid_token(env.get_connection_info().bind_address.as_str()))
@@ -214,6 +218,8 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
214218
assert_unauthorized(response).await;
215219

216220
env.stop().await;
221+
222+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
217223
}
218224

219225
#[tokio::test]
@@ -281,6 +287,8 @@ async fn should_fail_getting_a_torrent_info_when_the_provided_infohash_is_invali
281287

282288
#[tokio::test]
283289
async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
290+
logging::setup(LevelFilter::ERROR);
291+
284292
let env = Started::new(&configuration::ephemeral().into()).await;
285293

286294
let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap();
@@ -300,4 +308,6 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
300308
assert_unauthorized(response).await;
301309

302310
env.stop().await;
311+
312+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
303313
}

tests/servers/api/v1/contract/context/whitelist.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use std::str::FromStr;
22

33
use bittorrent_primitives::info_hash::InfoHash;
44
use torrust_tracker_test_helpers::configuration;
5+
use tracing::level_filters::LevelFilter;
56

7+
use crate::common::logging::{self, logs_contains_a_line_with};
68
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
79
use crate::servers::api::v1::asserts::{
810
assert_failed_to_reload_whitelist, assert_failed_to_remove_torrent_from_whitelist, assert_failed_to_whitelist_torrent,
@@ -51,6 +53,8 @@ async fn should_allow_whitelisting_a_torrent_that_has_been_already_whitelisted()
5153

5254
#[tokio::test]
5355
async fn should_not_allow_whitelisting_a_torrent_for_unauthenticated_users() {
56+
logging::setup(LevelFilter::ERROR);
57+
5458
let env = Started::new(&configuration::ephemeral().into()).await;
5559

5660
let info_hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
@@ -68,6 +72,8 @@ async fn should_not_allow_whitelisting_a_torrent_for_unauthenticated_users() {
6872
assert_unauthorized(response).await;
6973

7074
env.stop().await;
75+
76+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
7177
}
7278

7379
#[tokio::test]
@@ -166,6 +172,8 @@ async fn should_fail_removing_a_torrent_from_the_whitelist_when_the_provided_inf
166172

167173
#[tokio::test]
168174
async fn should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist() {
175+
logging::setup(LevelFilter::ERROR);
176+
169177
let env = Started::new(&configuration::ephemeral().into()).await;
170178

171179
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
@@ -181,10 +189,14 @@ async fn should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist() {
181189
assert_failed_to_remove_torrent_from_whitelist(response).await;
182190

183191
env.stop().await;
192+
193+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
184194
}
185195

186196
#[tokio::test]
187197
async fn should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthenticated_users() {
198+
logging::setup(LevelFilter::ERROR);
199+
188200
let env = Started::new(&configuration::ephemeral().into()).await;
189201

190202
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
@@ -205,6 +217,8 @@ async fn should_not_allow_removing_a_torrent_from_the_whitelist_for_unauthentica
205217
assert_unauthorized(response).await;
206218

207219
env.stop().await;
220+
221+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
208222
}
209223

210224
#[tokio::test]
@@ -234,6 +248,8 @@ async fn should_allow_reload_the_whitelist_from_the_database() {
234248

235249
#[tokio::test]
236250
async fn should_fail_when_the_whitelist_cannot_be_reloaded_from_the_database() {
251+
logging::setup(LevelFilter::ERROR);
252+
237253
let env = Started::new(&configuration::ephemeral().into()).await;
238254

239255
let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
@@ -247,4 +263,6 @@ async fn should_fail_when_the_whitelist_cannot_be_reloaded_from_the_database() {
247263
assert_failed_to_reload_whitelist(response).await;
248264

249265
env.stop().await;
266+
267+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
250268
}

tests/servers/http/v1/contract.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,14 +1126,18 @@ mod configured_as_whitelisted {
11261126

11271127
use bittorrent_primitives::info_hash::InfoHash;
11281128
use torrust_tracker_test_helpers::configuration;
1129+
use tracing::level_filters::LevelFilter;
11291130

1131+
use crate::common::logging::{self, logs_contains_a_line_with};
11301132
use crate::servers::http::asserts::{assert_is_announce_response, assert_torrent_not_in_whitelist_error_response};
11311133
use crate::servers::http::client::Client;
11321134
use crate::servers::http::requests::announce::QueryBuilder;
11331135
use crate::servers::http::Started;
11341136

11351137
#[tokio::test]
11361138
async fn should_fail_if_the_torrent_is_not_in_the_whitelist() {
1139+
logging::setup(LevelFilter::ERROR);
1140+
11371141
let env = Started::new(&configuration::ephemeral_listed().into()).await;
11381142

11391143
let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
@@ -1145,6 +1149,8 @@ mod configured_as_whitelisted {
11451149
assert_torrent_not_in_whitelist_error_response(response).await;
11461150

11471151
env.stop().await;
1152+
1153+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
11481154
}
11491155

11501156
#[tokio::test]
@@ -1175,14 +1181,18 @@ mod configured_as_whitelisted {
11751181
use bittorrent_primitives::info_hash::InfoHash;
11761182
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
11771183
use torrust_tracker_test_helpers::configuration;
1184+
use tracing::level_filters::LevelFilter;
11781185

1186+
use crate::common::logging::{self, logs_contains_a_line_with};
11791187
use crate::servers::http::asserts::assert_scrape_response;
11801188
use crate::servers::http::client::Client;
11811189
use crate::servers::http::responses::scrape::{File, ResponseBuilder};
11821190
use crate::servers::http::{requests, Started};
11831191

11841192
#[tokio::test]
11851193
async fn should_return_the_zeroed_file_when_the_requested_file_is_not_whitelisted() {
1194+
logging::setup(LevelFilter::ERROR);
1195+
11861196
let env = Started::new(&configuration::ephemeral_listed().into()).await;
11871197

11881198
let info_hash = InfoHash::from_str("9c38422213e30bff212b30c360d26f9a02136422").unwrap();
@@ -1208,6 +1218,8 @@ mod configured_as_whitelisted {
12081218
assert_scrape_response(response, &expected_scrape_response).await;
12091219

12101220
env.stop().await;
1221+
1222+
assert!(logs_contains_a_line_with(&["ERROR", "tower_http", "response failed"]));
12111223
}
12121224

12131225
#[tokio::test]

0 commit comments

Comments
 (0)