Skip to content

Commit 354dbca

Browse files
Prevent reassigning project to same application by validating associated project IDs(AST-124988) (#1387)
1 parent 828a903 commit 354dbca

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

internal/services/applications.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,14 @@ func updateApplication(applicationModel *wrappers.ApplicationConfiguration, appl
113113
}
114114

115115
func associateProjectToApplication(applicationID, projectID string, associatedProjectIds []string, applicationsWrapper wrappers.ApplicationsWrapper) error {
116-
associatedProjectIds = append(associatedProjectIds, projectID)
116+
for _, id := range associatedProjectIds {
117+
if id == projectID {
118+
logger.PrintfIfVerbose("Project is already associated with the application. Skipping association")
119+
return nil
120+
}
121+
}
117122
associateProjectsModel := &wrappers.AssociateProjectModel{
118-
ProjectIds: associatedProjectIds,
123+
ProjectIds: []string{projectID},
119124
}
120125
errorModel, err := applicationsWrapper.CreateProjectAssociation(applicationID, associateProjectsModel)
121126
return handleApplicationUpdateResponse(errorModel, err)

internal/services/applications_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package services
22

33
import (
4+
"reflect"
5+
"strings"
6+
"testing"
7+
48
errorConstants "github.com/checkmarx/ast-cli/internal/constants/errors"
59
"github.com/checkmarx/ast-cli/internal/wrappers"
610
"github.com/checkmarx/ast-cli/internal/wrappers/mock"
711
"gotest.tools/assert"
8-
"reflect"
9-
"strings"
10-
"testing"
1112
)
1213

1314
func Test_createApplicationIds(t *testing.T) {
@@ -87,3 +88,12 @@ func Test_ProjectAssociation_ToApplicationWithoutDirectAssociation(t *testing.T)
8788
})
8889
}
8990
}
91+
92+
func Test_AssociateProjectToApplication_ProjectAlreadyAssociated(t *testing.T) {
93+
projectID := "project-123"
94+
associatedProjectIds := []string{"project-123", "project-456"}
95+
applicationName := "app-1"
96+
applicationWrapper := &mock.ApplicationsMockWrapper{}
97+
err := associateProjectToApplication(applicationName, projectID, associatedProjectIds, applicationWrapper)
98+
assert.NilError(t, err)
99+
}

0 commit comments

Comments
 (0)