Skip to content

Commit 583b305

Browse files
committed
test: [torrust#874] new key generation endpoint
1 parent 09beb52 commit 583b305

File tree

3 files changed

+267
-36
lines changed

3 files changed

+267
-36
lines changed

tests/servers/api/v1/asserts.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ pub async fn assert_bad_request(response: Response, body: &str) {
6161
assert_eq!(response.text().await.unwrap(), body);
6262
}
6363

64+
pub async fn assert_unprocessable_content(response: Response, text: &str) {
65+
assert_eq!(response.status(), 422);
66+
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
67+
assert!(response.text().await.unwrap().contains(text));
68+
}
69+
6470
pub async fn assert_not_found(response: Response) {
6571
assert_eq!(response.status(), 404);
6672
// todo: missing header in the response
@@ -82,10 +88,37 @@ pub async fn assert_invalid_infohash_param(response: Response, invalid_infohash:
8288
.await;
8389
}
8490

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

95+
pub async fn assert_invalid_auth_key_post_param(response: Response, invalid_auth_key: &str) {
96+
assert_bad_request(
97+
response,
98+
&format!(
99+
"Invalid URL: invalid auth key: string \"{}\", ParseKeyError",
100+
&invalid_auth_key
101+
),
102+
)
103+
.await;
104+
}
105+
106+
pub async fn _assert_unprocessable_auth_key_param(response: Response, _invalid_value: &str) {
107+
assert_unprocessable_content(
108+
response,
109+
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
110+
)
111+
.await;
112+
}
113+
114+
pub async fn assert_unprocessable_auth_key_duration_param(response: Response, _invalid_value: &str) {
115+
assert_unprocessable_content(
116+
response,
117+
"Failed to deserialize the JSON body into the target type: seconds_valid: invalid type",
118+
)
119+
.await;
120+
}
121+
89122
pub async fn assert_invalid_key_duration_param(response: Response, invalid_key_duration: &str) {
90123
assert_bad_request(
91124
response,

tests/servers/api/v1/client.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use reqwest::Response;
2+
use serde::Serialize;
23

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

2021
pub async fn generate_auth_key(&self, seconds_valid: i32) -> Response {
21-
self.post(&format!("key/{}", &seconds_valid)).await
22+
self.post_empty(&format!("key/{}", &seconds_valid)).await
23+
}
24+
25+
pub async fn add_auth_key(&self, add_key_form: AddKeyForm) -> Response {
26+
self.post_form("keys", &add_key_form).await
2227
}
2328

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

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

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

66-
pub async fn post(&self, path: &str) -> Response {
71+
pub async fn post_empty(&self, path: &str) -> Response {
72+
reqwest::Client::new()
73+
.post(self.base_url(path).clone())
74+
.query(&ReqwestQuery::from(self.query_with_token()))
75+
.send()
76+
.await
77+
.unwrap()
78+
}
79+
80+
pub async fn post_form<T: Serialize + ?Sized>(&self, path: &str, form: &T) -> Response {
6781
reqwest::Client::new()
6882
.post(self.base_url(path).clone())
6983
.query(&ReqwestQuery::from(self.query_with_token()))
84+
.json(&form)
7085
.send()
7186
.await
7287
.unwrap()
@@ -114,3 +129,10 @@ pub async fn get(path: &str, query: Option<Query>) -> Response {
114129
None => reqwest::Client::builder().build().unwrap().get(path).send().await.unwrap(),
115130
}
116131
}
132+
133+
#[derive(Serialize, Debug)]
134+
pub struct AddKeyForm {
135+
#[serde(rename = "key")]
136+
pub opt_key: Option<String>,
137+
pub seconds_valid: u64,
138+
}

0 commit comments

Comments
 (0)