@@ -231,3 +231,136 @@ impl KeysHandler {
231
231
Ok ( ( ) )
232
232
}
233
233
}
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
+ }
0 commit comments