Skip to content

Commit

Permalink
test(api): [#143] add tests for authenticaation
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Jan 12, 2023
1 parent 517ffde commit 2da0719
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 8 deletions.
30 changes: 22 additions & 8 deletions tests/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,7 @@ impl Client {
query.add_param(QueryParam::new("token", token));
};

reqwest::Client::builder()
.build()
.unwrap()
.get(self.base_url(path))
.query(&ReqwestQuery::from(query))
.send()
.await
.unwrap()
self.get_request_with_query(path, query).await
}

async fn post(&self, path: &str) -> Response {
Expand All @@ -149,6 +142,27 @@ impl Client {
format!("http://{}{}{path}", &self.connection_info.bind_address, &self.base_path)
}

pub async fn get_request_with_query(&self, path: &str, params: Query) -> Response {
reqwest::Client::builder()
.build()
.unwrap()
.get(self.base_url(path))
.query(&ReqwestQuery::from(params))
.send()
.await
.unwrap()
}

pub async fn get_request(&self, path: &str) -> Response {
reqwest::Client::builder()
.build()
.unwrap()
.get(self.base_url(path))
.send()
.await
.unwrap()
}

fn query_with_token(&self) -> Query {
match &self.connection_info.api_token {
Some(token) => Query::with_token(token),
Expand Down
74 changes: 74 additions & 0 deletions tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,80 @@ mod tracker_apis {
*/

mod authentication {
use crate::api::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::api::client::{Client, Query, QueryParam};
use crate::api::server::start_default_api;
use crate::api::Version;

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

let token = api_server.get_connection_info().api_token.unwrap();

let response = Client::new(api_server.get_connection_info())
.get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()))
.await;

assert_eq!(response.status(), 200);
}

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

let response = Client::new(api_server.get_connection_info())
.get_request_with_query("stats", Query::default())
.await;

assert_unauthorized(response).await;
}

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

let response = Client::new(api_server.get_connection_info())
.get_request_with_query("stats", Query::params([QueryParam::new("token", "")].to_vec()))
.await;

assert_token_not_valid(response).await;
}

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

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

assert_token_not_valid(response).await;
}

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

let token = api_server.get_connection_info().api_token.unwrap();

// At the beginning of the query component
let response = Client::new(api_server.get_connection_info())
.get_request(&format!("torrents?token={}&limit=1", &token))
.await;

assert_eq!(response.status(), 200);

// At the end of the query component
let response = Client::new(api_server.get_connection_info())
.get_request(&format!("torrents?limit=1&token={}", &token))
.await;

assert_eq!(response.status(), 200);
}
}

mod for_stats_resources {
use std::str::FromStr;

Expand Down

0 comments on commit 2da0719

Please sign in to comment.