From 7bf1230424f2613046c6a86464c4306019b6e232 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Mon, 11 Nov 2024 13:47:13 +0200 Subject: [PATCH 1/7] do not update existing project with new group (AST-70718) --- internal/services/projects.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/internal/services/projects.go b/internal/services/projects.go index ea79e8cb4..a14fa1a07 100644 --- a/internal/services/projects.go +++ b/internal/services/projects.go @@ -245,26 +245,9 @@ func updateProject( } } - if projectGroups != "" { - err = UpsertProjectGroupsByUpdateFlow(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, projModelResp, projectGroups, projectID, projectName, featureFlagsWrapper) - if err != nil { - return projectID, err - } - } return projectID, nil } -func UpsertProjectGroupsByUpdateFlow(groupsWrapper wrappers.GroupsWrapper, projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper, - accessManagementWrapper wrappers.AccessManagementWrapper, projModelResp *wrappers.ProjectResponseModel, - projectGroups string, projectID string, projectName string, featureFlagsWrapper wrappers.FeatureFlagsWrapper) error { - groupsMap, groups, groupErr := GetGroupMap(groupsWrapper, projectGroups, projModelResp, featureFlagsWrapper) - if groupErr != nil { - return groupErr - } - projModel.Groups = groups - return UpsertProjectGroups(projModel, projectsWrapper, accessManagementWrapper, projectID, projectName, featureFlagsWrapper, groupsMap) -} - func UpsertProjectGroups(projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper, accessManagementWrapper wrappers.AccessManagementWrapper, projectID string, projectName string, featureFlagsWrapper wrappers.FeatureFlagsWrapper, groupsMap []*wrappers.Group) error { From b856e48c5248ccbcf2d7b47129cb8d9f49958588 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Tue, 12 Nov 2024 16:29:06 +0200 Subject: [PATCH 2/7] CR fix --- internal/services/projects.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/internal/services/projects.go b/internal/services/projects.go index a14fa1a07..4346ded36 100644 --- a/internal/services/projects.go +++ b/internal/services/projects.go @@ -38,7 +38,6 @@ func FindProject( for i := 0; i < len(resp.Projects); i++ { if resp.Projects[i].Name == projectName { - projectGroups, _ := cmd.Flags().GetString(commonParams.ProjectGroupList) projectTags, _ := cmd.Flags().GetString(commonParams.ProjectTagList) projectPrivatePackage, _ := cmd.Flags().GetString(commonParams.ProjecPrivatePackageFlag) return updateProject( @@ -50,7 +49,6 @@ func FindProject( applicationWrapper, projectName, applicationID, - projectGroups, projectTags, projectPrivatePackage, featureFlagsWrapper) @@ -185,7 +183,6 @@ func updateProject( applicationsWrapper wrappers.ApplicationsWrapper, projectName string, applicationID []string, - projectGroups string, projectTags string, projectPrivatePackage string, featureFlagsWrapper wrappers.FeatureFlagsWrapper, @@ -205,8 +202,8 @@ func updateProject( projModel.RepoURL = resp.Projects[i].RepoURL } } - if projectGroups == "" && projectTags == "" && projectPrivatePackage == "" && len(applicationID) == 0 { - logger.PrintIfVerbose("No groups, applicationId or tags to update. Skipping project update.") + if projectTags == "" && projectPrivatePackage == "" && len(applicationID) == 0 { + logger.PrintIfVerbose("No applicationId or tags to update. Skipping project update.") return projectID, nil } if projectPrivatePackage != "" { From df137a0e8a2ce6c9b6149c894416c608237e7c80 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Wed, 13 Nov 2024 10:32:26 +0200 Subject: [PATCH 3/7] add tests --- internal/commands/scan_test.go | 60 +++++++++++++++++++++++++----- internal/services/projects_test.go | 1 - 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 6186bccf8..10c7811e4 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -3,17 +3,16 @@ package commands import ( - "archive/zip" - "bytes" "fmt" - "log" "os" "reflect" "strings" "testing" + "github.com/checkmarx/ast-cli/internal/commands/util" errorConstants "github.com/checkmarx/ast-cli/internal/constants/errors" exitCodes "github.com/checkmarx/ast-cli/internal/constants/exit-codes" + "github.com/checkmarx/ast-cli/internal/logger" commonParams "github.com/checkmarx/ast-cli/internal/params" "github.com/checkmarx/ast-cli/internal/wrappers" "github.com/checkmarx/ast-cli/internal/wrappers/mock" @@ -52,6 +51,8 @@ const ( InvalidEngineMessage = "Please verify if engine is installed" SCSScoreCardError = "SCS scan failed to start: Scorecard scan is missing required flags, please include in the ast-cli arguments: " + "--scs-repo-url your_repo_url --scs-repo-token your_repo_token" + outputFileName = "test_output.log" + noUpdatesForExistingProject = "No applicationId or tags to update. Skipping project update." ) func TestScanHelp(t *testing.T) { @@ -382,14 +383,31 @@ func TestCreateScanBranches(t *testing.T) { execCmdNilAssertion(t, "scan", "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "branch_defined") } -func TestCreateScanWithProjectGroup(t *testing.T) { +func TestCreateScan_WhenProjectNotExistsAndInvalidGroup_ShouldFail(t *testing.T) { err := execCmdNotNilAssertion( t, - "scan", "create", "--project-name", "invalidGroup", "-s", ".", "--branch", "main", "--project-groups", "invalidGroup", + "scan", "create", "--project-name", "newProject", "-s", ".", "--branch", "main", "--project-groups", "invalidGroup", ) assert.Assert(t, err.Error() == "Failed updating a project: Failed finding groups: [invalidGroup]", "\n the received error is:", err.Error()) } +func TestCreateScan_WhenProjectNotExists_ShouldCreateProjectAndAssignGroup(t *testing.T) { + file := createOutputFile(t, outputFileName) + defer deleteOutputFile(t, file) + defer logger.SetOutput(os.Stdout) + + baseArgs := []string{"scan", "create", "--project-name", "newProject", "-s", ".", "--branch", "main", "--project-groups", "existsGroup1", "--debug"} + execCmdNilAssertion( + t, + baseArgs..., + ) + stdoutString, err := util.ReadFileAsString(file.Name()) + if err != nil { + t.Fatalf("Failed to read log file: %v", err) + } + assert.Equal(t, strings.Contains(stdoutString, "Updating project groups"), true, "Expected output: %s", "Updating project groups") +} + func TestScanWorkflowMissingID(t *testing.T) { err := execCmdNotNilAssertion(t, "scan", "workflow") assert.Error(t, err, "Please provide a scan ID", err.Error()) @@ -590,11 +608,18 @@ func TestCreateScanProjectTags(t *testing.T) { "--project-tags", "test", "--debug") } -func TestCreateScanProjecGroupsError(t *testing.T) { +func TestCreateScan_WhenProjectExists_ShouldIgnoreGroups(t *testing.T) { + file := createOutputFile(t, outputFileName) + defer deleteOutputFile(t, file) + defer logger.SetOutput(os.Stdout) baseArgs := []string{scanCommand, "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch", - "--debug", "--project-groups", "err"} - err := execCmdNotNilAssertion(t, baseArgs...) - assert.Error(t, err, "Failed updating a project: Failed finding groups: [err]", err.Error()) + "--debug", "--project-groups", "anyProjectGroup"} + execCmdNilAssertion(t, baseArgs...) + stdoutString, err := util.ReadFileAsString(file.Name()) + if err != nil { + t.Fatalf("Failed to read log file: %v", err) + } + assert.Equal(t, strings.Contains(stdoutString, noUpdatesForExistingProject), true, "Expected output: %s", noUpdatesForExistingProject) } func TestScanCreateLastSastScanTimeWithInvalidValue(t *testing.T) { baseArgs := []string{"scan", "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch", "--sca-exploitable-path", "true", "--sca-last-sast-scan-time", "notaniteger"} @@ -1275,3 +1300,20 @@ func TestFilterMatched(t *testing.T) { }) } } + +func createOutputFile(t *testing.T, fileName string) *os.File { + file, err := os.Create(fileName) + if err != nil { + t.Fatalf("Failed to create log file: %v", err) + } + logger.SetOutput(file) + return file +} + +func deleteOutputFile(t *testing.T, file *os.File) { + file.Close() + err := os.Remove(file.Name()) + if err != nil { + logger.Printf("Failed to remove log file: %v", err) + } +} diff --git a/internal/services/projects_test.go b/internal/services/projects_test.go index 45382812e..00235b3fd 100644 --- a/internal/services/projects_test.go +++ b/internal/services/projects_test.go @@ -266,7 +266,6 @@ func Test_updateProject(t *testing.T) { ttt.args.applicationsWrapper, ttt.args.projectName, ttt.args.applicationID, - ttt.args.projectGroups, ttt.args.projectTags, ttt.args.projectPrivatePackage, ttt.args.featureFlagsWrapper) From 1a152c3d43b6516b67b078ea2856dc9dd9d9a67a Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Wed, 13 Nov 2024 10:47:14 +0200 Subject: [PATCH 4/7] fix issues from conflict --- internal/commands/scan_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index 10c7811e4..a720d36b5 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -3,7 +3,10 @@ package commands import ( + "archive/zip" + "bytes" "fmt" + "log" "os" "reflect" "strings" From 275f146b27ab90ca3b86e6628c6e54cef799dc97 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Wed, 13 Nov 2024 10:51:58 +0200 Subject: [PATCH 5/7] fix for lint --- internal/commands/scan_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/commands/scan_test.go b/internal/commands/scan_test.go index a720d36b5..c8d341feb 100644 --- a/internal/commands/scan_test.go +++ b/internal/commands/scan_test.go @@ -396,7 +396,7 @@ func TestCreateScan_WhenProjectNotExistsAndInvalidGroup_ShouldFail(t *testing.T) func TestCreateScan_WhenProjectNotExists_ShouldCreateProjectAndAssignGroup(t *testing.T) { file := createOutputFile(t, outputFileName) - defer deleteOutputFile(t, file) + defer deleteOutputFile(file) defer logger.SetOutput(os.Stdout) baseArgs := []string{"scan", "create", "--project-name", "newProject", "-s", ".", "--branch", "main", "--project-groups", "existsGroup1", "--debug"} @@ -613,7 +613,7 @@ func TestCreateScanProjectTags(t *testing.T) { func TestCreateScan_WhenProjectExists_ShouldIgnoreGroups(t *testing.T) { file := createOutputFile(t, outputFileName) - defer deleteOutputFile(t, file) + defer deleteOutputFile(file) defer logger.SetOutput(os.Stdout) baseArgs := []string{scanCommand, "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch", "--debug", "--project-groups", "anyProjectGroup"} @@ -1313,7 +1313,7 @@ func createOutputFile(t *testing.T, fileName string) *os.File { return file } -func deleteOutputFile(t *testing.T, file *os.File) { +func deleteOutputFile(file *os.File) { file.Close() err := os.Remove(file.Name()) if err != nil { From 1837f2221af3f4101877838f18211618af59ac87 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Wed, 13 Nov 2024 12:37:18 +0200 Subject: [PATCH 6/7] add integration test --- test/integration/scan_test.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index b01834551..34937fa34 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -12,6 +12,7 @@ import ( "log" "os" "path/filepath" + "reflect" "runtime" "strings" "testing" @@ -1551,7 +1552,9 @@ func TestScanGeneratingPdfReportWithPdfOptions(t *testing.T) { //} func TestScanCreateUsingWrongProjectGroups(t *testing.T) { - _, projectName := getRootProject(t) + projectID, projectName := getRootProject(t) + project := showProject(t, projectID) + groupsBeforeScanCreate := project.Groups args := []string{ scanCommand, "create", @@ -1561,10 +1564,20 @@ func TestScanCreateUsingWrongProjectGroups(t *testing.T) { flag(params.PresetName), "Checkmarx Default", flag(params.BranchFlag), "dummy_branch", flag(params.ProjectGroupList), "wrong_group", + "--async", } err, _ := executeCommand(t, args...) - assertError(t, err, "Failed finding groups") + if err != nil { + assertError(t, err, "running a scan should pass") + } + + project = showProject(t, projectID) + groupsAfterScanCreate := project.Groups + if !reflect.DeepEqual(groupsBeforeScanCreate, groupsAfterScanCreate) { + t.Errorf("When project exists, groups before and after scan creation should be equal. Got %v, want %v", groupsAfterScanCreate, groupsBeforeScanCreate) + } + } func TestScanCreateExploitablePath(t *testing.T) { _, projectName := getRootProject(t) From 27a5be5bdd4da9932309bc961d1037f20b9b01c7 Mon Sep 17 00:00:00 2001 From: miryamfoiferCX Date: Wed, 13 Nov 2024 13:13:05 +0200 Subject: [PATCH 7/7] change test name --- test/integration/scan_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/scan_test.go b/test/integration/scan_test.go index 34937fa34..fe9956a9c 100644 --- a/test/integration/scan_test.go +++ b/test/integration/scan_test.go @@ -1551,7 +1551,7 @@ func TestScanGeneratingPdfReportWithPdfOptions(t *testing.T) { // //} -func TestScanCreateUsingWrongProjectGroups(t *testing.T) { +func TestScanCreate_WhenProjectExists_ShouldNotUpdateGroups(t *testing.T) { projectID, projectName := getRootProject(t) project := showProject(t, projectID) groupsBeforeScanCreate := project.Groups