Skip to content

Commit 08efc3b

Browse files
committed
refactor: [torrust#1316] move tracker API integration tests to pkg
1 parent a841e03 commit 08efc3b

24 files changed

+109
-87
lines changed

Cargo.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "packag
7474
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "packages/udp-tracker-server" }
7575
tracing = "0"
7676
tracing-subscriber = { version = "0", features = ["json"] }
77-
url = { version = "2", features = ["serde"] }
7877
uuid = { version = "1", features = ["v4"] }
7978
zerocopy = "0.7"
8079

packages/axum-tracker-api-server/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ thiserror = "2"
3333
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
3434
torrust-axum-server = { version = "3.0.0-develop", path = "../axum-server" }
3535
torrust-server-lib = { version = "3.0.0-develop", path = "../server-lib" }
36+
torrust-tracker-api-client = { version = "3.0.0-develop", path = "../tracker-api-client" }
3637
torrust-tracker-api-core = { version = "3.0.0-develop", path = "../tracker-api-core" }
3738
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
3839
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
@@ -46,3 +47,5 @@ local-ip-address = "0"
4647
mockall = "0"
4748
torrust-tracker-api-client = { version = "3.0.0-develop", path = "../tracker-api-client" }
4849
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
50+
url = { version = "2", features = ["serde"] }
51+
uuid = { version = "1", features = ["v4"] }

tests/servers/api/environment.rs packages/axum-tracker-api-server/src/environment.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ use bittorrent_tracker_core::container::TrackerCoreContainer;
77
use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer;
88
use futures::executor::block_on;
99
use torrust_axum_server::tsl::make_rust_tls;
10-
use torrust_axum_tracker_api_server::server::{ApiServer, Launcher, Running, Stopped};
1110
use torrust_server_lib::registar::Registar;
1211
use torrust_tracker_api_client::connection_info::{ConnectionInfo, Origin};
1312
use torrust_tracker_api_core::container::TrackerHttpApiCoreContainer;
1413
use torrust_tracker_configuration::{logging, Configuration};
1514
use torrust_tracker_primitives::peer;
1615

16+
use crate::server::{ApiServer, Launcher, Running, Stopped};
17+
18+
pub type Started = Environment<Running>;
19+
1720
pub struct Environment<S>
1821
where
1922
S: std::fmt::Debug + std::fmt::Display,
@@ -38,6 +41,11 @@ where
3841
}
3942

4043
impl Environment<Stopped> {
44+
/// # Panics
45+
///
46+
/// Will panic if it cannot make the TSL configuration from the provided
47+
/// configuration.
48+
#[must_use]
4149
pub fn new(configuration: &Arc<Configuration>) -> Self {
4250
initialize_global_services(configuration);
4351

@@ -59,6 +67,9 @@ impl Environment<Stopped> {
5967
}
6068
}
6169

70+
/// # Panics
71+
///
72+
/// Will panic if the server cannot be started.
6273
pub async fn start(self) -> Environment<Running> {
6374
let access_tokens = Arc::new(
6475
self.container
@@ -89,6 +100,9 @@ impl Environment<Running> {
89100
Environment::<Stopped>::new(configuration).start().await
90101
}
91102

103+
/// # Panics
104+
///
105+
/// Will panic if the server cannot be stopped.
92106
pub async fn stop(self) -> Environment<Stopped> {
93107
Environment {
94108
container: self.container,
@@ -97,6 +111,11 @@ impl Environment<Running> {
97111
}
98112
}
99113

114+
/// # Panics
115+
///
116+
/// Will panic if it cannot build the origin for the connection info from the
117+
/// server local socket address.
118+
#[must_use]
100119
pub fn get_connection_info(&self) -> ConnectionInfo {
101120
let origin = Origin::new(&format!("http://{}/", self.server.state.local_addr)).unwrap(); // DevSkim: ignore DS137138
102121

@@ -112,6 +131,7 @@ impl Environment<Running> {
112131
}
113132
}
114133

134+
#[must_use]
115135
pub fn bind_address(&self) -> SocketAddr {
116136
self.server.state.local_addr
117137
}
@@ -123,6 +143,14 @@ pub struct EnvContainer {
123143
}
124144

125145
impl EnvContainer {
146+
/// # Panics
147+
///
148+
/// Will panic if:
149+
///
150+
/// - The configuration does not contain a HTTP tracker configuration.
151+
/// - The configuration does not contain a UDP tracker configuration.
152+
/// - The configuration does not contain a HTTP API configuration.
153+
#[must_use]
126154
pub fn initialize(configuration: &Configuration) -> Self {
127155
let core_config = Arc::new(configuration.core.clone());
128156

packages/axum-tracker-api-server/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
//! > **NOTICE**: we are using [curl](https://curl.se/) in the API examples.
154154
//! > And you have to use quotes around the URL in order to avoid unexpected
155155
//! > errors. For example: `curl "http://127.0.0.1:1212/api/v1/stats?token=MyAccessToken"`.
156+
pub mod environment;
156157
pub mod routes;
157158
pub mod server;
158159
pub mod v1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pub fn invalid_info_hashes() -> Vec<String> {
2+
[
3+
"0".to_string(),
4+
"-1".to_string(),
5+
"1.1".to_string(),
6+
"INVALID INFOHASH".to_string(),
7+
"9c38422213e30bff212b30c360d26f9a0213642".to_string(), // 39-char length instead of 40. DevSkim: ignore DS173237
8+
"9c38422213e30bff212b30c360d26f9a0213642&".to_string(), // Invalid char
9+
]
10+
.to_vec()
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod fixtures;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Integration tests.
2+
//!
3+
//! ```text
4+
//! cargo test --test integration
5+
//! ```
6+
7+
use torrust_tracker_clock::clock;
8+
mod common;
9+
mod server;
10+
11+
/// This code needs to be copied into each crate.
12+
/// Working version, for production.
13+
#[cfg(not(test))]
14+
#[allow(dead_code)]
15+
pub(crate) type CurrentClock = clock::Working;
16+
17+
/// Stopped version, for testing.
18+
#[cfg(test)]
19+
#[allow(dead_code)]
20+
pub(crate) type CurrentClock = clock::Stopped;

tests/servers/api/mod.rs packages/axum-tracker-api-server/tests/server/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
use std::sync::Arc;
2-
3-
use bittorrent_tracker_core::databases::Database;
4-
use torrust_axum_tracker_api_server::server;
5-
61
pub mod connection_info;
7-
pub mod environment;
82
pub mod v1;
93

10-
pub type Started = environment::Environment<server::Running>;
4+
use std::sync::Arc;
5+
6+
use bittorrent_tracker_core::databases::Database;
117

128
/// It forces a database error by dropping all tables. That makes all queries
139
/// fail.

tests/servers/api/v1/contract/authentication.rs packages/axum-tracker-api-server/tests/server/v1/contract/authentication.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
use torrust_axum_tracker_api_server::environment::Started;
12
use torrust_tracker_api_client::common::http::{Query, QueryParam};
23
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};
3-
use torrust_tracker_test_helpers::configuration;
4+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
5+
use torrust_tracker_test_helpers::{configuration, logging};
46
use uuid::Uuid;
57

6-
use crate::common::logging::{self, logs_contains_a_line_with};
7-
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
8-
use crate::servers::api::Started;
8+
use crate::server::v1::asserts::{assert_token_not_valid, assert_unauthorized};
99

1010
#[tokio::test]
1111
async fn should_authenticate_requests_by_using_a_token_query_param() {

tests/servers/api/v1/contract/context/auth_key.rs packages/axum-tracker-api-server/tests/server/v1/contract/context/auth_key.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@ use std::time::Duration;
22

33
use bittorrent_tracker_core::authentication::Key;
44
use serde::Serialize;
5+
use torrust_axum_tracker_api_server::environment::Started;
56
use torrust_tracker_api_client::v1::client::{headers_with_request_id, AddKeyForm, Client};
6-
use torrust_tracker_test_helpers::configuration;
7+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
8+
use torrust_tracker_test_helpers::{configuration, logging};
79
use uuid::Uuid;
810

9-
use crate::common::logging::{self, logs_contains_a_line_with};
10-
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
11-
use crate::servers::api::v1::asserts::{
11+
use crate::server::connection_info::{connection_with_invalid_token, connection_with_no_token};
12+
use crate::server::force_database_error;
13+
use crate::server::v1::asserts::{
1214
assert_auth_key_utf8, assert_failed_to_delete_key, assert_failed_to_generate_key, assert_failed_to_reload_keys,
1315
assert_invalid_auth_key_get_param, assert_invalid_auth_key_post_param, assert_ok, assert_token_not_valid,
1416
assert_unauthorized, assert_unprocessable_auth_key_duration_param,
1517
};
16-
use crate::servers::api::{force_database_error, Started};
1718

1819
#[tokio::test]
1920
async fn should_allow_generating_a_new_random_auth_key() {
@@ -481,17 +482,18 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
481482
mod deprecated_generate_key_endpoint {
482483

483484
use bittorrent_tracker_core::authentication::Key;
485+
use torrust_axum_tracker_api_server::environment::Started;
484486
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};
485-
use torrust_tracker_test_helpers::configuration;
487+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
488+
use torrust_tracker_test_helpers::{configuration, logging};
486489
use uuid::Uuid;
487490

488-
use crate::common::logging::{self, logs_contains_a_line_with};
489-
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
490-
use crate::servers::api::v1::asserts::{
491+
use crate::server::connection_info::{connection_with_invalid_token, connection_with_no_token};
492+
use crate::server::force_database_error;
493+
use crate::server::v1::asserts::{
491494
assert_auth_key_utf8, assert_failed_to_generate_key, assert_invalid_key_duration_param, assert_token_not_valid,
492495
assert_unauthorized,
493496
};
494-
use crate::servers::api::{force_database_error, Started};
495497

496498
#[tokio::test]
497499
async fn should_allow_generating_a_new_auth_key() {

tests/servers/api/v1/contract/context/health_check.rs packages/axum-tracker-api-server/tests/server/v1/contract/context/health_check.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
use torrust_axum_tracker_api_server::environment::Started;
12
use torrust_axum_tracker_api_server::v1::context::health_check::resources::{Report, Status};
23
use torrust_tracker_api_client::v1::client::get;
3-
use torrust_tracker_test_helpers::configuration;
4+
use torrust_tracker_test_helpers::{configuration, logging};
45
use url::Url;
56

6-
use crate::common::logging;
7-
use crate::servers::api::Started;
8-
97
#[tokio::test]
108
async fn health_check_endpoint_should_return_status_ok_if_api_is_running() {
119
logging::setup();

tests/servers/api/v1/contract/context/stats.rs packages/axum-tracker-api-server/tests/server/v1/contract/context/stats.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
use std::str::FromStr;
22

33
use bittorrent_primitives::info_hash::InfoHash;
4+
use torrust_axum_tracker_api_server::environment::Started;
45
use torrust_axum_tracker_api_server::v1::context::stats::resources::Stats;
56
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};
67
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
7-
use torrust_tracker_test_helpers::configuration;
8+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
9+
use torrust_tracker_test_helpers::{configuration, logging};
810
use uuid::Uuid;
911

10-
use crate::common::logging::{self, logs_contains_a_line_with};
11-
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
12-
use crate::servers::api::v1::asserts::{assert_stats, assert_token_not_valid, assert_unauthorized};
13-
use crate::servers::api::Started;
12+
use crate::server::connection_info::{connection_with_invalid_token, connection_with_no_token};
13+
use crate::server::v1::asserts::{assert_stats, assert_token_not_valid, assert_unauthorized};
1414

1515
#[tokio::test]
1616
async fn should_allow_getting_tracker_statistics() {

tests/servers/api/v1/contract/context/torrent.rs packages/axum-tracker-api-server/tests/server/v1/contract/context/torrent.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
use std::str::FromStr;
22

33
use bittorrent_primitives::info_hash::InfoHash;
4+
use torrust_axum_tracker_api_server::environment::Started;
45
use torrust_axum_tracker_api_server::v1::context::torrent::resources::peer::Peer;
56
use torrust_axum_tracker_api_server::v1::context::torrent::resources::torrent::{self, Torrent};
67
use torrust_tracker_api_client::common::http::{Query, QueryParam};
78
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};
89
use torrust_tracker_primitives::peer::fixture::PeerBuilder;
9-
use torrust_tracker_test_helpers::configuration;
10+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
11+
use torrust_tracker_test_helpers::{configuration, logging};
1012
use uuid::Uuid;
1113

12-
use crate::common::logging::{self, logs_contains_a_line_with};
13-
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
14-
use crate::servers::api::v1::asserts::{
14+
use crate::server::connection_info::{connection_with_invalid_token, connection_with_no_token};
15+
use crate::server::v1::asserts::{
1516
assert_bad_request, assert_invalid_infohash_param, assert_not_found, assert_token_not_valid, assert_torrent_info,
1617
assert_torrent_list, assert_torrent_not_known, assert_unauthorized,
1718
};
18-
use crate::servers::api::v1::contract::fixtures::{
19-
invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found,
20-
};
21-
use crate::servers::api::Started;
19+
use crate::server::v1::contract::fixtures::{invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found};
2220

2321
#[tokio::test]
2422
async fn should_allow_getting_all_torrents() {

tests/servers/api/v1/contract/context/whitelist.rs packages/axum-tracker-api-server/tests/server/v1/contract/context/whitelist.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
use std::str::FromStr;
22

33
use bittorrent_primitives::info_hash::InfoHash;
4+
use torrust_axum_tracker_api_server::environment::Started;
45
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};
5-
use torrust_tracker_test_helpers::configuration;
6+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
7+
use torrust_tracker_test_helpers::{configuration, logging};
68
use uuid::Uuid;
79

8-
use crate::common::logging::{self, logs_contains_a_line_with};
9-
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
10-
use crate::servers::api::v1::asserts::{
10+
use crate::server::connection_info::{connection_with_invalid_token, connection_with_no_token};
11+
use crate::server::force_database_error;
12+
use crate::server::v1::asserts::{
1113
assert_failed_to_reload_whitelist, assert_failed_to_remove_torrent_from_whitelist, assert_failed_to_whitelist_torrent,
1214
assert_invalid_infohash_param, assert_not_found, assert_ok, assert_token_not_valid, assert_unauthorized,
1315
};
14-
use crate::servers::api::v1::contract::fixtures::{
15-
invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found,
16-
};
17-
use crate::servers::api::{force_database_error, Started};
16+
use crate::server::v1::contract::fixtures::{invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found};
1817

1918
#[tokio::test]
2019
async fn should_allow_whitelisting_a_torrent() {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
pub mod authentication;
2-
pub mod configuration;
32
pub mod context;
43
pub mod fixtures;

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

-32
This file was deleted.

0 commit comments

Comments
 (0)