Skip to content

Commit 9c61b26

Browse files
committed
refactor: [#1195] move tests to KeysHandler
These tests do not require the authentication service.
1 parent c06da07 commit 9c61b26

File tree

2 files changed

+133
-64
lines changed

2 files changed

+133
-64
lines changed

src/core/authentication/handler.rs

+133
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,136 @@ impl KeysHandler {
231231
Ok(())
232232
}
233233
}
234+
235+
#[cfg(test)]
236+
mod tests {
237+
238+
mod the_keys_handler_when_tracker_is_configured_as_private {
239+
240+
use std::sync::Arc;
241+
242+
use torrust_tracker_configuration::v2_0_0::core::PrivateMode;
243+
use torrust_tracker_configuration::Configuration;
244+
use torrust_tracker_test_helpers::configuration;
245+
246+
use crate::core::authentication::handler::KeysHandler;
247+
use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository;
248+
use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository;
249+
use crate::core::services::initialize_database;
250+
251+
fn instantiate_keys_handler() -> KeysHandler {
252+
let config = configuration::ephemeral_private();
253+
254+
instantiate_keys_handler_with_configuration(&config)
255+
}
256+
257+
#[allow(dead_code)]
258+
fn instantiate_keys_handler_with_checking_keys_expiration_disabled() -> KeysHandler {
259+
let mut config = configuration::ephemeral_private();
260+
261+
config.core.private_mode = Some(PrivateMode {
262+
check_keys_expiration: false,
263+
});
264+
265+
instantiate_keys_handler_with_configuration(&config)
266+
}
267+
268+
fn instantiate_keys_handler_with_configuration(config: &Configuration) -> KeysHandler {
269+
let database = initialize_database(config);
270+
271+
let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database));
272+
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
273+
274+
KeysHandler::new(&db_key_repository, &in_memory_key_repository)
275+
}
276+
277+
mod with_expiring_and {
278+
279+
mod randomly_generated_keys {
280+
use std::time::Duration;
281+
282+
use torrust_tracker_clock::clock::Time;
283+
284+
use crate::core::authentication::handler::tests::the_keys_handler_when_tracker_is_configured_as_private::instantiate_keys_handler;
285+
use crate::CurrentClock;
286+
287+
#[tokio::test]
288+
async fn it_should_generate_the_key() {
289+
let keys_handler = instantiate_keys_handler();
290+
291+
let peer_key = keys_handler.generate_auth_key(Some(Duration::from_secs(100))).await.unwrap();
292+
293+
assert_eq!(
294+
peer_key.valid_until,
295+
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
296+
);
297+
}
298+
}
299+
300+
mod pre_generated_keys {
301+
use std::time::Duration;
302+
303+
use torrust_tracker_clock::clock::Time;
304+
305+
use crate::core::authentication::handler::tests::the_keys_handler_when_tracker_is_configured_as_private::instantiate_keys_handler;
306+
use crate::core::authentication::{AddKeyRequest, Key};
307+
use crate::CurrentClock;
308+
309+
#[tokio::test]
310+
async fn it_should_add_a_pre_generated_key() {
311+
let keys_handler = instantiate_keys_handler();
312+
313+
let peer_key = keys_handler
314+
.add_peer_key(AddKeyRequest {
315+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
316+
opt_seconds_valid: Some(100),
317+
})
318+
.await
319+
.unwrap();
320+
321+
assert_eq!(
322+
peer_key.valid_until,
323+
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
324+
);
325+
}
326+
}
327+
}
328+
329+
mod with_permanent_and {
330+
331+
mod randomly_generated_keys {
332+
use crate::core::authentication::handler::tests::the_keys_handler_when_tracker_is_configured_as_private::instantiate_keys_handler;
333+
334+
#[tokio::test]
335+
async fn it_should_generate_the_key() {
336+
let keys_handler = instantiate_keys_handler();
337+
338+
let peer_key = keys_handler.generate_permanent_auth_key().await.unwrap();
339+
340+
assert_eq!(peer_key.valid_until, None);
341+
}
342+
}
343+
344+
mod pre_generated_keys {
345+
346+
use crate::core::authentication::handler::tests::the_keys_handler_when_tracker_is_configured_as_private::instantiate_keys_handler;
347+
use crate::core::authentication::{AddKeyRequest, Key};
348+
349+
#[tokio::test]
350+
async fn it_should_add_a_pre_generated_key() {
351+
let keys_handler = instantiate_keys_handler();
352+
353+
let peer_key = keys_handler
354+
.add_peer_key(AddKeyRequest {
355+
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
356+
opt_seconds_valid: None,
357+
})
358+
.await
359+
.unwrap();
360+
361+
assert_eq!(peer_key.valid_until, None);
362+
}
363+
}
364+
}
365+
}
366+
}

src/core/authentication/mod.rs

-64
Original file line numberDiff line numberDiff line change
@@ -221,28 +221,10 @@ mod tests {
221221
mod randomly_generated_keys {
222222
use std::time::Duration;
223223

224-
use torrust_tracker_clock::clock::Time;
225-
226224
use crate::core::authentication::tests::the_tracker_configured_as_private::{
227225
instantiate_authentication, instantiate_authentication_with_checking_keys_expiration_disabled,
228226
};
229227
use crate::core::authentication::Key;
230-
use crate::CurrentClock;
231-
232-
#[tokio::test]
233-
async fn it_should_generate_the_key() {
234-
let authentication = instantiate_authentication();
235-
236-
let peer_key = authentication
237-
.generate_auth_key(Some(Duration::from_secs(100)))
238-
.await
239-
.unwrap();
240-
241-
assert_eq!(
242-
peer_key.valid_until,
243-
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
244-
);
245-
}
246228

247229
#[tokio::test]
248230
async fn it_should_authenticate_a_peer_with_the_key() {
@@ -274,33 +256,11 @@ mod tests {
274256
}
275257

276258
mod pre_generated_keys {
277-
use std::time::Duration;
278-
279-
use torrust_tracker_clock::clock::Time;
280259

281260
use crate::core::authentication::tests::the_tracker_configured_as_private::{
282261
instantiate_authentication, instantiate_authentication_with_checking_keys_expiration_disabled,
283262
};
284263
use crate::core::authentication::{AddKeyRequest, Key};
285-
use crate::CurrentClock;
286-
287-
#[tokio::test]
288-
async fn it_should_add_a_pre_generated_key() {
289-
let authentication = instantiate_authentication();
290-
291-
let peer_key = authentication
292-
.add_peer_key(AddKeyRequest {
293-
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
294-
opt_seconds_valid: Some(100),
295-
})
296-
.await
297-
.unwrap();
298-
299-
assert_eq!(
300-
peer_key.valid_until,
301-
Some(CurrentClock::now_add(&Duration::from_secs(100)).unwrap())
302-
);
303-
}
304264

305265
#[tokio::test]
306266
async fn it_should_authenticate_a_peer_with_the_key() {
@@ -341,15 +301,6 @@ mod tests {
341301
mod randomly_generated_keys {
342302
use crate::core::authentication::tests::the_tracker_configured_as_private::instantiate_authentication;
343303

344-
#[tokio::test]
345-
async fn it_should_generate_the_key() {
346-
let authentication = instantiate_authentication();
347-
348-
let peer_key = authentication.generate_permanent_auth_key().await.unwrap();
349-
350-
assert_eq!(peer_key.valid_until, None);
351-
}
352-
353304
#[tokio::test]
354305
async fn it_should_authenticate_a_peer_with_the_key() {
355306
let authentication = instantiate_authentication();
@@ -366,21 +317,6 @@ mod tests {
366317
use crate::core::authentication::tests::the_tracker_configured_as_private::instantiate_authentication;
367318
use crate::core::authentication::{AddKeyRequest, Key};
368319

369-
#[tokio::test]
370-
async fn it_should_add_a_pre_generated_key() {
371-
let authentication = instantiate_authentication();
372-
373-
let peer_key = authentication
374-
.add_peer_key(AddKeyRequest {
375-
opt_key: Some(Key::new("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap().to_string()),
376-
opt_seconds_valid: None,
377-
})
378-
.await
379-
.unwrap();
380-
381-
assert_eq!(peer_key.valid_until, None);
382-
}
383-
384320
#[tokio::test]
385321
async fn it_should_authenticate_a_peer_with_the_key() {
386322
let authentication = instantiate_authentication();

0 commit comments

Comments
 (0)