diff --git a/src/tracker/mod.rs b/src/tracker/mod.rs index bbf49e23..4f1dab49 100644 --- a/src/tracker/mod.rs +++ b/src/tracker/mod.rs @@ -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) } diff --git a/tests/tracker_api.rs b/tests/tracker_api.rs index ec4d1f2e..f959f67b 100644 --- a/tests/tracker_api.rs +++ b/tests/tracker_api.rs @@ -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; @@ -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;