Skip to content

Commit 9a60c0c

Browse files
committed
refactor: [#11191] copy private tracker tests to authentication module
1 parent a093680 commit 9a60c0c

File tree

1 file changed

+298
-0
lines changed

1 file changed

+298
-0
lines changed

src/core/authentication/mod.rs

+298
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,301 @@ impl Facade {
302302
Ok(())
303303
}
304304
}
305+
306+
#[cfg(test)]
307+
mod tests {
308+
309+
mod the_tracker {
310+
311+
use torrust_tracker_configuration::v2_0_0::core::PrivateMode;
312+
use torrust_tracker_test_helpers::configuration;
313+
314+
use crate::app_test::initialize_tracker_dependencies;
315+
use crate::core::services::initialize_tracker;
316+
use crate::core::Tracker;
317+
318+
fn private_tracker() -> Tracker {
319+
let config = configuration::ephemeral_private();
320+
321+
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
322+
initialize_tracker_dependencies(&config);
323+
324+
initialize_tracker(&config, &database, &whitelist_authorization, &authentication)
325+
}
326+
327+
fn private_tracker_without_checking_keys_expiration() -> Tracker {
328+
let mut config = configuration::ephemeral_private();
329+
330+
config.core.private_mode = Some(PrivateMode {
331+
check_keys_expiration: false,
332+
});
333+
334+
let (database, _in_memory_whitelist, whitelist_authorization, authentication) =
335+
initialize_tracker_dependencies(&config);
336+
337+
initialize_tracker(&config, &database, &whitelist_authorization, &authentication)
338+
}
339+
340+
mod configured_as_private {
341+
342+
mod handling_authentication {
343+
use std::str::FromStr;
344+
use std::time::Duration;
345+
346+
use crate::core::authentication::tests::the_tracker::private_tracker;
347+
use crate::core::authentication::{self};
348+
349+
#[tokio::test]
350+
async fn it_should_fail_authenticating_a_peer_when_it_uses_an_unregistered_key() {
351+
let tracker = private_tracker();
352+
353+
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
354+
355+
let result = tracker.authentication.authenticate(&unregistered_key).await;
356+
357+
assert!(result.is_err());
358+
}
359+
360+
#[tokio::test]
361+
async fn it_should_fail_verifying_an_unregistered_authentication_key() {
362+
let tracker = private_tracker();
363+
364+
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
365+
366+
assert!(tracker.authentication.verify_auth_key(&unregistered_key).await.is_err());
367+
}
368+
369+
#[tokio::test]
370+
async fn it_should_remove_an_authentication_key() {
371+
let tracker = private_tracker();
372+
373+
let expiring_key = tracker
374+
.authentication
375+
.generate_auth_key(Some(Duration::from_secs(100)))
376+
.await
377+
.unwrap();
378+
379+
let result = tracker.authentication.remove_auth_key(&expiring_key.key()).await;
380+
381+
assert!(result.is_ok());
382+
assert!(tracker.authentication.verify_auth_key(&expiring_key.key()).await.is_err());
383+
}
384+
385+
#[tokio::test]
386+
async fn it_should_load_authentication_keys_from_the_database() {
387+
let tracker = private_tracker();
388+
389+
let expiring_key = tracker
390+
.authentication
391+
.generate_auth_key(Some(Duration::from_secs(100)))
392+
.await
393+
.unwrap();
394+
395+
// Remove the newly generated key in memory
396+
tracker.authentication.remove_in_memory_auth_key(&expiring_key.key()).await;
397+
398+
let result = tracker.authentication.load_keys_from_database().await;
399+
400+
assert!(result.is_ok());
401+
assert!(tracker.authentication.verify_auth_key(&expiring_key.key()).await.is_ok());
402+
}
403+
404+
mod with_expiring_and {
405+
406+
mod randomly_generated_keys {
407+
use std::time::Duration;
408+
409+
use torrust_tracker_clock::clock::Time;
410+
411+
use crate::core::authentication::tests::the_tracker::{
412+
private_tracker, private_tracker_without_checking_keys_expiration,
413+
};
414+
use crate::core::authentication::Key;
415+
use crate::CurrentClock;
416+
417+
#[tokio::test]
418+
async fn it_should_generate_the_key() {
419+
let tracker = private_tracker();
420+
421+
let peer_key = tracker
422+
.authentication
423+
.generate_auth_key(Some(Duration::from_secs(100)))
424+
.await
425+
.unwrap();
426+
427+
assert_eq!(
428+
peer_key.valid_until,
429+
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
430+
);
431+
}
432+
433+
#[tokio::test]
434+
async fn it_should_authenticate_a_peer_with_the_key() {
435+
let tracker = private_tracker();
436+
437+
let peer_key = tracker
438+
.authentication
439+
.generate_auth_key(Some(Duration::from_secs(100)))
440+
.await
441+
.unwrap();
442+
443+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
444+
445+
assert!(result.is_ok());
446+
}
447+
448+
#[tokio::test]
449+
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
450+
let tracker = private_tracker_without_checking_keys_expiration();
451+
452+
let past_timestamp = Duration::ZERO;
453+
454+
let peer_key = tracker
455+
.authentication
456+
.add_auth_key(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(), Some(past_timestamp))
457+
.await
458+
.unwrap();
459+
460+
assert!(tracker.authentication.authenticate(&peer_key.key()).await.is_ok());
461+
}
462+
}
463+
464+
mod pre_generated_keys {
465+
use std::time::Duration;
466+
467+
use torrust_tracker_clock::clock::Time;
468+
469+
use crate::core::authentication::tests::the_tracker::{
470+
private_tracker, private_tracker_without_checking_keys_expiration,
471+
};
472+
use crate::core::authentication::{AddKeyRequest, Key};
473+
use crate::CurrentClock;
474+
475+
#[tokio::test]
476+
async fn it_should_add_a_pre_generated_key() {
477+
let tracker = private_tracker();
478+
479+
let peer_key = tracker
480+
.authentication
481+
.add_peer_key(AddKeyRequest {
482+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
483+
opt_seconds_valid: Some(100),
484+
})
485+
.await
486+
.unwrap();
487+
488+
assert_eq!(
489+
peer_key.valid_until,
490+
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
491+
);
492+
}
493+
494+
#[tokio::test]
495+
async fn it_should_authenticate_a_peer_with_the_key() {
496+
let tracker = private_tracker();
497+
498+
let peer_key = tracker
499+
.authentication
500+
.add_peer_key(AddKeyRequest {
501+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
502+
opt_seconds_valid: Some(100),
503+
})
504+
.await
505+
.unwrap();
506+
507+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
508+
509+
assert!(result.is_ok());
510+
}
511+
512+
#[tokio::test]
513+
async fn it_should_accept_an_expired_key_when_checking_expiration_is_disabled_in_configuration() {
514+
let tracker = private_tracker_without_checking_keys_expiration();
515+
516+
let peer_key = tracker
517+
.authentication
518+
.add_peer_key(AddKeyRequest {
519+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
520+
opt_seconds_valid: Some(0),
521+
})
522+
.await
523+
.unwrap();
524+
525+
assert!(tracker.authentication.authenticate(&peer_key.key()).await.is_ok());
526+
}
527+
}
528+
}
529+
530+
mod with_permanent_and {
531+
532+
mod randomly_generated_keys {
533+
use crate::core::authentication::tests::the_tracker::private_tracker;
534+
535+
#[tokio::test]
536+
async fn it_should_generate_the_key() {
537+
let tracker = private_tracker();
538+
539+
let peer_key = tracker.authentication.generate_permanent_auth_key().await.unwrap();
540+
541+
assert_eq!(peer_key.valid_until, None);
542+
}
543+
544+
#[tokio::test]
545+
async fn it_should_authenticate_a_peer_with_the_key() {
546+
let tracker = private_tracker();
547+
548+
let peer_key = tracker.authentication.generate_permanent_auth_key().await.unwrap();
549+
550+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
551+
552+
assert!(result.is_ok());
553+
}
554+
}
555+
556+
mod pre_generated_keys {
557+
use crate::core::authentication::tests::the_tracker::private_tracker;
558+
use crate::core::authentication::{AddKeyRequest, Key};
559+
560+
#[tokio::test]
561+
async fn it_should_add_a_pre_generated_key() {
562+
let tracker = private_tracker();
563+
564+
let peer_key = tracker
565+
.authentication
566+
.add_peer_key(AddKeyRequest {
567+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
568+
opt_seconds_valid: None,
569+
})
570+
.await
571+
.unwrap();
572+
573+
assert_eq!(peer_key.valid_until, None);
574+
}
575+
576+
#[tokio::test]
577+
async fn it_should_authenticate_a_peer_with_the_key() {
578+
let tracker = private_tracker();
579+
580+
let peer_key = tracker
581+
.authentication
582+
.add_peer_key(AddKeyRequest {
583+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
584+
opt_seconds_valid: None,
585+
})
586+
.await
587+
.unwrap();
588+
589+
let result = tracker.authentication.authenticate(&peer_key.key()).await;
590+
591+
assert!(result.is_ok());
592+
}
593+
}
594+
}
595+
}
596+
597+
mod handling_an_announce_request {}
598+
599+
mod handling_an_scrape_request {}
600+
}
601+
}
602+
}

0 commit comments

Comments
 (0)