Skip to content

Commit

Permalink
fix(api): [#143] do not fail trying to remove a whitelisted torrent t…
Browse files Browse the repository at this point in the history
…wice

Previous behavior:

When you try to remove a non exisinting whitelisted torrent the response
is 500 error.

New bahavior:

The enpoints checks if the torrent is included in the whitelist. If it
does not, then it ignores the reqeust returning a 200 code.

It should return a 204 or 404 but the current API only uses these codes:
200, 400, 405, 500. In the new API version we are planning to refctor
all endpoints.
  • Loading branch information
josecelano committed Jan 12, 2023
1 parent 3bcbbc9 commit aa2a2ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,30 @@ impl Tracker {
///
/// Will return a `database::Error` if unable to remove the `info_hash` from the whitelist database.
pub async fn remove_torrent_from_whitelist(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> {
self.remove_torrent_from_database_whitelist(info_hash).await?;
self.remove_torrent_from_memory_whitelist(info_hash).await;
Ok(())
}

/// # Errors
///
/// Will return a `database::Error` if unable to remove the `info_hash` from the whitelist database.
pub async fn remove_torrent_from_database_whitelist(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> {
let is_whitelisted = self.database.is_info_hash_whitelisted(info_hash).await?;

if !is_whitelisted {
return Ok(());
}

self.database.remove_info_hash_from_whitelist(*info_hash).await?;
self.whitelist.write().await.remove(info_hash);

Ok(())
}

pub async fn remove_torrent_from_memory_whitelist(&self, info_hash: &InfoHash) -> bool {
self.whitelist.write().await.remove(info_hash)
}

pub async fn is_info_hash_whitelisted(&self, info_hash: &InfoHash) -> bool {
self.whitelist.read().await.contains(info_hash)
}
Expand Down
26 changes: 26 additions & 0 deletions tests/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,19 @@ mod tracker_api {
assert!(!api_server.tracker.is_info_hash_whitelisted(&info_hash).await);
}

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

let non_whitelisted_torrent_hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();

let response = Client::new(api_server.get_connection_info())
.remove_torrent_from_whitelist(&non_whitelisted_torrent_hash)
.await;

assert_ok(response).await;
}

#[tokio::test]
async fn should_fail_when_the_torrent_cannot_be_removed_from_the_whitelist() {
let api_server = start_default_api(&Version::Warp).await;
Expand Down Expand Up @@ -1208,6 +1221,19 @@ mod tracker_apis {
assert!(!api_server.tracker.is_info_hash_whitelisted(&info_hash).await);
}

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

let non_whitelisted_torrent_hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();

let response = Client::new(api_server.get_connection_info())
.remove_torrent_from_whitelist(&non_whitelisted_torrent_hash)
.await;

assert_ok(response).await;
}

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

0 comments on commit aa2a2ef

Please sign in to comment.