Skip to content

Commit

Permalink
test(api): [#143] add test for torrent not known response in GET /api…
Browse files Browse the repository at this point in the history
…/torrent/:info_hash endpoint
  • Loading branch information
josecelano committed Jan 9, 2023
1 parent 16d438d commit 2aebf9a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/apis/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl IntoResponse for AuthError {

(
StatusCode::INTERNAL_SERVER_ERROR,
[(header::CONTENT_TYPE, "text/plain")],
[(header::CONTENT_TYPE, "text/plain; charset=utf-8")],
body,
)
.into_response()
Expand Down
8 changes: 8 additions & 0 deletions tests/api/asserts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use reqwest::Response;

pub async fn assert_token_not_valid(response: Response) {
assert_eq!(response.status(), 500);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(
response.text().await.unwrap(),
"Unhandled rejection: Err { reason: \"token not valid\" }"
Expand All @@ -10,8 +11,15 @@ pub async fn assert_token_not_valid(response: Response) {

pub async fn assert_unauthorized(response: Response) {
assert_eq!(response.status(), 500);
assert_eq!(response.headers().get("content-type").unwrap(), "text/plain; charset=utf-8");
assert_eq!(
response.text().await.unwrap(),
"Unhandled rejection: Err { reason: \"unauthorized\" }"
);
}

pub async fn assert_torrent_not_known(response: Response) {
assert_eq!(response.status(), 200);
assert_eq!(response.headers().get("content-type").unwrap(), "application/json");
assert_eq!(response.text().await.unwrap(), "\"torrent not known\"");
}
15 changes: 14 additions & 1 deletion tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mod tracker_api {
use torrust_tracker::api::resource::torrent::{self, Torrent};
use torrust_tracker::protocol::info_hash::InfoHash;

use crate::api::asserts::{assert_token_not_valid, assert_unauthorized};
use crate::api::asserts::{assert_token_not_valid, assert_torrent_not_known, assert_unauthorized};
use crate::api::client::{Client, Query, QueryParam};
use crate::api::connection_info::{connection_with_invalid_token, connection_with_no_token};
use crate::api::fixtures::sample_peer;
Expand Down Expand Up @@ -251,6 +251,19 @@ mod tracker_api {
);
}

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

let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap();

let response = Client::new(api_server.get_connection_info(), &Version::Warp)
.get_torrent(&info_hash.to_string())
.await;

assert_torrent_not_known(response).await;
}

#[tokio::test]
async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
let api_server = start_default_api(&Version::Warp).await;
Expand Down

0 comments on commit 2aebf9a

Please sign in to comment.