1
1
// This test needs to run first
2
2
const chai = require ( 'chai' ) ;
3
3
const db = require ( '../src/db' ) ;
4
+ const { trimTrailingDotGit } = require ( '../src/db/helper' ) ;
4
5
5
6
const { expect } = chai ;
6
7
@@ -46,6 +47,21 @@ const TEST_PUSH = {
46
47
attestation : null ,
47
48
} ;
48
49
50
+ const TEST_REPO_DOT_GIT = {
51
+ project : 'finos' ,
52
+ name : 'db.git-test-repo' ,
53
+ url : 'https://github.com/finos/db.git-test-repo.git' ,
54
+ } ;
55
+
56
+ // the same as TEST_PUSH but with .git somewhere valid within the name
57
+ // to ensure a global replace isn't done when trimming, just to the end
58
+ const TEST_PUSH_DOT_GIT = {
59
+ ...TEST_PUSH ,
60
+ repoName : 'db.git-test-repo.git' ,
61
+ url : 'https://github.com/finos/db.git-test-repo.git' ,
62
+ repo : 'finos/db.git-test-repo.git' ,
63
+ } ;
64
+
49
65
/**
50
66
* Clean up response data from the DB by removing an extraneous properties,
51
67
* allowing comparison with expect.
@@ -563,7 +579,7 @@ describe('Database clients', async () => {
563
579
564
580
it ( 'should be able to check if a user can cancel push' , async function ( ) {
565
581
let threwError = false ;
566
- const repoName = TEST_PUSH . repoName . replace ( '.git' , '' ) ;
582
+ const repoName = trimTrailingDotGit ( TEST_PUSH . repoName ) ;
567
583
try {
568
584
// push does not exist yet, should return false
569
585
let allowed = await db . canUserCancelPush ( TEST_PUSH . id , TEST_USER . username ) ;
@@ -588,34 +604,81 @@ describe('Database clients', async () => {
588
604
} ) ;
589
605
590
606
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' , '' ) ;
607
+ let allowed = undefined ;
608
+ const repoName = trimTrailingDotGit ( TEST_PUSH . repoName ) ;
609
+
593
610
try {
594
611
// push does not exist yet, should return false
595
- let allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
612
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
596
613
expect ( allowed ) . to . be . false ;
614
+ } catch ( e ) {
615
+ expect . fail ( e ) ;
616
+ }
597
617
618
+ try {
598
619
// create the push - user should already exist and not authorised to push
599
620
await db . writeAudit ( TEST_PUSH ) ;
600
621
allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
601
622
expect ( allowed ) . to . be . false ;
623
+ } catch ( e ) {
624
+ expect . fail ( e ) ;
625
+ }
602
626
627
+ try {
603
628
// authorise user and recheck
604
629
await db . addUserCanAuthorise ( repoName , TEST_USER . username ) ;
605
630
allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
606
631
expect ( allowed ) . to . be . true ;
607
632
} catch ( e ) {
608
- threwError = true ;
633
+ expect . fail ( e ) ;
609
634
}
610
- expect ( threwError ) . to . be . false ;
635
+
611
636
// clean up
612
637
await db . deletePush ( TEST_PUSH . id ) ;
613
638
await db . removeUserCanAuthorise ( repoName , TEST_USER . username ) ;
614
639
} ) ;
615
640
641
+ it ( 'should be able to check if a user can approve/reject push including .git within the repo name' , async function ( ) {
642
+ let allowed = undefined ;
643
+ const repoName = trimTrailingDotGit ( TEST_PUSH_DOT_GIT . repoName ) ;
644
+
645
+ await db . createRepo ( TEST_REPO_DOT_GIT ) ;
646
+ try {
647
+ // push does not exist yet, should return false
648
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH_DOT_GIT . id , TEST_USER . username ) ;
649
+ expect ( allowed ) . to . be . false ;
650
+ } catch ( e ) {
651
+ expect . fail ( e ) ;
652
+ }
653
+
654
+ try {
655
+ // create the push - user should already exist and not authorised to push
656
+ await db . writeAudit ( TEST_PUSH_DOT_GIT ) ;
657
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH_DOT_GIT . id , TEST_USER . username ) ;
658
+ expect ( allowed ) . to . be . false ;
659
+ } catch ( e ) {
660
+ expect . fail ( e ) ;
661
+ }
662
+
663
+ try {
664
+ // authorise user and recheck
665
+ await db . addUserCanAuthorise ( repoName , TEST_USER . username ) ;
666
+ allowed = await db . canUserApproveRejectPush ( TEST_PUSH_DOT_GIT . id , TEST_USER . username ) ;
667
+ expect ( allowed ) . to . be . true ;
668
+ } catch ( e ) {
669
+ expect . fail ( e ) ;
670
+ }
671
+
672
+ // clean up
673
+ await db . deletePush ( TEST_PUSH_DOT_GIT . id ) ;
674
+ await db . removeUserCanAuthorise ( repoName , TEST_USER . username ) ;
675
+ } ) ;
676
+
616
677
after ( async function ( ) {
617
678
await db . deleteRepo ( TEST_REPO . name ) ;
679
+ await db . deleteRepo ( TEST_REPO_DOT_GIT . name ) ;
618
680
await db . deleteUser ( TEST_USER . username ) ;
619
681
await db . deletePush ( TEST_PUSH . id ) ;
682
+ await db . deletePush ( TEST_PUSH_DOT_GIT . id ) ;
620
683
} ) ;
621
684
} ) ;
0 commit comments