@@ -11,56 +11,70 @@ import addUser from "../utils/addUser";
11
11
import * as impersonationModel from "../../models/impersonationRequests" ;
12
12
import * as validationService from "../../services/impersonationRequests" ;
13
13
import { CreateImpersonationRequestBody , ImpersonationRequest } from "../../types/impersonationRequest" ;
14
- import { REQUEST_CREATED_SUCCESSFULLY , REQUEST_STATE } from "../../constants/requests" ;
14
+ import { REQUEST_CREATED_SUCCESSFULLY , REQUEST_DOES_NOT_EXIST , REQUEST_STATE } from "../../constants/requests" ;
15
15
import { impersonationRequestsBodyData } from "../fixtures/impersonation-requests/impersonationRequests" ;
16
16
17
17
const { expect } = chai ;
18
18
const cookieName = config . get ( "userToken.cookieName" ) ;
19
19
const userData = userDataFixture ( ) ;
20
20
chai . use ( chaiHttp ) ;
21
21
22
- let testUserId : string ;
22
+ let authToken : string ;
23
+ let superUserToken : string ;
24
+ let requestsEndpoint : string ;
25
+ let testUserId1 : string ;
23
26
let testUserId2 : string ;
24
27
let testUserId3 : string ;
28
+ let testUserId4 : string ;
29
+ let testUserId5 : string ;
25
30
let testSuperUserId : string ;
26
- let authToken : string ;
27
- let superUserToken : string ;
28
31
let impersonationRequestBody : CreateImpersonationRequestBody ;
32
+ let impersonationRequest1 : ImpersonationRequest ;
29
33
30
34
describe ( "Impersonation Requests" , ( ) => {
31
- const requestsEndpoint : string = "/impersonation/requests?dev=true" ;
35
+ requestsEndpoint = "/impersonation/requests?dev=true" ;
32
36
33
37
beforeEach ( async ( ) => {
34
38
const userIdPromises = [
35
39
addUser ( userData [ 16 ] ) ,
36
- addUser ( userData [ 18 ] ) ,
40
+ addUser ( userData [ 19 ] ) ,
37
41
addUser ( userData [ 12 ] ) ,
42
+ addUser ( userData [ 0 ] ) ,
43
+ addUser ( userData [ 1 ] ) ,
38
44
addUser ( userData [ 4 ] )
39
45
] ;
40
- const [ userId1 , userId2 , userId3 , superUserId ] = await Promise . all ( userIdPromises ) ;
41
- testUserId = userId1 ;
42
- testUserId2 = userId2 ;
43
- testUserId3 = userId3 ;
44
- testSuperUserId = superUserId ;
46
+ [
47
+ testUserId1 ,
48
+ testUserId2 ,
49
+ testUserId3 ,
50
+ testUserId4 ,
51
+ testUserId5 ,
52
+ testSuperUserId
53
+ ] = await Promise . all ( userIdPromises ) ;
45
54
46
55
impersonationRequestBody = {
47
- impersonatedUserId : testUserId ,
56
+ impersonatedUserId : testUserId1 ,
48
57
reason : "User assistance required for account debugging."
49
58
} ;
50
59
51
- await impersonationModel . createImpersonationRequest ( {
60
+ impersonationRequest1 = await impersonationModel . createImpersonationRequest ( {
52
61
...impersonationRequestsBodyData [ 0 ] ,
53
62
impersonatedUserId : testUserId2 ,
54
- userId : superUserId ,
63
+ createdFor : userData [ 19 ] . username ,
64
+ userId : testSuperUserId ,
65
+ createdBy : userData [ 4 ] . username
55
66
} ) ;
67
+
56
68
await impersonationModel . createImpersonationRequest ( {
57
69
...impersonationRequestsBodyData [ 0 ] ,
58
70
impersonatedUserId : testUserId3 ,
59
- userId : superUserId ,
71
+ createdFor : userData [ 12 ] . username ,
72
+ createdBy : userData [ 4 ] . username ,
73
+ userId : testSuperUserId ,
60
74
status : REQUEST_STATE . APPROVED
61
75
} ) ;
62
76
63
- authToken = authService . generateAuthToken ( { userId : testUserId } ) ;
77
+ authToken = authService . generateAuthToken ( { userId : testUserId1 } ) ;
64
78
superUserToken = authService . generateAuthToken ( { userId : testSuperUserId } ) ;
65
79
} ) ;
66
80
@@ -264,4 +278,306 @@ describe("Impersonation Requests", () => {
264
278
} ) ;
265
279
} ) ;
266
280
} ) ;
281
+
282
+ describe ( "GET /impersonation/requests" , function ( ) {
283
+ beforeEach ( async ( ) => {
284
+ await impersonationModel . createImpersonationRequest ( {
285
+ ...impersonationRequestsBodyData [ 3 ] ,
286
+ impersonatedUserId : testUserId4 ,
287
+ createdFor : userData [ 0 ] . username ,
288
+ userId : testSuperUserId ,
289
+ status : REQUEST_STATE . REJECTED ,
290
+ createdBy : userData [ 4 ] . username
291
+ } ) ;
292
+
293
+ await impersonationModel . createImpersonationRequest ( {
294
+ ...impersonationRequestsBodyData [ 4 ] ,
295
+ impersonatedUserId : testUserId5 ,
296
+ createdFor : userData [ 1 ] . username ,
297
+ userId : testSuperUserId ,
298
+ status : REQUEST_STATE . REJECTED ,
299
+ createdBy : userData [ 4 ] . username
300
+ } ) ;
301
+ } ) ;
302
+
303
+ it ( "should return 404 and 'Route not found' message when dev is false" , function ( done ) {
304
+ chai
305
+ . request ( app )
306
+ . get ( "/impersonation/requests?dev=false" )
307
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
308
+ . end ( function ( err , res ) {
309
+ if ( err ) return done ( err ) ;
310
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
311
+ expect ( res . body . message ) . to . equal ( "Route not found" ) ;
312
+ done ( ) ;
313
+ } ) ;
314
+ } ) ;
315
+
316
+ it ( "should return 404 and 'Route not found' message when dev is missing" , function ( done ) {
317
+ chai
318
+ . request ( app )
319
+ . get ( "/impersonation/requests" )
320
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
321
+ . end ( function ( err , res ) {
322
+ if ( err ) return done ( err ) ;
323
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
324
+ expect ( res . body . message ) . to . equal ( "Route not found" ) ;
325
+ done ( ) ;
326
+ } ) ;
327
+ } ) ;
328
+
329
+ it ( "should return all requests if dev flag is present" , function ( done ) {
330
+ chai
331
+ . request ( app )
332
+ . get ( requestsEndpoint )
333
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
334
+ . end ( function ( err , res ) {
335
+ if ( err ) return done ( err ) ;
336
+ expect ( res ) . to . have . status ( 200 ) ;
337
+ expect ( res . body . data ) . to . be . an ( "array" ) ;
338
+ expect ( res . body . data . length ) . to . be . equal ( 4 ) ;
339
+ expect ( res . body . data [ 0 ] ) . to . include . all . keys (
340
+ "id" , "createdBy" , "userId" , "impersonatedUserId" , "createdFor"
341
+ ) ;
342
+ done ( ) ;
343
+ } ) ;
344
+ } ) ;
345
+
346
+
347
+ it ( "should return all requests created by a specific user" , function ( done ) {
348
+ chai
349
+ . request ( app )
350
+ . get ( `${ requestsEndpoint } &createdBy=${ userData [ 4 ] . username } ` )
351
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
352
+ . end ( function ( err , res ) {
353
+ if ( err ) return done ( err ) ;
354
+ expect ( res ) . to . have . status ( 200 ) ;
355
+ expect ( res . body . data ) . to . be . an ( "array" ) ;
356
+ expect ( res . body . data . every ( ( r ) => r . userId === testSuperUserId ) ) . to . be . true ;
357
+ expect ( res . body . data . every ( ( r ) => r . createdBy === userData [ 4 ] . username ) ) . to . be . true ;
358
+ done ( ) ;
359
+ } ) ;
360
+ } ) ;
361
+
362
+ it ( "should return all requests created for a specific user" , function ( done ) {
363
+ chai
364
+ . request ( app )
365
+ . get ( `${ requestsEndpoint } &createdFor=${ userData [ 19 ] . username } ` )
366
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
367
+ . end ( function ( err , res ) {
368
+ if ( err ) return done ( err ) ;
369
+ expect ( res ) . to . have . status ( 200 ) ;
370
+ expect ( res . body . data ) . to . be . an ( "array" ) ;
371
+ expect ( res . body . data . every ( ( r ) => r . createdFor === userData [ 19 ] . username ) ) . to . be . true ;
372
+ expect ( res . body . data . length ) . to . equal ( 1 ) ;
373
+ done ( ) ;
374
+ } ) ;
375
+ } ) ;
376
+
377
+ it ( "should return 204 with no response body when no data found" , function ( done ) {
378
+ chai
379
+ . request ( app )
380
+ . get ( `${ requestsEndpoint } &createdBy=testUserRandom` )
381
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
382
+ . end ( function ( err , res ) {
383
+ if ( err ) return done ( err ) ;
384
+ expect ( res ) . to . have . status ( 204 ) ;
385
+ expect ( res . body ) . to . deep . equal ( { } ) ;
386
+ done ( ) ;
387
+ } ) ;
388
+ } ) ;
389
+
390
+ it ( "should return requests filtered by status APPROVED" , function ( done ) {
391
+ chai
392
+ . request ( app )
393
+ . get ( `${ requestsEndpoint } &status=APPROVED` )
394
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
395
+ . end ( function ( err , res ) {
396
+ if ( err ) return done ( err ) ;
397
+ expect ( res ) . to . have . status ( 200 ) ;
398
+ expect ( res . body . data ) . to . be . an ( "array" ) ;
399
+ expect ( res . body . data . every ( ( r ) => r . status === "APPROVED" ) ) . to . be . true ;
400
+ done ( ) ;
401
+ } ) ;
402
+ } ) ;
403
+
404
+ it ( "should return error if invalid status is passed" , function ( done ) {
405
+ chai
406
+ . request ( app )
407
+ . get ( `${ requestsEndpoint } &status=ACTIVE` )
408
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
409
+ . end ( function ( err , res ) {
410
+ if ( err ) return done ( err ) ;
411
+ expect ( res ) . to . have . status ( 400 ) ;
412
+ expect ( res . body . error ) . to . equal ( "Bad Request" ) ;
413
+ expect ( res . body . message ) . to . equal ( `"status" must be one of [APPROVED, PENDING, REJECTED]` ) ;
414
+ done ( ) ;
415
+ } ) ;
416
+ } ) ;
417
+
418
+ it ( "should return a next link when next param is provided" , function ( done ) {
419
+ chai
420
+ . request ( app )
421
+ . get ( `${ requestsEndpoint } &size=2` )
422
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
423
+ . end ( function ( err , res ) {
424
+ if ( err ) return done ( err ) ;
425
+ expect ( res ) . to . have . status ( 200 ) ;
426
+ expect ( res . body ) . to . have . property ( "next" ) ;
427
+ expect ( res . body ) . to . have . property ( "prev" ) ;
428
+ expect ( res . body . prev ) . to . be . null ;
429
+ expect ( res . body . next ) . to . be . not . null ;
430
+ expect ( res . body ) . to . have . property ( "data" ) ;
431
+ expect ( res . body ) . to . have . property ( "count" ) . to . equal ( 2 ) ;
432
+ done ( ) ;
433
+ } ) ;
434
+ } ) ;
435
+
436
+ it ( "should return count property with the number of requests" , function ( done ) {
437
+ chai
438
+ . request ( app )
439
+ . get ( requestsEndpoint )
440
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
441
+ . end ( function ( err , res ) {
442
+ if ( err ) return done ( err ) ;
443
+ expect ( res ) . to . have . status ( 200 ) ;
444
+ expect ( res . body ) . to . have . property ( "count" ) ;
445
+ expect ( res . body . count ) . to . be . a ( "number" ) ;
446
+ expect ( res . body . count ) . to . equal ( res . body . data . length ) ;
447
+ done ( ) ;
448
+ } ) ;
449
+ } ) ;
450
+
451
+ it ( "should return the next page of results using next cursor" , function ( done ) {
452
+ chai
453
+ . request ( app )
454
+ . get ( `${ requestsEndpoint } &size=2` )
455
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
456
+ . end ( function ( err , res1 ) {
457
+ if ( err ) return done ( err ) ;
458
+ expect ( res1 ) . to . have . status ( 200 ) ;
459
+ expect ( res1 . body ) . to . have . property ( "next" ) . is . not . null ;
460
+ expect ( res1 . body ) . to . have . property ( "data" ) . is . an ( "array" ) ;
461
+ expect ( res1 . body . data . length ) . to . be . at . most ( 2 ) ;
462
+
463
+ const nextEndpoint = res1 . body . next ;
464
+
465
+ chai
466
+ . request ( app )
467
+ . get ( `${ nextEndpoint } ` )
468
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
469
+ . end ( function ( err2 , res2 ) {
470
+ if ( err2 ) return done ( err2 ) ;
471
+ expect ( res2 ) . to . have . status ( 200 ) ;
472
+ expect ( res2 . body ) . to . have . property ( "data" ) . is . an ( "array" ) ;
473
+ expect ( res2 . body . data . length ) . to . be . at . most ( 2 ) ;
474
+ expect ( res2 . body ) . to . have . property ( "prev" ) . is . not . null ;
475
+ expect ( res2 . body . data [ 0 ] . id ) . to . not . equal ( res1 . body . data [ 0 ] . id ) ;
476
+ done ( ) ;
477
+ } ) ;
478
+ } ) ;
479
+ } ) ;
480
+
481
+ it ( "should return the previous page of results using prev cursor" , function ( done ) {
482
+ chai
483
+ . request ( app )
484
+ . get ( `${ requestsEndpoint } &size=2` )
485
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
486
+ . end ( function ( err , res1 ) {
487
+ if ( err ) return done ( err ) ;
488
+ const nextEndpoint = res1 . body . next ;
489
+
490
+ chai
491
+ . request ( app )
492
+ . get ( `${ nextEndpoint } ` )
493
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
494
+ . end ( function ( err2 , res2 ) {
495
+ if ( err2 ) return done ( err2 ) ;
496
+
497
+ const prevEndpoint = res2 . body . prev ;
498
+ if ( ! prevEndpoint ) return done ( ) ;
499
+
500
+ chai
501
+ . request ( app )
502
+ . get ( `${ prevEndpoint } ` )
503
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
504
+ . end ( function ( err3 , res3 ) {
505
+ if ( err3 ) return done ( err3 ) ;
506
+ expect ( res3 ) . to . have . status ( 200 ) ;
507
+ expect ( res3 . body ) . to . have . property ( "data" ) . is . an ( "array" ) ;
508
+ expect ( res3 . body . data [ 0 ] . id ) . to . equal ( res1 . body . data [ 0 ] . id ) ;
509
+ done ( ) ;
510
+ } ) ;
511
+ } ) ;
512
+ } ) ;
513
+ } ) ;
514
+ } ) ;
515
+ describe ( "GET /impersonation/requests/:id" , function ( ) {
516
+ it ( "should return 404 and 'Route not found' message when dev is false" , function ( done ) {
517
+ chai
518
+ . request ( app )
519
+ . get ( "/impersonation/requests/randomId?dev=false" )
520
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
521
+ . end ( function ( err , res ) {
522
+ if ( err ) return done ( err ) ;
523
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
524
+ expect ( res . body . message ) . to . equal ( "Route not found" ) ;
525
+ done ( ) ;
526
+ } ) ;
527
+ } ) ;
528
+
529
+ it ( "should return 404 and 'Route not found' message when dev is missing" , function ( done ) {
530
+ chai
531
+ . request ( app )
532
+ . get ( "/impersonation/requests/randomId" )
533
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
534
+ . end ( function ( err , res ) {
535
+ if ( err ) return done ( err ) ;
536
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
537
+ expect ( res . body . message ) . to . equal ( "Route not found" ) ;
538
+ done ( ) ;
539
+ } ) ;
540
+ } ) ;
541
+
542
+ it ( "should return request by specific ID" , function ( done ) {
543
+ chai
544
+ . request ( app )
545
+ . get ( `/impersonation/requests/${ impersonationRequest1 . id } ?dev=true` )
546
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
547
+ . end ( function ( err , res ) {
548
+ if ( err ) return done ( err ) ;
549
+ expect ( res ) . to . have . status ( 200 ) ;
550
+ expect ( res . body . data ) . to . be . an ( "object" ) ;
551
+ expect ( res . body . data . id ) . to . equal ( impersonationRequest1 . id ) ;
552
+ done ( ) ;
553
+ } ) ;
554
+ } ) ;
555
+
556
+ it ( "should return 404 and 'Route not found' message when request ID is not found" , function ( done ) {
557
+ chai
558
+ . request ( app )
559
+ . get ( `/impersonation/requests/randomId?dev=true` )
560
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
561
+ . end ( function ( err , res ) {
562
+ if ( err ) return done ( err ) ;
563
+ expect ( res . statusCode ) . to . equal ( 404 ) ;
564
+ expect ( res . body . message ) . to . equal ( REQUEST_DOES_NOT_EXIST ) ;
565
+ done ( ) ;
566
+ } ) ;
567
+ } ) ;
568
+
569
+ it ( "should return 400 and 'Bad Request' message when validator check fails" , function ( done ) {
570
+ chai
571
+ . request ( app )
572
+ . get ( `/impersonation/requests/4&8828**?dev=true` )
573
+ . set ( "cookie" , `${ cookieName } =${ authToken } ` )
574
+ . end ( function ( err , res ) {
575
+ if ( err ) return done ( err ) ;
576
+ expect ( res . statusCode ) . to . equal ( 400 ) ;
577
+ expect ( res . body . message ) . to . equal ( '"id" with value "4&8828**" fails to match the required pattern: /^[a-zA-Z0-9-_]+$/' ) ;
578
+ done ( ) ;
579
+ } ) ;
580
+ } ) ;
581
+
582
+ } )
267
583
} ) ;
0 commit comments