Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: improve REST API client #1335

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn should_authenticate_requests_by_using_a_token_query_param() {
let token = env.get_connection_info().api_token.unwrap();

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

Expand All @@ -33,6 +34,7 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {
let request_id = Uuid::new_v4();

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

Expand All @@ -55,6 +57,7 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_request_with_query(
"stats",
Query::params([QueryParam::new("token", "")].to_vec()),
Expand All @@ -81,6 +84,7 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_request_with_query(
"stats",
Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()),
Expand Down Expand Up @@ -108,13 +112,15 @@ async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_que

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async fn should_allow_generating_a_new_random_auth_key() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.add_auth_key(
AddKeyForm {
opt_key: None,
Expand Down Expand Up @@ -56,6 +57,7 @@ async fn should_allow_uploading_a_preexisting_auth_key() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.add_auth_key(
AddKeyForm {
opt_key: Some("Xc1L4PbQJSFGlrgSRZl8wxSFAuMa21z5".to_string()),
Expand Down Expand Up @@ -87,6 +89,7 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.add_auth_key(
AddKeyForm {
opt_key: None,
Expand All @@ -106,6 +109,7 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.add_auth_key(
AddKeyForm {
opt_key: None,
Expand Down Expand Up @@ -136,6 +140,7 @@ async fn should_fail_when_the_auth_key_cannot_be_generated() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.add_auth_key(
AddKeyForm {
opt_key: None,
Expand Down Expand Up @@ -173,6 +178,7 @@ async fn should_allow_deleting_an_auth_key() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -207,6 +213,7 @@ async fn should_fail_generating_a_new_auth_key_when_the_provided_key_is_invalid(
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.post_form(
"keys",
&InvalidAddKeyForm {
Expand Down Expand Up @@ -246,6 +253,7 @@ async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid(
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.post_form(
"keys",
&InvalidAddKeyForm {
Expand Down Expand Up @@ -282,6 +290,7 @@ async fn should_fail_deleting_an_auth_key_when_the_key_id_is_invalid() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.delete_auth_key(invalid_auth_key, Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -311,6 +320,7 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -344,6 +354,7 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -366,6 +377,7 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -396,6 +408,7 @@ async fn should_allow_reloading_keys() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.reload_keys(Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -423,6 +436,7 @@ async fn should_fail_when_keys_cannot_be_reloaded() {
force_database_error(&env.container.tracker_core_container.database);

let response = Client::new(env.get_connection_info())
.unwrap()
.reload_keys(Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -453,6 +467,7 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.reload_keys(Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -466,6 +481,7 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.reload_keys(Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -504,6 +520,7 @@ mod deprecated_generate_key_endpoint {
let seconds_valid = 60;

let response = Client::new(env.get_connection_info())
.unwrap()
.generate_auth_key(seconds_valid, None)
.await;

Expand All @@ -530,12 +547,14 @@ mod deprecated_generate_key_endpoint {
let seconds_valid = 60;

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.generate_auth_key(seconds_valid, Some(headers_with_request_id(request_id)))
.await;

assert_token_not_valid(response).await;

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.generate_auth_key(seconds_valid, None)
.await;

Expand Down Expand Up @@ -563,6 +582,7 @@ mod deprecated_generate_key_endpoint {

for invalid_key_duration in invalid_key_durations {
let response = Client::new(env.get_connection_info())
.unwrap()
.post_empty(&format!("key/{invalid_key_duration}"), None)
.await;

Expand All @@ -583,6 +603,7 @@ mod deprecated_generate_key_endpoint {
let request_id = Uuid::new_v4();
let seconds_valid = 60;
let response = Client::new(env.get_connection_info())
.unwrap()
.generate_auth_key(seconds_valid, Some(headers_with_request_id(request_id)))
.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async fn should_allow_getting_tracker_statistics() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -80,6 +81,7 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -93,6 +95,7 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ async fn should_allow_getting_all_torrents() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(Query::empty(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -64,6 +65,7 @@ async fn should_allow_limiting_the_torrents_in_the_result() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params([QueryParam::new("limit", "1")].to_vec()),
Some(headers_with_request_id(request_id)),
Expand Down Expand Up @@ -100,6 +102,7 @@ async fn should_allow_the_torrents_result_pagination() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params([QueryParam::new("offset", "1")].to_vec()),
Some(headers_with_request_id(request_id)),
Expand Down Expand Up @@ -135,6 +138,7 @@ async fn should_allow_getting_a_list_of_torrents_providing_infohashes() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params(
[
Expand Down Expand Up @@ -181,6 +185,7 @@ async fn should_fail_getting_torrents_when_the_offset_query_parameter_cannot_be_
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params([QueryParam::new("offset", invalid_offset)].to_vec()),
Some(headers_with_request_id(request_id)),
Expand Down Expand Up @@ -209,6 +214,7 @@ async fn should_fail_getting_torrents_when_the_limit_query_parameter_cannot_be_p
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params([QueryParam::new("limit", invalid_limit)].to_vec()),
Some(headers_with_request_id(request_id)),
Expand Down Expand Up @@ -237,6 +243,7 @@ async fn should_fail_getting_torrents_when_the_info_hash_parameter_is_invalid()
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrents(
Query::params([QueryParam::new("info_hash", invalid_info_hash)].to_vec()),
Some(headers_with_request_id(request_id)),
Expand All @@ -262,6 +269,7 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.get_torrents(Query::empty(), Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -275,6 +283,7 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.get_torrents(Query::default(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -303,6 +312,7 @@ async fn should_allow_getting_a_torrent_info() {
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand Down Expand Up @@ -331,6 +341,7 @@ async fn should_fail_while_getting_a_torrent_info_when_the_torrent_does_not_exis
let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -349,6 +360,7 @@ async fn should_fail_getting_a_torrent_info_when_the_provided_infohash_is_invali
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrent(invalid_infohash, Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -359,6 +371,7 @@ async fn should_fail_getting_a_torrent_info_when_the_provided_infohash_is_invali
let request_id = Uuid::new_v4();

let response = Client::new(env.get_connection_info())
.unwrap()
.get_torrent(invalid_infohash, Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -381,6 +394,7 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
.unwrap()
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand All @@ -394,6 +408,7 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
let request_id = Uuid::new_v4();

let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
.unwrap()
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
.await;

Expand Down
Loading
Loading