11// This test needs to run first
22const chai = require ( 'chai' ) ;
33const db = require ( '../src/db' ) ;
4+ const { trimTrailingDotGit } = require ( '../src/db/helper' ) ;
45
56const { expect } = chai ;
67
@@ -46,6 +47,21 @@ const TEST_PUSH = {
4647 attestation : null ,
4748} ;
4849
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+
4965/**
5066 * Clean up response data from the DB by removing an extraneous properties,
5167 * allowing comparison with expect.
@@ -563,7 +579,7 @@ describe('Database clients', async () => {
563579
564580 it ( 'should be able to check if a user can cancel push' , async function ( ) {
565581 let threwError = false ;
566- const repoName = TEST_PUSH . repoName . replace ( '.git' , '' ) ;
582+ const repoName = trimTrailingDotGit ( TEST_PUSH . repoName ) ;
567583 try {
568584 // push does not exist yet, should return false
569585 let allowed = await db . canUserCancelPush ( TEST_PUSH . id , TEST_USER . username ) ;
@@ -588,34 +604,81 @@ describe('Database clients', async () => {
588604 } ) ;
589605
590606 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+
593610 try {
594611 // 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 ) ;
596613 expect ( allowed ) . to . be . false ;
614+ } catch ( e ) {
615+ expect . fail ( e ) ;
616+ }
597617
618+ try {
598619 // create the push - user should already exist and not authorised to push
599620 await db . writeAudit ( TEST_PUSH ) ;
600621 allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
601622 expect ( allowed ) . to . be . false ;
623+ } catch ( e ) {
624+ expect . fail ( e ) ;
625+ }
602626
627+ try {
603628 // authorise user and recheck
604629 await db . addUserCanAuthorise ( repoName , TEST_USER . username ) ;
605630 allowed = await db . canUserApproveRejectPush ( TEST_PUSH . id , TEST_USER . username ) ;
606631 expect ( allowed ) . to . be . true ;
607632 } catch ( e ) {
608- threwError = true ;
633+ expect . fail ( e ) ;
609634 }
610- expect ( threwError ) . to . be . false ;
635+
611636 // clean up
612637 await db . deletePush ( TEST_PUSH . id ) ;
613638 await db . removeUserCanAuthorise ( repoName , TEST_USER . username ) ;
614639 } ) ;
615640
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+
616677 after ( async function ( ) {
617678 await db . deleteRepo ( TEST_REPO . name ) ;
679+ await db . deleteRepo ( TEST_REPO_DOT_GIT . name ) ;
618680 await db . deleteUser ( TEST_USER . username ) ;
619681 await db . deletePush ( TEST_PUSH . id ) ;
682+ await db . deletePush ( TEST_PUSH_DOT_GIT . id ) ;
620683 } ) ;
621684} ) ;
0 commit comments