Skip to content

Commit

Permalink
test(api): [#143] add test for invalid key duration URL path param
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 13, 2023
1 parent 39c15c6 commit 2c222ee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/apis/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ use crate::tracker::services::statistics::get_metrics;
use crate::tracker::services::torrent::{get_torrent_info, get_torrents, Pagination};
use crate::tracker::Tracker;

/* code-review:
When Axum cannot parse a path or query param it shows a message like this:
For the "seconds_valid_or_key" path param:
"Invalid URL: Cannot parse "-1" to a `u64`"
That message is not an informative message, specially if you have more than one param.
We should show a message similar to the one we use when we parse the value in the handler.
For example:
"Invalid URL: invalid infohash param: string \"INVALID VALUE\", expected a 40 character long string"
We can customize the error message by using a custom type with custom serde deserialization.
The same we are using for the "InfoHashVisitor".
Input data from HTTP requests should use struts with primitive types (first level of validation).
We can put the second level of validation in the application and domain services.
*/

pub fn router(tracker: &Arc<Tracker>) -> Router {
Router::new()
// Stats
Expand Down
4 changes: 3 additions & 1 deletion tests/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Client {
self.get_request_with_query(path, query).await
}

async fn post(&self, path: &str) -> Response {
pub async fn post(&self, path: &str) -> Response {
reqwest::Client::new()
.post(self.base_url(path).clone())
.query(&ReqwestQuery::from(self.query_with_token()))
Expand All @@ -142,6 +142,7 @@ impl Client {
format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path)
}

// Unauthenticated GET request with query component
pub async fn get_request_with_query(&self, path: &str, params: Query) -> Response {
reqwest::Client::builder()
.build()
Expand All @@ -153,6 +154,7 @@ impl Client {
.unwrap()
}

// Unauthenticated GET request
pub async fn get_request(&self, path: &str) -> Response {
reqwest::Client::builder()
.build()
Expand Down
22 changes: 16 additions & 6 deletions tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,16 +1444,26 @@ mod tracker_apis {
}

#[tokio::test]
async fn should_fail_generating_a_new_auth_key_when_the_key_duration_cannot_be_parsed() {
async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid() {
let api_server = start_default_api(&Version::Axum).await;

let invalid_key_duration = -1;
let invalid_key_durations = [
// "", it returns 404
// " ", it returns 404
"-1", "text",
];

let response = Client::new(api_server.get_connection_info())
.generate_auth_key(invalid_key_duration)
.await;
for invalid_key_duration in &invalid_key_durations {
let response = Client::new(api_server.get_connection_info())
.post(&format!("key/{}", &invalid_key_duration))
.await;

assert_bad_request(response, "Invalid URL: Cannot parse `\"-1\"` to a `u64`").await;
assert_bad_request(
response,
&format!("Invalid URL: Cannot parse `\"{invalid_key_duration}\"` to a `u64`"),
)
.await;
}
}

#[tokio::test]
Expand Down

0 comments on commit 2c222ee

Please sign in to comment.