@@ -95,6 +95,11 @@ describe('Database clients', async () => {
95
95
const repos2 = await db . getRepos ( { url : TEST_REPO . url } ) ;
96
96
const cleanRepos2 = cleanResponseData ( TEST_REPO , repos2 ) ;
97
97
expect ( cleanRepos2 [ 0 ] ) . to . eql ( TEST_REPO ) ;
98
+
99
+ // passing an empty query should produce same results as no query
100
+ const repos3 = await db . getRepos ( ) ;
101
+ const repos4 = await db . getRepos ( { } ) ;
102
+ expect ( repos3 ) . to . have . same . deep . members ( repos4 ) ;
98
103
} ) ;
99
104
100
105
it ( 'should be able to retrieve a repo by name' , async function ( ) {
@@ -270,6 +275,17 @@ describe('Database clients', async () => {
270
275
// leave user in place for next test(s)
271
276
} ) ;
272
277
278
+ it ( 'should throw an error when authorising a user to push on non-existent repo' , async function ( ) {
279
+ let threwError = false ;
280
+ try {
281
+ // uppercase the filter value to confirm db client is lowercasing inputs
282
+ await db . addUserCanPush ( 'non-existent-repo' , TEST_USER . username ) ;
283
+ } catch ( e ) {
284
+ threwError = true ;
285
+ }
286
+ expect ( threwError ) . to . be . true ;
287
+ } ) ;
288
+
273
289
it ( 'should be able to authorise a user to push and confirm that they can' , async function ( ) {
274
290
let threwError = false ;
275
291
try {
@@ -301,6 +317,16 @@ describe('Database clients', async () => {
301
317
expect ( threwError ) . to . be . false ;
302
318
} ) ;
303
319
320
+ it ( 'should throw an error when de-authorising a user to push on non-existent repo' , async function ( ) {
321
+ let threwError = false ;
322
+ try {
323
+ await db . removeUserCanPush ( 'non-existent-repo' , TEST_USER . username ) ;
324
+ } catch ( e ) {
325
+ threwError = true ;
326
+ }
327
+ expect ( threwError ) . to . be . true ;
328
+ } ) ;
329
+
304
330
it ( "should be able to de-authorise a user to push and confirm that they can't" , async function ( ) {
305
331
let threwError = false ;
306
332
try {
@@ -331,6 +357,16 @@ describe('Database clients', async () => {
331
357
expect ( threwError ) . to . be . false ;
332
358
} ) ;
333
359
360
+ it ( 'should throw an error when authorising a user to authorise on non-existent repo' , async function ( ) {
361
+ let threwError = false ;
362
+ try {
363
+ await db . addUserCanAuthorise ( 'non-existent-repo' , TEST_USER . username ) ;
364
+ } catch ( e ) {
365
+ threwError = true ;
366
+ }
367
+ expect ( threwError ) . to . be . true ;
368
+ } ) ;
369
+
334
370
it ( 'should be able to authorise a user to authorise and confirm that they can' , async function ( ) {
335
371
let threwError = false ;
336
372
try {
@@ -361,6 +397,17 @@ describe('Database clients', async () => {
361
397
expect ( threwError ) . to . be . false ;
362
398
} ) ;
363
399
400
+ it ( 'should throw an error when de-authorising a user to push on non-existent repo' , async function ( ) {
401
+ let threwError = false ;
402
+ try {
403
+ // uppercase the filter value to confirm db client is lowercasing inputs
404
+ await db . removeUserCanAuthorise ( 'non-existent-repo' , TEST_USER . username ) ;
405
+ } catch ( e ) {
406
+ threwError = true ;
407
+ }
408
+ expect ( threwError ) . to . be . true ;
409
+ } ) ;
410
+
364
411
it ( "should be able to de-authorise a user to authorise and confirm that they can't" , async function ( ) {
365
412
let threwError = false ;
366
413
try {
@@ -397,6 +444,33 @@ describe('Database clients', async () => {
397
444
expect ( threwError ) . to . be . false ;
398
445
} ) ;
399
446
447
+ it ( 'should NOT throw an error when checking whether a user can push on non-existent repo' , async function ( ) {
448
+ let threwError = false ;
449
+ try {
450
+ // uppercase the filter value to confirm db client is lowercasing inputs
451
+ const allowed = await db . isUserPushAllowed ( 'non-existent-repo' , TEST_USER . username ) ;
452
+ expect ( allowed ) . to . be . false ;
453
+ } catch ( e ) {
454
+ threwError = true ;
455
+ }
456
+ expect ( threwError ) . to . be . false ;
457
+ } ) ;
458
+
459
+ it ( 'should NOT throw an error when checking whether a user can authorise on non-existent repo' , async function ( ) {
460
+ let threwError = false ;
461
+ try {
462
+ // uppercase the filter value to confirm db client is lowercasing inputs
463
+ const allowed = await db . canUserApproveRejectPushRepo (
464
+ 'non-existent-repo' ,
465
+ TEST_USER . username ,
466
+ ) ;
467
+ expect ( allowed ) . to . be . false ;
468
+ } catch ( e ) {
469
+ threwError = true ;
470
+ }
471
+ expect ( threwError ) . to . be . false ;
472
+ } ) ;
473
+
400
474
it ( 'should be able to create a push' , async function ( ) {
401
475
await db . writeAudit ( TEST_PUSH ) ;
402
476
const pushes = await db . getPushes ( ) ;
@@ -411,6 +485,134 @@ describe('Database clients', async () => {
411
485
expect ( cleanPushes ) . to . not . deep . include ( TEST_PUSH ) ;
412
486
} ) ;
413
487
488
+ it ( 'should be able to authorise a push' , async function ( ) {
489
+ // first create the push
490
+ await db . writeAudit ( TEST_PUSH ) ;
491
+ let threwError = false ;
492
+ try {
493
+ const msg = await db . authorise ( TEST_PUSH . id ) ;
494
+ expect ( msg ) . to . have . property ( 'message' ) ;
495
+ } catch ( e ) {
496
+ console . error ( 'Error: ' , e ) ;
497
+ threwError = true ;
498
+ }
499
+ expect ( threwError ) . to . be . false ;
500
+ // clean up
501
+ await db . deletePush ( TEST_PUSH . id ) ;
502
+ } ) ;
503
+
504
+ it ( 'should throw an error when authorising a non-existent a push' , async function ( ) {
505
+ let threwError = false ;
506
+ try {
507
+ await db . authorise ( TEST_PUSH . id ) ;
508
+ } catch ( e ) {
509
+ threwError = true ;
510
+ }
511
+ expect ( threwError ) . to . be . true ;
512
+ } ) ;
513
+
514
+ it ( 'should be able to reject a push' , async function ( ) {
515
+ // first create the push
516
+ await db . writeAudit ( TEST_PUSH ) ;
517
+ let threwError = false ;
518
+ try {
519
+ const msg = await db . reject ( TEST_PUSH . id ) ;
520
+ expect ( msg ) . to . have . property ( 'message' ) ;
521
+ } catch ( e ) {
522
+ threwError = true ;
523
+ }
524
+ expect ( threwError ) . to . be . false ;
525
+ // clean up
526
+ await db . deletePush ( TEST_PUSH . id ) ;
527
+ } ) ;
528
+
529
+ it ( 'should throw an error when rejecting a non-existent a push' , async function ( ) {
530
+ let threwError = false ;
531
+ try {
532
+ await db . reject ( TEST_PUSH . id ) ;
533
+ } catch ( e ) {
534
+ threwError = true ;
535
+ }
536
+ expect ( threwError ) . to . be . true ;
537
+ } ) ;
538
+
539
+ it ( 'should be able to cancel a push' , async function ( ) {
540
+ // first create the push
541
+ await db . writeAudit ( TEST_PUSH ) ;
542
+ let threwError = false ;
543
+ try {
544
+ const msg = await db . cancel ( TEST_PUSH . id ) ;
545
+ expect ( msg ) . to . have . property ( 'message' ) ;
546
+ } catch ( e ) {
547
+ threwError = true ;
548
+ }
549
+ expect ( threwError ) . to . be . false ;
550
+ // clean up
551
+ await db . deletePush ( TEST_PUSH . id ) ;
552
+ } ) ;
553
+
554
+ it ( 'should throw an error when cancelling a non-existent a push' , async function ( ) {
555
+ let threwError = false ;
556
+ try {
557
+ await db . cancel ( TEST_PUSH . id ) ;
558
+ } catch ( e ) {
559
+ threwError = true ;
560
+ }
561
+ expect ( threwError ) . to . be . true ;
562
+ } ) ;
563
+
564
+ it ( 'should be able to check if a user can cancel push' , async function ( ) {
565
+ let threwError = false ;
566
+ const repoName = TEST_PUSH . repoName . replace ( '.git' , '' ) ;
567
+ try {
568
+ // push does not exist yet, should return false
569
+ let allowed = await db . canUserCancelPush ( TEST_PUSH . id , TEST_USER . username ) ;
570
+ expect ( allowed ) . to . be . false ;
571
+
572
+ // create the push - user should already exist and not authorised to push
573
+ await db . writeAudit ( TEST_PUSH ) ;
574
+ allowed = await db . canUserCancelPush ( TEST_PUSH . id , TEST_USER . username ) ;
575
+ expect ( allowed ) . to . be . false ;
576
+
577
+ // authorise user and recheck
578
+ await db . addUserCanPush ( repoName , TEST_USER . username ) ;
579
+ allowed = await db . canUserCancelPush ( TEST_PUSH . id , TEST_USER . username ) ;
580
+ expect ( allowed ) . to . be . true ;
581
+ } catch ( e ) {
582
+ threwError = true ;
583
+ }
584
+ expect ( threwError ) . to . be . false ;
585
+ // clean up
586
+ await db . deletePush ( TEST_PUSH . id ) ;
587
+ await db . removeUserCanPush ( repoName , TEST_USER . username ) ;
588
+ } ) ;
589
+
590
+ it ( 'should be able to check if a user can approve/reject push' , async function ( ) {
591
+ let threwError = false ;
592
+ const repoName = TEST_PUSH . repoName . replace ( '.git' , '' ) ;
593
+ try {
594
+ // push does not exist yet, should return false
595
+ let allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
596
+ expect ( allowed ) . to . be . false ;
597
+
598
+ // create the push - user should already exist and not authorised to push
599
+ await db . writeAudit ( TEST_PUSH ) ;
600
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
601
+ expect ( allowed ) . to . be . false ;
602
+
603
+ // authorise user and recheck
604
+ await db . addUserCanAuthorise ( repoName , TEST_USER . username ) ;
605
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
606
+ expect ( allowed ) . to . be . true ;
607
+ } catch ( e ) {
608
+ threwError = true ;
609
+ }
610
+ expect ( threwError ) . to . be . false ;
611
+ // clean up
612
+ await db . deletePush ( TEST_PUSH . id ) ;
613
+ await db . removeUserCanAuthorise ( repoName , TEST_USER . username ) ;
614
+ } ) ;
615
+
414
616
after ( async function ( ) {
415
617
await db . deleteRepo ( TEST_REPO . name ) ;
416
618
await db . deleteUser ( TEST_USER . username ) ;
0 commit comments