Skip to content

Commit 25016e0

Browse files
committed
feat: [#146] return infohash after successfully uploading a torrent
1 parent 150e99e commit 25016e0

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/models/response.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct TokenResponse {
3232
#[derive(Serialize, Deserialize, Debug)]
3333
pub struct NewTorrentResponse {
3434
pub torrent_id: i64,
35+
pub info_hash: String,
3536
}
3637

3738
#[allow(clippy::module_name_repetitions)]

src/routes/torrent.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ pub async fn upload(req: HttpRequest, payload: Multipart, app_data: WebAppData)
133133

134134
// respond with the newly uploaded torrent id
135135
Ok(HttpResponse::Ok().json(OkResponse {
136-
data: NewTorrentResponse { torrent_id },
136+
data: NewTorrentResponse {
137+
torrent_id,
138+
info_hash: torrent_request.torrent.info_hash(),
139+
},
137140
}))
138141
}
139142

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

354357
Ok(HttpResponse::Ok().json(OkResponse {
355358
data: NewTorrentResponse {
356359
torrent_id: torrent_listing.torrent_id,
360+
info_hash: torrent_listing.info_hash,
357361
},
358362
}))
359363
}

tests/common/contexts/torrent/responses.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ pub struct UploadedTorrentResponse {
8484
#[derive(Deserialize, PartialEq, Debug)]
8585
pub struct UploadedTorrent {
8686
pub torrent_id: Id,
87+
pub info_hash: String,
8788
}
8889

8990
#[derive(Deserialize, PartialEq, Debug)]

tests/e2e/contexts/torrent/contract.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,18 @@ mod for_authenticated_users {
305305
let client = Client::authenticated(&env.server_socket_addr().unwrap(), &uploader.token);
306306

307307
let test_torrent = random_torrent();
308+
let infohash = test_torrent.infohash().clone();
308309

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

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

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

316+
assert_eq!(
317+
uploaded_torrent_response.data.info_hash.to_lowercase(),
318+
infohash.to_lowercase()
319+
);
321320
assert!(response.is_json_and_ok());
322321
}
323322

0 commit comments

Comments
 (0)