Skip to content

Commit

Permalink
test: [torrust#874] new key generation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jul 30, 2024
1 parent 09beb52 commit 0b42017
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 36 deletions.
28 changes: 27 additions & 1 deletion tests/servers/api/v1/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ pub async fn assert_bad_request(response: Response, body: &str) {
assert_eq!(response.text().await.unwrap(), body);
}

pub async fn assert_unprocessable_content(response: Response, text: &str) {
assert_eq!(response.status(), 422);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert!(response.text().await.unwrap().contains(text));
}

pub async fn assert_not_found(response: Response) {
assert_eq!(response.status(), 404);
// todo: missing header in the response
Expand All @@ -82,10 +88,30 @@ pub async fn assert_invalid_infohash_param(response: Response, invalid_infohash:
.await;
}

pub async fn assert_invalid_auth_key_param(response: Response, invalid_auth_key: &str) {
pub async fn assert_invalid_auth_key_get_param(response: Response, invalid_auth_key: &str) {
assert_bad_request(response, &format!("Invalid auth key id param \"{}\"", &invalid_auth_key)).await;
}

pub async fn assert_invalid_auth_key_post_param(response: Response, invalid_auth_key: &str) {
assert_bad_request(response, &format!("Invalid URL: invalid auth key: string \"{}\", ParseKeyError", &invalid_auth_key)).await;
}

pub async fn _assert_unprocessable_auth_key_param(response: Response, _invalid_value: &str) {
assert_unprocessable_content(
response,
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
)
.await;
}

pub async fn assert_unprocessable_auth_key_duration_param(response: Response, _invalid_value: &str) {
assert_unprocessable_content(
response,
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
)
.await;
}

pub async fn assert_invalid_key_duration_param(response: Response, invalid_key_duration: &str) {
assert_bad_request(
response,
Expand Down
28 changes: 25 additions & 3 deletions tests/servers/api/v1/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use reqwest::Response;
use serde::Serialize;

use crate::common::http::{Query, QueryParam, ReqwestQuery};
use crate::servers::api::connection_info::ConnectionInfo;
Expand All @@ -18,7 +19,11 @@ impl Client {
}

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

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

pub async fn delete_auth_key(&self, key: &str) -> Response {
Expand All @@ -30,7 +35,7 @@ impl Client {
}

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

pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Response {
Expand Down Expand Up @@ -63,10 +68,20 @@ impl Client {
self.get_request_with_query(path, query).await
}

pub async fn post(&self, path: &str) -> Response {
pub async fn post_empty(&self, path: &str) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.send()
.await
.unwrap()
}

pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
.json(&form)
.send()
.await
.unwrap()
Expand Down Expand Up @@ -114,3 +129,10 @@ pub async fn get(path: &str, query: Option<Query>) -> Response {
None => reqwest::Client::builder().build().unwrap().get(path).send().await.unwrap(),
}
}

#[derive(Serialize, Debug)]
pub struct AddKeyForm {
#[serde(rename = "key")]
pub opt_key: Option<String>,
pub seconds_valid: u64,
}
Loading

0 comments on commit 0b42017

Please sign in to comment.