@@ -202,18 +202,101 @@ mod test {
202
202
routing:: get,
203
203
Extension , Router ,
204
204
} ;
205
- use tower:: Service ;
205
+ use tower:: { Service , ServiceBuilder } ;
206
206
207
207
use adapter:: {
208
208
dummy:: { Dummy , HeaderToken } ,
209
209
ethereum:: test_util:: GANACHE_1 ,
210
210
} ;
211
- use primitives:: test_util:: { DUMMY_AUTH , LEADER } ;
211
+ use primitives:: test_util:: { ADVERTISER , DUMMY_AUTH , FOLLOWER , IDS , LEADER , PUBLISHER } ;
212
212
213
213
use crate :: { middleware:: body_to_string, test_util:: setup_dummy_app, Session } ;
214
214
215
215
use super :: * ;
216
216
217
+ #[ tokio:: test]
218
+ async fn test_is_admin ( ) {
219
+ let app_guard = setup_dummy_app ( ) . await ;
220
+ let app = Arc :: new ( app_guard. app ) ;
221
+
222
+ let admin = {
223
+ assert ! (
224
+ app. config. admins. contains( & LEADER ) ,
225
+ "Should contain the Leader as an Admin for this test!"
226
+ ) ;
227
+ IDS [ & LEADER ]
228
+ } ;
229
+
230
+ let not_admin = {
231
+ assert ! (
232
+ !app. config. admins. contains( & FOLLOWER ) ,
233
+ "Should not contain the Follower as an Admin for this test!"
234
+ ) ;
235
+
236
+ IDS [ & FOLLOWER ]
237
+ } ;
238
+
239
+ async fn handle ( ) -> String {
240
+ "Ok" . into ( )
241
+ }
242
+
243
+ let mut router = Router :: new ( )
244
+ . route ( "/" , get ( handle) )
245
+ . layer ( from_fn ( is_admin :: < Dummy , _ > ) ) ;
246
+
247
+ // No Auth - Unauthorized
248
+ {
249
+ let no_auth = Request :: builder ( )
250
+ . extension ( app. clone ( ) )
251
+ . body ( Body :: empty ( ) )
252
+ . expect ( "should never fail!" ) ;
253
+
254
+ let response = router
255
+ . call ( no_auth)
256
+ . await
257
+ . expect ( "Should make request to Router" ) ;
258
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
259
+ }
260
+
261
+ // Not an Admin - Unauthorized
262
+ {
263
+ let not_admin_request = Request :: builder ( )
264
+ . extension ( app. clone ( ) )
265
+ . extension ( Auth {
266
+ era : 1 ,
267
+ uid : not_admin,
268
+ chain : GANACHE_1 . clone ( ) ,
269
+ } )
270
+ . body ( Body :: empty ( ) )
271
+ . expect ( "should never fail!" ) ;
272
+
273
+ let response = router
274
+ . call ( not_admin_request)
275
+ . await
276
+ . expect ( "Should make request to Router" ) ;
277
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
278
+ }
279
+
280
+ // An Admin - Ok
281
+ {
282
+ let not_admin_request = Request :: builder ( )
283
+ . extension ( app. clone ( ) )
284
+ . extension ( Auth {
285
+ era : 1 ,
286
+ uid : admin,
287
+ chain : GANACHE_1 . clone ( ) ,
288
+ } )
289
+ . body ( Body :: empty ( ) )
290
+ . expect ( "should never fail!" ) ;
291
+
292
+ let response = router
293
+ . call ( not_admin_request)
294
+ . await
295
+ . expect ( "Should make request to Router" ) ;
296
+ assert_eq ! ( StatusCode :: OK , response. status( ) ) ;
297
+ }
298
+ }
299
+
217
300
#[ tokio:: test]
218
301
async fn no_authentication_or_incorrect_value_should_not_add_session ( ) {
219
302
let app_guard = setup_dummy_app ( ) . await ;
@@ -383,4 +466,192 @@ mod test {
383
466
assert_eq ! ( Some ( "192.168.0.1" . to_string( ) ) , actual_ips) ;
384
467
}
385
468
}
469
+
470
+ #[ tokio:: test]
471
+ async fn test_authenticate_as_advertiser_and_publisher ( ) {
472
+ let build_request = |auth : Option < Auth > | {
473
+ let mut request = Request :: builder ( ) . uri ( format ! ( "/" ) ) ;
474
+
475
+ if let Some ( auth) = auth {
476
+ request = request. extension ( auth) ;
477
+ }
478
+
479
+ request. body ( Body :: empty ( ) ) . expect ( "Should build Request" )
480
+ } ;
481
+
482
+ #[ derive( Debug , Clone , Copy ) ]
483
+ pub enum TestFor {
484
+ Advertiser ,
485
+ Publisher ,
486
+ }
487
+
488
+ async fn handle (
489
+ Extension ( auth_as) : Extension < AuthenticateAs > ,
490
+ Extension ( test_for) : Extension < TestFor > ,
491
+ ) -> StatusCode {
492
+ match ( auth_as, test_for) {
493
+ ( AuthenticateAs :: Advertiser ( _addr) , TestFor :: Advertiser )
494
+ | ( AuthenticateAs :: Publisher ( _addr) , TestFor :: Publisher ) => StatusCode :: OK ,
495
+ _ => StatusCode :: BAD_REQUEST ,
496
+ }
497
+ }
498
+
499
+ // Advertiser
500
+ {
501
+ let mut router = Router :: new ( ) . route (
502
+ "/" ,
503
+ get ( handle) . route_layer (
504
+ ServiceBuilder :: new ( )
505
+ . layer ( Extension ( TestFor :: Advertiser ) )
506
+ . layer ( from_fn ( authenticate_as_advertiser) ) ,
507
+ ) ,
508
+ ) ;
509
+
510
+ // No Auth, should return Unauthorized
511
+ {
512
+ let request = build_request ( None ) ;
513
+
514
+ let response = router
515
+ . call ( request)
516
+ . await
517
+ . expect ( "Should make request to Router" ) ;
518
+
519
+ assert_eq ! (
520
+ StatusCode :: UNAUTHORIZED ,
521
+ response. status( ) ,
522
+ "Should be unauthorized"
523
+ )
524
+ }
525
+
526
+ // AuthenticateAs::Advertiser - OK
527
+ {
528
+ let request = build_request ( Some ( Auth {
529
+ era : 1 ,
530
+ uid : IDS [ & ADVERTISER ] ,
531
+ chain : GANACHE_1 . clone ( ) ,
532
+ } ) ) ;
533
+
534
+ let response = router
535
+ . call ( request)
536
+ . await
537
+ . expect ( "Should make request to Router" ) ;
538
+
539
+ assert_eq ! (
540
+ StatusCode :: OK ,
541
+ response. status( ) ,
542
+ "Should be authenticated as Advertiser!"
543
+ )
544
+ }
545
+ }
546
+
547
+ // Publisher
548
+ {
549
+ let mut router = Router :: new ( ) . route (
550
+ "/" ,
551
+ get ( handle) . route_layer (
552
+ ServiceBuilder :: new ( )
553
+ . layer ( Extension ( TestFor :: Publisher ) )
554
+ . layer ( from_fn ( authenticate_as_publisher) ) ,
555
+ ) ,
556
+ ) ;
557
+
558
+ // No Auth, should return Unauthorized
559
+ {
560
+ let request = build_request ( None ) ;
561
+
562
+ let response = router
563
+ . call ( request)
564
+ . await
565
+ . expect ( "Should make request to Router" ) ;
566
+
567
+ assert_eq ! (
568
+ StatusCode :: UNAUTHORIZED ,
569
+ response. status( ) ,
570
+ "Should be unauthorized"
571
+ )
572
+ }
573
+
574
+ // AuthenticateAs::Publisher - OK
575
+ {
576
+ let request = build_request ( Some ( Auth {
577
+ era : 1 ,
578
+ uid : IDS [ & PUBLISHER ] ,
579
+ chain : GANACHE_1 . clone ( ) ,
580
+ } ) ) ;
581
+
582
+ let response = router
583
+ . call ( request)
584
+ . await
585
+ . expect ( "Should make request to Router" ) ;
586
+
587
+ assert_eq ! (
588
+ StatusCode :: OK ,
589
+ response. status( ) ,
590
+ "Should be authenticated as Publisher!"
591
+ )
592
+ }
593
+ }
594
+ }
595
+
596
+ #[ tokio:: test]
597
+ #[ should_panic]
598
+ async fn test_authenticate_as_advertiser_panic ( ) {
599
+ async fn handle ( Extension ( _auth_as) : Extension < AuthenticateAs > ) -> String {
600
+ "It should have panicked at this point" . into ( )
601
+ }
602
+
603
+ let mut router = Router :: new ( ) . route (
604
+ "/" ,
605
+ get ( handle) . route_layer ( from_fn ( authenticate_as_advertiser) ) ,
606
+ ) ;
607
+
608
+ let auth = Auth {
609
+ era : 1 ,
610
+ uid : IDS [ & ADVERTISER ] ,
611
+ chain : GANACHE_1 . clone ( ) ,
612
+ } ;
613
+
614
+ let request = Request :: builder ( )
615
+ . uri ( format ! ( "/" ) )
616
+ . extension ( auth. clone ( ) )
617
+ . extension ( AuthenticateAs :: Advertiser ( auth. uid ) )
618
+ . body ( Body :: empty ( ) )
619
+ . expect ( "Should build Request" ) ;
620
+
621
+ let _response = router
622
+ . call ( request)
623
+ . await
624
+ . expect ( "Should make request to Router" ) ;
625
+ }
626
+
627
+ #[ tokio:: test]
628
+ #[ should_panic]
629
+ async fn test_authenticate_as_publisher_panic ( ) {
630
+ async fn handle ( Extension ( _auth_as) : Extension < AuthenticateAs > ) -> String {
631
+ "It should have panicked at this point" . into ( )
632
+ }
633
+
634
+ let mut router = Router :: new ( ) . route (
635
+ "/" ,
636
+ get ( handle) . route_layer ( from_fn ( authenticate_as_publisher) ) ,
637
+ ) ;
638
+
639
+ let auth = Auth {
640
+ era : 1 ,
641
+ uid : IDS [ & PUBLISHER ] ,
642
+ chain : GANACHE_1 . clone ( ) ,
643
+ } ;
644
+
645
+ let request = Request :: builder ( )
646
+ . uri ( format ! ( "/" ) )
647
+ . extension ( auth. clone ( ) )
648
+ . extension ( AuthenticateAs :: Publisher ( auth. uid ) )
649
+ . body ( Body :: empty ( ) )
650
+ . expect ( "Should build Request" ) ;
651
+
652
+ let _response = router
653
+ . call ( request)
654
+ . await
655
+ . expect ( "Should make request to Router" ) ;
656
+ }
386
657
}
0 commit comments