Skip to content

Commit

Permalink
feat: [torrust#146] return infohash after successfully uploading a to…
Browse files Browse the repository at this point in the history
…rrent
  • Loading branch information
josecelano committed May 16, 2023
1 parent 150e99e commit 25016e0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/models/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct TokenResponse {
#[derive(Serialize, Deserialize, Debug)]
pub struct NewTorrentResponse {
pub torrent_id: i64,
pub info_hash: String,
}

#[allow(clippy::module_name_repetitions)]
Expand Down
8 changes: 6 additions & 2 deletions src/routes/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData)

// respond with the newly uploaded torrent id
Ok(HttpResponse::Ok().json(OkResponse {
data: NewTorrentResponse { torrent_id },
data: NewTorrentResponse {
torrent_id,
info_hash: torrent_request.torrent.info_hash(),
},
}))
}

Expand Down Expand Up @@ -348,12 +351,13 @@ pub async fn delete(req: HttpRequest, app_data: WebAppData) -> ServiceResult<imp
// remove info_hash from tracker whitelist
let _ = app_data
.tracker_service
.remove_info_hash_from_whitelist(torrent_listing.info_hash)
.remove_info_hash_from_whitelist(torrent_listing.info_hash.clone())
.await;

Ok(HttpResponse::Ok().json(OkResponse {
data: NewTorrentResponse {
torrent_id: torrent_listing.torrent_id,
info_hash: torrent_listing.info_hash,
},
}))
}
Expand Down
1 change: 1 addition & 0 deletions tests/common/contexts/torrent/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub struct UploadedTorrentResponse {
#[derive(Deserialize, PartialEq, Debug)]
pub struct UploadedTorrent {
pub torrent_id: Id,
pub info_hash: String,
}

#[derive(Deserialize, PartialEq, Debug)]
Expand Down
13 changes: 6 additions & 7 deletions tests/e2e/contexts/torrent/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,18 @@ mod for_authenticated_users {
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);

let test_torrent = random_torrent();
let infohash = test_torrent.infohash().clone();

let form: UploadTorrentMultipartForm = test_torrent.index_info.into();

let response = client.upload_torrent(form.into()).await;

let _uploaded_torrent_response: UploadedTorrentResponse = serde_json::from_str(&response.body).unwrap();

// code-review: the response only returns the torrent autoincrement ID
// generated by the DB. So we can't assert that the torrent was uploaded.
// We could return the infohash.
// We are going to use the infohash to get the torrent. See issue:
// https://github.com/torrust/torrust-index-backend/issues/115
let uploaded_torrent_response: UploadedTorrentResponse = serde_json::from_str(&response.body).unwrap();

assert_eq!(
uploaded_torrent_response.data.info_hash.to_lowercase(),
infohash.to_lowercase()
);
assert!(response.is_json_and_ok());
}

Expand Down

0 comments on commit 25016e0

Please sign in to comment.