Skip to content

Commit

Permalink
refactor: [#1159] use new tracker api client package in tests
Browse files Browse the repository at this point in the history
The Tracker API client was extracted into a new package. This removes
the duplicate client in tests and starts using the new package.
  • Loading branch information
josecelano committed Jan 10, 2025
1 parent a1ded65 commit 99fcbb6
Show file tree
Hide file tree
Showing 13 changed files with 14 additions and 204 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ ignored = ["crossbeam-skiplist", "dashmap", "figment", "parking_lot", "serde_byt
[dev-dependencies]
local-ip-address = "0"
mockall = "0"
torrust-tracker-api-client = { version = "3.0.0-develop", path = "packages/tracker-api-client" }
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "packages/test-helpers" }

[workspace]
Expand Down
10 changes: 0 additions & 10 deletions packages/tracker-api-client/src/connection_info.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#[must_use]
pub fn connection_with_invalid_token(bind_address: &str) -> ConnectionInfo {
ConnectionInfo::authenticated(bind_address, "invalid token")
}

#[must_use]
pub fn connection_with_no_token(bind_address: &str) -> ConnectionInfo {
ConnectionInfo::anonymous(bind_address)
}

#[derive(Clone)]
pub struct ConnectionInfo {
pub bind_address: String,
Expand Down
24 changes: 2 additions & 22 deletions tests/servers/api/connection_info.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
use torrust_tracker_api_client::connection_info::ConnectionInfo;

pub fn connection_with_invalid_token(bind_address: &str) -> ConnectionInfo {
ConnectionInfo::authenticated(bind_address, "invalid token")
}

pub fn connection_with_no_token(bind_address: &str) -> ConnectionInfo {
ConnectionInfo::anonymous(bind_address)
}

#[derive(Clone)]
pub struct ConnectionInfo {
pub bind_address: String,
pub api_token: Option<String>,
}

impl ConnectionInfo {
pub fn authenticated(bind_address: &str, api_token: &str) -> Self {
Self {
bind_address: bind_address.to_string(),
api_token: Some(api_token.to_string()),
}
}

pub fn anonymous(bind_address: &str) -> Self {
Self {
bind_address: bind_address.to_string(),
api_token: None,
}
}
}
2 changes: 1 addition & 1 deletion tests/servers/api/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use torrust_tracker_lib::servers::udp::server::banning::BanService;
use torrust_tracker_lib::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP;
use torrust_tracker_primitives::peer;

use super::connection_info::ConnectionInfo;
use torrust_tracker_api_client::connection_info::ConnectionInfo;

pub struct Environment<S>
where
Expand Down
161 changes: 0 additions & 161 deletions tests/servers/api/v1/client.rs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/servers/api/v1/contract/authentication.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use torrust_tracker_test_helpers::configuration;
use uuid::Uuid;

use crate::common::http::{Query, QueryParam};
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::api::v1::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::servers::api::v1::client::{headers_with_request_id, Client};
use crate::servers::api::Started;
use torrust_tracker_api_client::common::http::{Query, QueryParam};
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};

#[tokio::test]
async fn should_authenticate_requests_by_using_a_token_query_param() {
Expand Down
4 changes: 2 additions & 2 deletions tests/servers/api/v1/contract/context/auth_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::servers::api::v1::asserts::{
assert_invalid_auth_key_get_param, assert_invalid_auth_key_post_param, assert_ok, assert_token_not_valid,
assert_unauthorized, assert_unprocessable_auth_key_duration_param,
};
use crate::servers::api::v1::client::{headers_with_request_id, AddKeyForm, Client};
use crate::servers::api::{force_database_error, Started};
use torrust_tracker_api_client::v1::client::{headers_with_request_id, AddKeyForm, Client};

#[tokio::test]
async fn should_allow_generating_a_new_random_auth_key() {
Expand Down Expand Up @@ -472,8 +472,8 @@ mod deprecated_generate_key_endpoint {
assert_auth_key_utf8, assert_failed_to_generate_key, assert_invalid_key_duration_param, assert_token_not_valid,
assert_unauthorized,
};
use crate::servers::api::v1::client::{headers_with_request_id, Client};
use crate::servers::api::{force_database_error, Started};
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};

#[tokio::test]
async fn should_allow_generating_a_new_auth_key() {
Expand Down
2 changes: 1 addition & 1 deletion tests/servers/api/v1/contract/context/health_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use torrust_tracker_lib::servers::apis::v1::context::health_check::resources::{R
use torrust_tracker_test_helpers::configuration;

use crate::common::logging;
use crate::servers::api::v1::client::get;
use crate::servers::api::Started;
use torrust_tracker_api_client::v1::client::get;

#[tokio::test]
async fn health_check_endpoint_should_return_status_ok_if_api_is_running() {
Expand Down
2 changes: 1 addition & 1 deletion tests/servers/api/v1/contract/context/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use uuid::Uuid;
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::servers::api::v1::asserts::{assert_stats, assert_token_not_valid, assert_unauthorized};
use crate::servers::api::v1::client::{headers_with_request_id, Client};
use crate::servers::api::Started;
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};

#[tokio::test]
async fn should_allow_getting_tracker_statistics() {
Expand Down
4 changes: 2 additions & 2 deletions tests/servers/api/v1/contract/context/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use torrust_tracker_primitives::peer::fixture::PeerBuilder;
use torrust_tracker_test_helpers::configuration;
use uuid::Uuid;

use crate::common::http::{Query, QueryParam};
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::servers::api::v1::asserts::{
assert_bad_request, assert_invalid_infohash_param, assert_not_found, assert_token_not_valid, assert_torrent_info,
assert_torrent_list, assert_torrent_not_known, assert_unauthorized,
};
use crate::servers::api::v1::client::{headers_with_request_id, Client};
use crate::servers::api::v1::contract::fixtures::{
invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found,
};
use crate::servers::api::Started;
use torrust_tracker_api_client::common::http::{Query, QueryParam};
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};

#[tokio::test]
async fn should_allow_getting_all_torrents() {
Expand Down
2 changes: 1 addition & 1 deletion tests/servers/api/v1/contract/context/whitelist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::servers::api::v1::asserts::{
assert_failed_to_reload_whitelist, assert_failed_to_remove_torrent_from_whitelist, assert_failed_to_whitelist_torrent,
assert_invalid_infohash_param, assert_not_found, assert_ok, assert_token_not_valid, assert_unauthorized,
};
use crate::servers::api::v1::client::{headers_with_request_id, Client};
use crate::servers::api::v1::contract::fixtures::{
invalid_infohashes_returning_bad_request, invalid_infohashes_returning_not_found,
};
use crate::servers::api::{force_database_error, Started};
use torrust_tracker_api_client::v1::client::{headers_with_request_id, Client};

#[tokio::test]
async fn should_allow_whitelisting_a_torrent() {
Expand Down
1 change: 0 additions & 1 deletion tests/servers/api/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod asserts;
pub mod client;
pub mod contract;

0 comments on commit 99fcbb6

Please sign in to comment.