forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: [torrust#115] add E2E tests for torrent download with personal …
…announce url
- Loading branch information
1 parent
e8bf537
commit 32af56d
Showing
2 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,75 @@ | ||
use std::sync::Arc; | ||
|
||
use torrust_index_backend::databases::database::connect_database; | ||
use torrust_index_backend::models::torrent_file::Torrent; | ||
use torrust_index_backend::models::tracker_key::TrackerKey; | ||
|
||
use crate::common::contexts::user::responses::LoggedInUserData; | ||
use crate::e2e::environment::TestEnv; | ||
|
||
/// The backend does not generate exactly the same torrent that was uploaded. | ||
/// So we need to update the expected torrent to match the one generated by | ||
/// the backend. | ||
pub fn expected_torrent(mut uploaded_torrent: Torrent) -> Torrent { | ||
pub async fn expected_torrent(mut uploaded_torrent: Torrent, env: &TestEnv, downloader: &Option<LoggedInUserData>) -> Torrent { | ||
// code-review: The backend does not generate exactly the same torrent | ||
// that was uploaded and created by the `imdl` command-line tool. | ||
// So we need to update the expected torrent to match the one generated | ||
// by the backend. For some of them it makes sense (`announce` and `announce_list`), | ||
// for others it does not. | ||
|
||
let tracker_url = format!("{}", env.server_settings().unwrap().tracker.url); | ||
|
||
let tracker_key = match downloader { | ||
Some(logged_in_user) => get_user_tracker_key(logged_in_user, env).await, | ||
None => None, | ||
}; | ||
|
||
uploaded_torrent.info.private = Some(0); | ||
uploaded_torrent.announce = Some("udp://tracker:6969".to_string()); | ||
uploaded_torrent.announce = Some(build_announce_url(&tracker_url, &tracker_key)); | ||
uploaded_torrent.encoding = None; | ||
uploaded_torrent.announce_list = Some(vec![vec!["udp://tracker:6969".to_string()]]); | ||
uploaded_torrent.announce_list = Some(build_announce_list(&tracker_url, &tracker_key)); | ||
uploaded_torrent.creation_date = None; | ||
uploaded_torrent.created_by = None; | ||
|
||
uploaded_torrent | ||
} | ||
|
||
async fn get_user_tracker_key(logged_in_user: &LoggedInUserData, env: &TestEnv) -> Option<TrackerKey> { | ||
// code-review: could we add a new endpoint to get the user's tracker key? | ||
// `/user/keys/recent` or `/user/keys/latest | ||
// We could use that endpoint to get the user's tracker key instead of | ||
// querying the database. | ||
|
||
let database = Arc::new( | ||
connect_database(&env.database_connect_url().unwrap()) | ||
.await | ||
.expect("Database error."), | ||
); | ||
|
||
// Get the logged-in user id | ||
let user_profile = database | ||
.get_user_profile_from_username(&logged_in_user.username) | ||
.await | ||
.unwrap(); | ||
|
||
// Get the user's tracker key | ||
let tracker_key = database.get_user_tracker_key(user_profile.user_id).await.unwrap(); | ||
|
||
Some(tracker_key) | ||
} | ||
|
||
fn build_announce_url(tracker_url: &str, tracker_key: &Option<TrackerKey>) -> String { | ||
if let Some(key) = &tracker_key { | ||
format!("{tracker_url}/{}", key.key) | ||
} else { | ||
format!("{tracker_url}") | ||
} | ||
} | ||
|
||
fn build_announce_list(tracker_url: &str, tracker_key: &Option<TrackerKey>) -> Vec<Vec<String>> { | ||
if let Some(key) = &tracker_key { | ||
vec![vec![format!("{tracker_url}/{}", key.key)], vec![format!("{tracker_url}")]] | ||
} else { | ||
vec![vec![format!("{tracker_url}")]] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters