@@ -217,3 +217,167 @@ pub fn analytics_router<C: Locked + 'static>() -> Router {
217
217
)
218
218
. merge ( authenticated_analytics)
219
219
}
220
+
221
+ #[ cfg( test) ]
222
+ mod test {
223
+ use super :: * ;
224
+ use crate :: { test_util:: { setup_dummy_app} , Auth } ;
225
+ use axum:: { body:: Body , http:: { Request , StatusCode } } ;
226
+ use primitives:: test_util:: { PUBLISHER , ADVERTISER , IDS , LEADER , FOLLOWER } ;
227
+ use adapter:: ethereum:: test_util:: GANACHE_1 ;
228
+ use tower:: { Service } ;
229
+
230
+ #[ tokio:: test]
231
+ async fn analytics_router_tests ( ) {
232
+ let mut router = analytics_router :: < Dummy > ( ) ;
233
+ let app_guard = setup_dummy_app ( ) . await ;
234
+ let app = Arc :: new ( app_guard. app ) ;
235
+
236
+ // Test /for-publisher with no auth
237
+ {
238
+ let req = Request :: builder ( )
239
+ . uri ( "/for-publisher" )
240
+ . extension ( app. clone ( ) )
241
+ . body ( Body :: empty ( ) )
242
+ . expect ( "Should build Request" ) ;
243
+
244
+ let response = router
245
+ . call ( req)
246
+ . await
247
+ . expect ( "Should make request to Router" ) ;
248
+
249
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
250
+ }
251
+ // Test /for-publisher with auth
252
+ {
253
+ let req = Request :: builder ( )
254
+ . uri ( "/for-publisher" )
255
+ . extension ( app. clone ( ) )
256
+ . extension ( Auth {
257
+ era : 1 ,
258
+ uid : IDS [ & PUBLISHER ] ,
259
+ chain : GANACHE_1 . clone ( ) ,
260
+ } )
261
+ . body ( Body :: empty ( ) )
262
+ . expect ( "Should build Request" ) ;
263
+
264
+ let response = router
265
+ . call ( req)
266
+ . await
267
+ . expect ( "Should make request to Router" ) ;
268
+
269
+ assert_eq ! ( StatusCode :: OK , response. status( ) ) ;
270
+ }
271
+ // Test /for-advertiser with no auth
272
+ {
273
+ let req = Request :: builder ( )
274
+ . uri ( "/for-advertiser" )
275
+ . extension ( app. clone ( ) )
276
+ . body ( Body :: empty ( ) )
277
+ . expect ( "Should build Request" ) ;
278
+
279
+ let response = router
280
+ . call ( req)
281
+ . await
282
+ . expect ( "Should make request to Router" ) ;
283
+
284
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
285
+ }
286
+ // Test /for-advertiser with auth
287
+ {
288
+ let req = Request :: builder ( )
289
+ . uri ( "/for-advertiser" )
290
+ . extension ( app. clone ( ) )
291
+ . extension ( Auth {
292
+ era : 1 ,
293
+ uid : IDS [ & ADVERTISER ] ,
294
+ chain : GANACHE_1 . clone ( ) ,
295
+ } )
296
+ . body ( Body :: empty ( ) )
297
+ . expect ( "Should build Request" ) ;
298
+
299
+ let response = router
300
+ . call ( req)
301
+ . await
302
+ . expect ( "Should make request to Router" ) ;
303
+
304
+ assert_eq ! ( StatusCode :: OK , response. status( ) ) ;
305
+ }
306
+ // Test /for-admin with no auth
307
+ {
308
+ let req = Request :: builder ( )
309
+ . uri ( "/for-admin" )
310
+ . extension ( app. clone ( ) )
311
+ . body ( Body :: empty ( ) )
312
+ . expect ( "Should build Request" ) ;
313
+
314
+ let response = router
315
+ . call ( req)
316
+ . await
317
+ . expect ( "Should make request to Router" ) ;
318
+
319
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
320
+ }
321
+ // Test /for-admin with wrong auth
322
+ {
323
+ let not_admin = {
324
+ assert ! (
325
+ !app. config. admins. contains( & FOLLOWER ) ,
326
+ "Should not contain the Follower as an Admin for this test!"
327
+ ) ;
328
+
329
+ IDS [ & FOLLOWER ]
330
+ } ;
331
+ let req = Request :: builder ( )
332
+ . uri ( "/for-admin" )
333
+ . extension ( app. clone ( ) )
334
+ . extension ( Auth {
335
+ era : 1 ,
336
+ uid : not_admin,
337
+ chain : GANACHE_1 . clone ( ) ,
338
+ } )
339
+ . body ( Body :: empty ( ) )
340
+ . expect ( "Should build Request" ) ;
341
+
342
+ let response = router
343
+ . call ( req)
344
+ . await
345
+ . expect ( "Should make request to Router" ) ;
346
+
347
+ assert_eq ! ( StatusCode :: UNAUTHORIZED , response. status( ) ) ;
348
+ }
349
+ // Test /for-admin with correct auth
350
+ {
351
+ let admin = {
352
+ assert ! (
353
+ app. config. admins. contains( & LEADER ) ,
354
+ "Should contain the Leader as an Admin for this test!"
355
+ ) ;
356
+ IDS [ & LEADER ]
357
+ } ;
358
+ let req = Request :: builder ( )
359
+ . uri ( "/for-admin" )
360
+ . extension ( app. clone ( ) )
361
+ . extension ( Auth {
362
+ era : 1 ,
363
+ uid : admin,
364
+ chain : GANACHE_1 . clone ( ) ,
365
+ } )
366
+ . body ( Body :: empty ( ) )
367
+ . expect ( "Should build Request" ) ;
368
+
369
+ let response = router
370
+ . call ( req)
371
+ . await
372
+ . expect ( "Should make request to Router" ) ;
373
+
374
+ assert_eq ! ( StatusCode :: OK , response. status( ) ) ;
375
+ }
376
+ }
377
+
378
+ #[ tokio:: test]
379
+ async fn guest_route_allowed_key_tests ( ) {
380
+ // TODO: Test each allowed key
381
+ // TODO: Test each not allowed key
382
+ }
383
+ }
0 commit comments