Skip to content

Commit

Permalink
test: [torrust#1152] write assertions when API writes errors into logs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Dec 26, 2024
1 parent cc2840f commit 5d49d48
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 129 deletions.
92 changes: 52 additions & 40 deletions tests/servers/api/v1/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,82 +20,94 @@ impl Client {
}
}

pub async fn generate_auth_key(&self, seconds_valid: i32) -> Response {
self.post_empty(&format!("key/{}", &seconds_valid)).await
pub async fn generate_auth_key(&self, seconds_valid: i32, headers: Option<HeaderMap>) -> Response {
self.post_empty(&format!("key/{}", &seconds_valid), headers).await
}

pub async fn add_auth_key(&self, add_key_form: AddKeyForm) -> Response {
self.post_form("keys", &add_key_form).await
pub async fn add_auth_key(&self, add_key_form: AddKeyForm, headers: Option<HeaderMap>) -> Response {
self.post_form("keys", &add_key_form, headers).await
}

pub async fn delete_auth_key(&self, key: &str) -> Response {
self.delete(&format!("key/{}", &key)).await
pub async fn delete_auth_key(&self, key: &str, headers: Option<HeaderMap>) -> Response {
self.delete(&format!("key/{}", &key), headers).await
}

pub async fn reload_keys(&self) -> Response {
self.get("keys/reload", Query::default()).await
pub async fn reload_keys(&self, headers: Option<HeaderMap>) -> Response {
self.get("keys/reload", Query::default(), headers).await
}

pub async fn whitelist_a_torrent(&self, info_hash: &str) -> Response {
self.post_empty(&format!("whitelist/{}", &info_hash)).await
pub async fn whitelist_a_torrent(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response {
self.post_empty(&format!("whitelist/{}", &info_hash), headers).await
}

pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Response {
self.delete(&format!("whitelist/{}", &info_hash)).await
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response {
self.delete(&format!("whitelist/{}", &info_hash), headers).await
}

pub async fn reload_whitelist(&self) -> Response {
self.get("whitelist/reload", Query::default()).await
pub async fn reload_whitelist(&self, headers: Option<HeaderMap>) -> Response {
self.get("whitelist/reload", Query::default(), headers).await
}

pub async fn get_torrent(&self, info_hash: &str) -> Response {
self.get(&format!("torrent/{}", &info_hash), Query::default()).await
pub async fn get_torrent(&self, info_hash: &str, headers: Option<HeaderMap>) -> Response {
self.get(&format!("torrent/{}", &info_hash), Query::default(), headers).await
}

pub async fn get_torrents(&self, params: Query) -> Response {
self.get("torrents", params).await
pub async fn get_torrents(&self, params: Query, headers: Option<HeaderMap>) -> Response {
self.get("torrents", params, headers).await
}

pub async fn get_tracker_statistics(&self) -> Response {
self.get("stats", Query::default()).await
pub async fn get_tracker_statistics(&self, headers: Option<HeaderMap>) -> Response {
self.get("stats", Query::default(), headers).await
}

pub async fn get(&self, path: &str, params: Query) -> Response {
pub async fn get(&self, path: &str, params: Query, headers: Option<HeaderMap>) -> Response {
let mut query: Query = params;

if let Some(token) = &self.connection_info.api_token {
query.add_param(QueryParam::new("token", token));
};

self.get_request_with_query(path, query, None).await
self.get_request_with_query(path, query, headers).await
}

pub async fn post_empty(&self, path: &str) -> Response {
reqwest::Client::new()
pub async fn post_empty(&self, path: &str, headers: Option<HeaderMap>) -> Response {
let builder = reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
.query(&ReqwestQuery::from(self.query_with_token()));

let builder = match headers {
Some(headers) => builder.headers(headers),
None => builder,
};

builder.send().await.unwrap()
}

pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> Response {
reqwest::Client::new()
pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T, headers: Option<HeaderMap>) -> Response {
let builder = reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.json(&form)
.send()
.await
.unwrap()
.json(&form);

let builder = match headers {
Some(headers) => builder.headers(headers),
None => builder,
};

builder.send().await.unwrap()
}

async fn delete(&self, path: &str) -> Response {
reqwest::Client::new()
async fn delete(&self, path: &str, headers: Option<HeaderMap>) -> Response {
let builder = reqwest::Client::new()
.delete(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
.query(&ReqwestQuery::from(self.query_with_token()));

let builder = match headers {
Some(headers) => builder.headers(headers),
None => builder,
};

builder.send().await.unwrap()
}

pub async fn get_request_with_query(&self, path: &str, params: Query, headers: Option<HeaderMap>) -> Response {
Expand Down
22 changes: 18 additions & 4 deletions tests/servers/api/v1/contract/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,19 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {

let env = Started::new(&configuration::ephemeral().into()).await;

let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.get_request_with_query("stats", Query::default(), None)
.get_request_with_query("stats", Query::default(), Some(headers_with_request_id(request_id)))
.await;

assert_unauthorized(response).await;

assert!(
logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]),
"Expected logs to contain: ERROR ... API ... request_id={request_id}"
);

env.stop().await;
}

Expand All @@ -57,12 +64,12 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {

assert_token_not_valid(response).await;

env.stop().await;

assert!(
logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]),
"Expected logs to contain: ERROR ... API ... request_id={request_id}"
);

env.stop().await;
}

#[tokio::test]
Expand All @@ -71,16 +78,23 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {

let env = Started::new(&configuration::ephemeral().into()).await;

let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.get_request_with_query(
"stats",
Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()),
None,
Some(headers_with_request_id(request_id)),
)
.await;

assert_token_not_valid(response).await;

assert!(
logs_contains_a_line_with(&["ERROR", "API", &format!("{request_id}")]),
"Expected logs to contain: ERROR ... API ... request_id={request_id}"
);

env.stop().await;
}

Expand Down
Loading

0 comments on commit 5d49d48

Please sign in to comment.